#include <QApplication>
#include <QWidget>
#include <QImage>
#include <QPainter>
Q_OBJECT
public:
}
protected:
private:
void init();
};
{
init();
}
void MyWidget :: init()
{
QImage bg
( width
(), height
(),
QImage::Format_ARGB32_Premultiplied );
QImage a
( width
(), height
(),
QImage::Format_ARGB32_Premultiplied );
QImage b
( width
(), height
(),
QImage::Format_ARGB32_Premultiplied );
p.begin( &bg );
p.fillRect( r, Qt::white );
p.end();
p.begin( &a );
p.drawLine( 10, 10, 50, 50 );
p.end();
p.begin( &b );
p.drawLine( 10, 70, 50, 10 );
p.end();
p.begin( this );
p.
drawImage( QPoint(0,
0), bg
);
p.
drawImage( QPoint(0,
0), a
);
p.
drawImage( QPoint(0,
0), b
);
p.end();
}
int main(int argc, char **argv)
{
MyWidget w;
w.show();
return app.exec();
}
#include "main.moc"
#include <QApplication>
#include <QWidget>
#include <QImage>
#include <QPainter>
class MyWidget: public QWidget {
Q_OBJECT
public:
MyWidget(QWidget *p = 0): QWidget(p) {
}
protected:
void paintEvent ( QPaintEvent * event );
private:
void init();
};
void MyWidget :: paintEvent ( QPaintEvent * event )
{
init();
}
void MyWidget :: init()
{
QImage bg( width(), height(), QImage::Format_ARGB32_Premultiplied );
QImage a( width(), height(), QImage::Format_ARGB32_Premultiplied );
QImage b( width(), height(), QImage::Format_ARGB32_Premultiplied );
QPainter p;
p.begin( &bg );
QRect r = rect();
p.fillRect( r, Qt::white );
p.end();
p.begin( &a );
p.setPen( QPen( QColor( Qt::red ), 5 ) );
p.drawLine( 10, 10, 50, 50 );
p.end();
p.begin( &b );
p.setPen( QPen( QColor( Qt::green ), 5 ) );
p.drawLine( 10, 70, 50, 10 );
p.end();
p.begin( this );
p.drawImage( QPoint(0,0), bg );
p.drawImage( QPoint(0,0), a );
p.drawImage( QPoint(0,0), b );
p.end();
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MyWidget w;
w.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
The widget does indeed get garbled as you resize the window. When you create a QImage as you have done it is not initialised (as clearly stated in the docs). You fill the background image with white but leave the others with whatever random rubbish they started with. On top of the random rubbish you draw a line, then draw the combined result over the white background image. It's dumb luck if the images start out clear of rubbish (possibly because the memory is zeroed when first allocated to the process by the OS). In my case, each time through the paintEvent() the images are put in slightly different memory locations that overlapped their previous position so recognisable ghosts of former images are visible.
class TypicalWidget
: public QWidget { Q_OBJECT
public:
}
protected:
p.fillRect(rect(), Qt::white);
p.drawLine( 10, 10, 50, 50 );
p.drawLine( 10, 70, 50, 10 );
}
};
class TypicalWidget: public QWidget {
Q_OBJECT
public:
TypicalWidget(QWidget *p = 0): QWidget(p) {
}
protected:
void paintEvent ( QPaintEvent * event ) {
QPainter p(this);
p.fillRect(rect(), Qt::white);
p.setPen( QPen( QColor( Qt::red ), 5 ) );
p.drawLine( 10, 10, 50, 50 );
p.setPen( QPen( QColor( Qt::green ), 5 ) );
p.drawLine( 10, 70, 50, 10 );
}
};
To copy to clipboard, switch view to plain text mode
but you might have a reason.
Bookmarks