QPainter ouside of paintEvent with a pixmap
I'm porting a Qt 3.3.2 application to Qt 4.1.1
In Qt 3 you can create a QPainter p(&pixmap) and paint to the pixmap anywhere in the code.
In Qt 4 it says that all painting must take place inside the paintEvent.
Why is that the following will work in Qt 4 outside of paintEvent
Code:
void someFunc()
{
Pixmap pix(size());
pix.fill(this, 0, 0);
// paint away
}
but, the following will not.
Code:
// here ptrPix was filled by another function
void someFunc()
{
QPainter p
(ptrPix
);
// QPainter::begin(), paintdevice returned engine == 0, type: 2 // paint away
}
am i missing a concept here?
I will move all of my painting to the paintEvent so that I can take advantage of the double buffering built in to Qt 4, I just want to know what is going on with the above statements.
Thanks.
Re: QPainter ouside of paintEvent with a pixmap
Quote:
From Qt docs:
Unless a widget has the Qt::WA_PaintOutsidePaintEvent attribute set. A QPainter can only be used on a widget inside a paintEvent() or a function called by a paintEvent().
This is about painting on a widget, not on a pixmap.
Re: QPainter ouside of paintEvent with a pixmap
The first example creates a QPixmap of the widgets size and fills it with the widgets background color or pixmap. Then you can paint on the pixmap with that QPainter.
The second one assigns a prefilled pixmap pointer to a QPainter and then paints on the pixmap.
They seem to be doing the same thing, which neither is painting to a widget.
So, I'm still confused why one would work and one would not.
Please explain more if I'm missing a point.
Re: QPainter ouside of paintEvent with a pixmap
Re: QPainter ouside of paintEvent with a pixmap
Yes 5 of them in this application.
Re: QPainter ouside of paintEvent with a pixmap
Do you modify QPixmaps in non-GUI thread? If yes, then you will have to switch to QImages.
Re: QPainter ouside of paintEvent with a pixmap
After trying some more things I'm confused as to why the following don't work in Qt 4.1.1
This is an application not using threads.
Code:
{
someFunc(&painter);
painter.begin(this);
painter.
drawPixmap(QPoint(0,
0),
*pmap
);
painter.end();
}
void MyType
::someFunc(QPainter *painter
) {
painter->begin(pmap);
painter->setPen(dark);
painter->drawLine(0, 0, 50, 50);
// .. do something useful
painter->end();
}
Re: QPainter ouside of paintEvent with a pixmap
How do you create that pmap pixmap?
Re: QPainter ouside of paintEvent with a pixmap
Code:
{
public:
// ... methods
private:
};
void MyType::createImage()
{
image->fill(backgroundColor->rgb());
for (int i=0; i<50; i++)
{
QRgb rgb = qRgb(i, i+25, i+50);
for (int x=0; x<50; ++x)
image->setPixel(x, i, rgb);
}
convertImage();
}
void MyType::convertImage()
{
pmap->fromImage(*image, Qt::PreferDither);
update();
}
Hope this is enough sudo code for you. ;)
Re: QPainter ouside of paintEvent with a pixmap
QPixmap::fromImage() is static.
It should be something like:
Code:
void MyType::convertImage()
{
update();
}
Re: QPainter ouside of paintEvent with a pixmap
Good catch, now it makes sense. Since static member functions have no "this" pointer my usage was wrong. That fixed it.
Thanks again.