PDA

View Full Version : paintEvent and Pixmap - not deleting old content!



StarShaper
13th February 2012, 19:31
Hi,

I would like to paint on a widget, but every time I call another function, it deletes the old widget content.

I already tried to create an instance of Pixmap with


pixmap = QPixmap(pixmap);

and removed fill() but this doesn't work.

Why is it possible to call


pixmap = QPixmap(size());

but not


pixmap = QPixmap(pixmap); ??

Here is the code snippet:


void ClientWindow::paintEvent(QPaintEvent * /* event */)
{
QStylePainter painter(this);
painter.drawPixmap(0, 0, pixmap);

if (hasFocus()) {
QStyleOptionFocusRect option;
option.initFrom(this);
option.backgroundColor = palette().dark().color();
//painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);
}
}

void ClientWindow::drawCircle(int x, int y, int width, int height)
{
pixmap = QPixmap(size());
pixmap.fill(this, 0, 0);

QPainter painter(&pixmap);
painter.initFrom(this);

pen.setStyle(Qt::SolidLine);
pen.setWidth(3);
pen.setBrush(Qt::green);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);

brush.setStyle(Qt::SolidPattern);

painter.setPen(pen);
//painter.setBrush(brush);

if (antialiased)
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawEllipse(x, y, width, height);
update();
}

void ClientWindow::drawLine(int x1, int y1, int x2, int y2)
{
pixmap = QPixmap(size());
pixmap.fill(this, 0, 0);

QPainter painter(&pixmap);
painter.initFrom(this);

//...

Spitfire
17th February 2012, 09:59
I'm bit confused what you're trying to do here.

Why you are drawing widget to pixmap?
Why you are drawing in each function?

Can't you cache the widget image once and then draw on top of it multiple times?
If the content of the widget changes often, there's no point caching it.
If the content doesn't change but your drawing do - then create separate cache for widget and separate for drawings and draw second on top of the first one.

Like I said - I'm bit confused so you need to explain what you're trying to achieve bit better.