Seite 1 von 1

Wo ist der Fehler?

Verfasst: 4. April 2007 03:53
von kackerli
hi leute,

bin neu in qt ( und wies aussieht ach in c++ :) )
mein kleines progm wächst und wächst und ich find den fehler nicht...
was muss ich löschen? vielen dank schonmal

gruß kackerli

Code: Alles auswählen

 #include <QObject>
 #include <QApplication>
 #include <QFont>
 #include <QWidget>
 #include <QMainWindow>
 #include <QTimer>
 #include <windows.h>
 #include <QBrush>
 #include <QPainter>
 #include <QPaintEvent>
 #include <QEvent>

inline unsigned int pgc( int x, int y) // = PixelGetColor
{
	HDC screen;
    screen = GetDC(NULL);
    COLORREF farbescreen = GetPixel(screen, x , y ); // pos sollte in jedem programm sein!
	return farbescreen;
}


 class MyWindow : public QMainWindow
 {	 
	 private:
		int links, oben, size;
	
		POINT startpos, cursorpos;

	 public:
		 MyWindow(QWidget *parent = 0);
		 ~MyWindow();
		 void timerEvent(QTimerEvent * event); 
		 void paintEvent(QPaintEvent * event );

};

 MyWindow::MyWindow(QWidget *parent)  : QMainWindow(parent)
 {
     setFixedSize(321, 321);
	 startTimer(100);

 }

 MyWindow::~MyWindow()
{
}

 void MyWindow::timerEvent(QTimerEvent * event)
 { 
	update();	
 }

void MyWindow::paintEvent(QPaintEvent * event)
 {
	startpos.x = 1;
	startpos.y = 1;
   GetCursorPos( &cursorpos);
	size = 40;

	for( int i = 0; i < 8; ++i)
	{
		for( int n = 0; n < 8; ++n)
		{
			QPainter *painter=new QPainter(this);	
			QBrush *brush=new QBrush(Qt::SolidPattern);

			unsigned int farbe= pgc( cursorpos.x - 3 + i, cursorpos.y - 3 + n);
			brush->setColor(farbe);	
			
			links = i * size + startpos.x;
			oben = n * size + startpos.y;			
			
			painter->fillRect( links, oben, size-2, size-2, *brush );

			delete brush;
			brush = NULL;
			delete painter;
			painter = NULL;
		}
	}		
 }


 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     MyWindow widget;
     widget.show();
	 
     return app.exec();
 }

Verfasst: 4. April 2007 07:15
von macman
Wo ist das Problem?

Verfasst: 4. April 2007 09:14
von upsala
Ich habe zwar auch keine Ahnung wo der Fehler sein sollte, aber hör mal auf Windows und Qt-Funktionen zu mischen, vor allem wenn es nicht notwendig ist. Und arbeite nicht soviel mit Zeigern, Referenzen sind sicherer und leichter zu handhaben. (s. paintEvent-Code)

Code: Alles auswählen

void MyWindow::paintEvent(QPaintEvent * event)
{
   startpos=QPoint(1, 1);
   GetCursorPos( &cursorpos);
   size = 40;

   QPainter p(this);
   QBrush brush(Qt::SolidPattern);

   for( int i = 0; i < 8; ++i) {
      for( int n = 0; n < 8; ++n) {

         unsigned int farbe= pgc( cursorpos.x - 3 + i, cursorpos.y - 3 + n);
         brush.setColor(farbe);   
         
         links = i * size + startpos.x;
         oben = n * size + startpos.y;         
         
         painter.fillRect( links, oben, size-2, size-2, brush );
      }
   }      
 } 

Verfasst: 4. April 2007 12:08
von Burgpflanze

Code: Alles auswählen

int main(int argc, char *argv[])
 {
    QApplication app(argc, argv);
    MyWindow widget;
    widget.show();

    app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
 
    return app.exec();
 }

Verfasst: 4. April 2007 12:55
von kackerli
sorry das ichs nicht näher erleutert habe.

das problem ist, dass das programm "wächst" ( siehe TaskManager )

und nach ungefähr 1min nicht mehr funktioniert.

dasselbe passiert wenn man z.B. in der winAPI eine brush in einer schleife erstellt und nicht immer wieder löscht.

nur wie lösche ich hier ne brush, oder muss ich das überhaupt???

gruß kackerli

PS: euer neuer code hat auch nichts daran geändert.