On Tuesday 21 September 2004 15:46, Prabu Subroto wrote:
I have not understood how should I use QPaintEvent. All of the example code show this function in this form " void pixForm::paintEvent( QPaintEvent * event) ".
Since I just want to make a try with "init()" (constructor) I don't understand how should the QPaintEvent implemented in my case.
I just want if my program executed than the pixForm form appear, executes the constructor ( init() ), and finally the image file showed up in the dialog form (pixForm) .
X doesn't work this way (come to think of it, neither does Win32). You don't actively begin painting on the screen, the windowing system tells your application when to do that - by sending an event. In X11 lingo this is called an "expose event" - which means that parts of your application's window (or all of it) are "exposed", i.e. either uncovered from windows that had been obscuring it, or simply displayed for the first time. In Qt this is mapped directly to the paintEvent() - a virtual function every QWidget has. Normally, you don't have to care about this, but if you want to do low-level drawing, this is your entry point. For your purposes (just displaying PNG or JPG graphics) you could also have used a QLabel with a QPixmap in it - I had suggested that earlier in this thread. It's basically like this: QLabel * imageViewer = new QLabel( parent ); QPixmap * pixmap = new QPixmap( "/wherever/mypixmap.png" ); imageViewer->setPixmap( pixmap ); But of course you can also derive your own custom widget class and do that yourself - by overwriting the paintEvent() virtual function and doing your own drawing there. Of course, you also have to take care of resizing your widget according to the pixmap's size (something QLabel also takes care of automatically).
Please give me a very short example code, please... the example from trolltech is too complicated for me at this time.
Too complicated?! http://doc.trolltech.com/3.3/showimg-example.html#x1260 void ImageViewer::paintEvent( QPaintEvent *e ) { if ( pm.size() != QSize( 0, 0 ) ) { // is an image loaded? QPainter painter(this); painter.setClipRect(e->rect()); painter.drawPixmap(0, menubar->heightForWidth( width() ), pmScaled); } } It doesn't get very much easier than this...
what does the QPaintEvent have to do with QPainter? I meant the relationship.
paintEvent() typically uses a QPainter to do the drawing.
I know from my book that QPaintEvent is a trigger but why I can not use init() to start the image file to be showed up.
X clients (applications) are not supposed to initiate drawing. They are
supposed to do that upon receiving an expose event. Since the X protocol is a
pretty complex client-server architecture, you might be lucky if you violate
that convention, and sometimes it might even work out and your image might be
displayed, but you might as well be not so lucky. I'd rather not rely on my
luck too much in programming. ;-)
--
Stefan Hundhammer