PDA

View Full Version : Qt3To4 port: replacement of QLabel::drawContents method



brazso
18th August 2010, 15:13
Hello,

I try to overwrite a bigger project from Qt3 to Qt4 and there is problem with the method of QLabel::drawContents. I know that drawContents was exiled from Qt4, and the proposed replacement is the usage of paintEvent(QPaintEvent*). I'm confused about replacing a draw function with an event. I suppose at first I have to implement the paintEvent in my class, then I have to call it with (QWidget:: ) update or repaint methods.

In my exact case there is a new class named ButtonPreview inherited from QLabel. (It is defined as virtual protected in the header file.) Its implementation looks like this in Qt3:


void ButtonPreview::drawContents(QPainter *painter)
{
QLabel::drawContents(painter);

// ... additional drawing
}

I suppose QLabel :: drawContents does nothing but I do not want to remove that line, it would be too simple :)
I wish to invoke QLabel :: paintEvent. Do I alter drawContents(painter) to "update" or "repaint"? I'm afraid I have to use "repaint" here, paintEvent must be executed immediately and synchronously before the execution of "additional drawing" part.
The ugliest case is if I have to create ButtonPreview :: paintEvent too with the content of ButtonPreview :: drawContents.

Brazso

The Storm
19th August 2010, 14:10
Just call the base class method in your case QLabel::paintEvent( event ); and then below the call initialize new painter and draw whatever you want to draw. If you want to keep your drawContents method do it like this:


void ButtonPreview::paintEvent( QPaintEvent* event )
{
QLabel::paintEvent( event );

QPainter painter( this );

drawContents( &painter ); // ... additional drawing
}


When you want to get the paintEvent called just call update() or repaint()

If I missed your point, sorry. :)