hitcounter
dpointer
Tuesday, September 28, 2004
 
Paint Your Form

Answering one question about QPainter in the mailing-list, I gave a short example like below. This is a single-file, Qt-based application, so perhaps you can compile directly and save time by avoiding qmake. The purpose of this small program is very simple, paint a picture, taken from file pic.jpg, to its main window. Of course, to try it you need to supply the JPEG file (could be anything).

#include <qmainwindow.h>
#include <qapplication.h>
#include <qpainter.h>
#include <qpixmap.h>

class PainterExample : public QMainWindow
{
public:
PainterExample();
virtual void paintEvent( QPaintEvent* );
};

PainterExample::PainterExample(): QMainWindow( 0, "painterexample" )
{
}

void PainterExample::paintEvent( QPaintEvent* )
{
QPainter p( this );
p.drawPixmap( 0,0, QPixmap("pic.jpg"),0,0,100,100 );
}

int main( int argc, char ** argv )
{
QApplication a( argc, argv );
QMainWindow* v = new PainterExample( );
v->show();
a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
return a.exec();
}

Wednesday, September 15, 2004
 
Idle

You want to process something while your application is in idle state? Look no more, and even no need to understand multithreading programming. This cheap trick is a fabulous replacement for thread (this is also mentioned in QTimer documentation):

 idleTimer = new QTimer( this );
QObject::connect( idleTimer, SIGNAL(timeout()), SLOT(idle()) );
idleTimer->start( 0, FALSE );

Of course this is rather quick-and-dirty. But still on most cases it works rather well. Or, use the real QThread if you don't like.



Powered by Blogger