Tom
Tom

Reputation: 65

Artefacts when drawing a scaled QImage onto a QWidget

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)

Upvotes: 1

Views: 2780

Answers (2)

gnud
gnud

Reputation: 78518

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));

Upvotes: 1

Arnold Spence
Arnold Spence

Reputation: 22272

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.

Upvotes: 5

Related Questions