tags:

views:

61

answers:

2

Hi there.

My main window has the following draw-function:

void MainWindow::paintEvent(QPaintEvent*)
{
    QImage sign(50, 50, QImage::Format_ARGB32_Premultiplied);
    QPainter p(&sign);
    p.setRenderHint(QPainter::Antialiasing, true);
    p.fillRect(sign.rect(), QColor(255, 255, 255, 0));
    p.setBrush(Qt::blue);
    p.setPen(Qt::NoPen);
    p.drawEllipse(0, 0, sign.width(), sign.height());
    p.end();

    QPainter painter(this);
    painter.drawImage(rect(), sign, sign.rect());
} 

So basically, it draws a blue filled circle onto a QImage and than draws that QImage onto the widget. However, when I resize the window, I get weird artefacts (in the upper left corner). This is what it looks like:

original: alt text

after changing the window size: alt text

Does anyone have an idea why this is?

(I'm working under Ubuntu 10.04, if that's of interest)

+2  A: 

I think your QImage is initialized with garbage. After constructing it, call sign.fill(). I tried your code and the artifacts were present even before resizing on my machine.

From the Qt docs:

QImage::QImage ( int width, int height, Format format )

Constructs an image with the given width, height and format.

Warning: This will create a QImage with uninitialized data. Call fill() to fill the image with an appropriate pixel value before drawing onto it with QPainter.

Arnold Spence
Thank you very much, that did the trick! :) I was assuming the "p.fillRect(sign.rect(), QColor(255, 255, 255, 0));" would do the "initialization", since it'd fill the complete QImage with a color. However the artifacts have gone once I inserted a "fill()"-call. I'm not sure I understand why, though. However, thank you very much for your answer! :)
Tom
I believe the problem is that p.fillRect() with the specified color and transparency is filling a rectangle with a color that is adjusted by the transparency value specified. It does not actually alter the alpha channel of the image which retains its random uninitialized values. This is just a guess though.
Arnold Spence
+1  A: 

Your image is transparent (except for the circle), and you never clear the window before painting the (resized) image, so artifacts from the previous circle/window size might be left over.

Before you draw the image into the window, add these lines:

QPalette palette = QApplication::palette();
painter.fillRect(event->rect(), palette.color(QPalette::Window));
gnud
Thanks for the tip. :) I tried that, but it didn't actually solve the problem. But still, you are correct that the widget needs to be cleared :)
Tom