QT-WIdget



  • kennt sich hier einer mit QT aus und kann mir bei dem widget hier helfen
    (es soll eine kugel sein die auf die pfeiltasten reagirt mit entsprechneder richtungsanderung)
    hier der Code:
    header:

    #include <qwidget.h>
    #include <qcolor.h>
    #include <qstring.h>
    #include <qpainter.h>
    #include <qbrush.h>
    
    class Kugel : public QWidget
    {
    	Q_OBJECT
    	Q_PROPERTY(QColor Color READ getColor WRITE setColor)
    	//Q_PROPERTY(int Geo[4] READ getGeo WRITE setGeo)
    	public:
    		Kugel(QWidget *parent = 0, const char *name = 0);
    		Kugel(QWidget *parent = 0, const char *name = 0, QColor Color = black);
    		Kugel(QWidget *parent = 0, QColor Color = black, const char *name = 0);
    		Kugel(QWidget *parent = 0, const char *name = 0, QColor Color = black , int x = 0, int y = 0, int w = 0, int h = 0);
    		Kugel(QWidget *parent = 0, const char *name = 0, QColor Color =  black, short x1 = 0, short y1 = 0, short x2 = 0, short y2 = 0);
    		~Kugel();
    		QColor Color;
    		int x;
    		int y;
    		long xy;
    		int w;
    		int h;
    		short x1;
    		short y1;
    		short x2;
    		short y2;
    
    	public slots:
    		void setColor(QColor  newColor);
    		void setGeo(int x, int y, int w, int h);
    		void setXY(int x, int y);
    		void setWidth(int w);
    		void setHeight(int h);
    		void setX(int x);
    		void setY(int y);
    		QColor getColor();
    		int getWidth();
    		int getHeigth();
    		int getX();
    		int getY();
    
    	protected:
    		void keyEvent(QKeyEvent *event);
    		void paintEvent(QPaintEvent *event);
    };
    

    implementierung

    #include "kugel.h"
    
    Kugel::Kugel(QWidget *parent, const char *name) : QWidget(parent, name)
    {
    	x = 0;
    	y = 0;
    	w = 0;
    	h = 0;
    	Color = black;
    	setCaption("Kugel 2005, Copyright (C)by Flozq(Flozq.news@gmx.de) under the terms of the QT-free-license for Linux edited with Kate");
    	repaint(x, y, w, h);
    }
    Kugel::Kugel(QWidget *parent, const char *name, QColor inColor) : QWidget(parent, name)
    {
    	x = 0;
    	y = 0;
    	w = 0;
    	h = 0;
    	Color = inColor;
    	setCaption("Kugel 2005, Copyright (C)by Flozq(Flozq.news@gmx.de) under the terms of the QT-free-license for Linux edited with Kate");
    	repaint(x, y, w, h);
    }
    Kugel::Kugel(QWidget *parent, QColor inColor, const char *name) : QWidget(parent, name)
    {
    	x = 0;
    	y = 0;
    	w = 0;
    	h = 0;
    	Color = inColor;
    	setCaption("Kugel 2005, Copyright (C)by Flozq(Flozq.news@gmx.de) under the terms of the QT-free-license for Linux edited with Kate");
    	repaint(x, y, w, h);
    }
    Kugel::Kugel(QWidget *parent , const char *name, QColor inColor , int x , int y, int w, int h) : QWidget(parent, name)
    {
    	setCaption("Kugel 2005, Copyright (C)by Flozq(Flozq.news@gmx.de) under the terms of the QT-free-license for Linux edited with Kate");
    	Color = inColor;
    	repaint(x, y, w, h);
    }
    
    Kugel::Kugel(QWidget *parent, const char *name, QColor inColor, short x1, short y , short x2, short y2) : QWidget(parent, name)
    {
    	x = x1;
    	y = y1;
    	w = y2 - y1;
    	h = x2 - x1;
    	Color = inColor;
    	setCaption("Kugel 2005, Copyright (C)by Flozq(Flozq.news@gmx.de) under the terms of the QT-free-license for Linux edited with Kate");
    	repaint(x, y, w, h);
    }
    Kugel::~Kugel()
    {
    	delete this;
    }
    void Kugel::setGeo(int newX ,int newY, int newW, int newH)
    {
    	x = newX;
    	y = newY;
    	w = newW;
    	h = newH;
    	repaint(x, y, w, h);
    }
    void Kugel::setXY(int newX, int newY)
    {
    	x = newX;
    	y = newY;
    	repaint(x, y, w, h);
    }
    void Kugel::setColor(QColor newColor)
    {
    	Color = newColor;
    	repaint(x, y, w, h);
    }
    void Kugel::setWidth(int newW)
    {
    	w = newW;
    	repaint(x, y, w, h);
    }
    void Kugel::setHeight(int newH)
    {
    	h = newH;
    	repaint(x, y, w, h);
    }
    void Kugel::setX(int newX)
    {
    	x = newX;
    	repaint(x, y, w, h);
    }
    void Kugel::setY(int newY)
    {
    	y = newY;
    	repaint(x, y, w, h);
    }
    
    QColor Kugel::getColor()
    {
    	return Color;
    }
    int Kugel::getWidth()
    {
    	return w;
    }
    int Kugel::getHeigth()
    {
    	return h;
    }
    int Kugel::getX()
    {
    	return x;
    }
    int Kugel::getY()
    {
    	return y;
    }
    void Kugel::keyEvent(QKeyEvent *event)
    {
    	switch(event->key())
    	{
    		case Qt::Key_Up:
    			x++;
    			repaint(x, y, w, h);
    		case Qt::Key_Left:
    			y--;
    			repaint(x, y, w, h);
    		case Qt::Key_Down:
    			x--;
    			repaint(x, y, w, h);
    		case Qt::Key_Right:
    			y++;
    			repaint(x, y, w, h);
    	}
    }
    void Kugel::paintEvent(QPaintEvent *event)
    {
    	if(event)
    	{
    		QPainter painter(this);
    		painter.setPen(Qt::SolidLine);
    		painter.setPen(1);
    		painter.setPen(Qt::black);
    		painter.setBrush(Qt::SolidPattern);
    		painter.setBrush(Color);
    		painter.drawEllipse(x, y, w, h);
    	}
    }
    


  • Du könntest zum Testen schon ein ganzes Programm zeigen.



  • War da jetzt Ironie mit drin? 😃



  • was soll das heisen ganzes programm?
    hier ist auch noch main.cpp-Code

    #include <qapplication.h>
    #include <qlabel.h>
    #include <qhbox.h>
    #include "kugel.h" 
    int main(int argc, char *argv[])
    {
    	QApplication app(argc, argv);
    	QColor Gelb;
    	QColor Blau;
    	QHBox *hbox = new QHBox(0);
    	hbox->setBackgroundColor(Qt::black);
    	Kugel gelb(hbox, Gelb);
    	Kugel blau(hbox, Blau);
    	app.setMainWidget(hbox);
    	hbox->show();
    	return app.exec();
    }
    

    und hier die pro datei

    ######################################################################
    # Automatically generated by qmake (1.06c) Mo 23. Mai 18:37:04 2005
    ######################################################################
    
    TEMPLATE = app
    INCLUDEPATH += .
    
    # Input
    HEADERS += kugel.h
    SOURCES += kugel.cpp main.cpp
    


  • ich kann hier doch keine .exe datei hochladen , also muss ich euch den code zeiogen, oder?
    😕



  • Also:

    1. Es sind nur zwei Konstruktoren notwendig. Die anderen sind darin enthalten oder überflüssig:

    Kugel(QWidget *parent = 0, QColor Color = black, const char *name = 0);
    Kugel(QWidget *parent = 0, const char *name = 0, QColor Color = black , int x = 50, int y = 50, int w = 100, int h = 100);

    (Default werte, um was zu sehen sind besser als 0)

    2. QColor getColor() const; ist besser als QColor getColor();

    3. Im Konstruktor solltest du die Werte auch initialisieren:

    Kugel::Kugel(QWidget *parent , const char *name, QColor inColor , int x , int y, int w, int h) : QWidget(parent, name),
       x(x), y(y), w(w), h(h)
     {
         setCaption("Kugel 2005, Copyright (C)by Flozq(Flozq.news@gmx.de) under the terms of the QT-free-license for Linux edited with Kate");
         Color = inColor;
         repaint(x, y, w, h);
     }
    

    4. Es gibt IMO kein keyEvent sondern andere: keyPressEvent zum Beispiel.

    5. Du musst dafür sorgen, dass dein Event auch ankommt. Dazu muss dein Widget grad aktiv sein.

    Obige Änderungen zusammen mit keyPressEvent ergeben bei folgendem Programm einen guten Startpunkt:

    #include <qapplication.h>
    #include <kugel.h>
    
    int main( int argc, char **argv )
    {
        QApplication a( argc, argv );
    
        Kugel hello( 0, "Kugel" );
    
        hello.resize( 500, 500 );
    
        a.setMainWidget( &hello);
        hello.show();
        return a.exec();
    }
    


  • danke , mal probieren obs funktioniert 🙂 🙂 🙂 🙂 🙂 🙂 🙂 😋


Anmelden zum Antworten