QPalette newPalette = palette(); newPalette.setBrush(QPalette::Window, QBrush(Qt::NoBrush)); setPalette(newPalette);
QPainter painter(this); painter.setPen(privatePen); painter.setBrush(palette().brush(QPalette::Button)); painter.drawEllipse(privateRect);
setRect(QRect newRect){ privateRect = newRect; resize(privateRect.width(), privateRect.height()); update(privateRect.width(), privateRect.height());}
void paintTo( QPainter &p ){ ...}
QPixmap myimage;QPainter painter( &myimage );painter.setPen(privatePen);painter.setBrush(palette().brush(QPalette::Button));painter.drawEllipse(privateRect);
class MyWidget : public QWidget {protected: QPaintDevice * paintdevice; void paintEvent( QPaintEvent * );public: MyWidget( QWidget * parent=NULL ); const QPaintDevice * currentPaintDevice(); void setCurrentPaintDevice( QPaintDevice * pd );};MyWidget::MyWidget( QWidget * parent ) : QWidget( parent ) { paintdevice = this;}void MyWidget::paintEvent( QPaintEvent * ) { Q_ASSERT( paintdevice != NULL ); QPainter painter( paintdevice ); painter.setPen( privatePen ); painter.setBrush( palette().brush( QPalette::Button ) ); painter.drawEllipse( privateRect );}const QPaintDevice * MyWidget::currentPaintDevice() { return paintdevice;}void MyWidget::setCurrentPaintDevice( QPaintDevice * pd ) { if ( pd == NULL ) return; paintdevice = pd;}
class MyWidget : public QWidget {protected: QPaintDevice * paintdevice; void paintEvent( QPaintEvent * );public: MyWidget( QWidget * parent=NULL ); const QPaintDevice * currentPaintDevice(); void setCurrentPaintDevice( QPaintDevice * pd ); void drawTo( QPaintDevice * pd );};MyWidget::MyWidget( QWidget * parent ) : QWidget( parent ) { paintdevice = this;}void MyWidget::paintEvent( QPaintEvent * ) { drawTo( paintdevice );}const QPaintDevice * MyWidget::currentPaintDevice() { return paintdevice;}void MyWidget::setCurrentPaintDevice( QPaintDevice * pd ) { if ( pd == NULL ) return; paintdevice = pd;}void MyWidget::drawTo( QPaintDevice * pd ) { Q_ASSERT( pd != NULL ); QPainter painter( pd ); painter.setPen( privatePen ); painter.setBrush( palette().brush( QPalette::Button ) ); painter.drawEllipse( privateRect );}
QPixmap image;MyWidget::drawTo( &image );
class MyBaseWidget : public QWidget { ... virtual void drawTo( QPaintDevice * pd ) = 0; ...};
class MyEllipseWidget : public MyBaseWidget { ... void drawTo( QPaintDevice * pd ) { Q_ASSERT( pd != NULL ); QPainter painter( pd ); painter.setPen( privatePen ); painter.setBrush( palette().brush( QPalette::Button ) ); painter.drawEllipse( privateRect ); } ...};class MyQuadWidget : public MyBaseWidget { ... void drawTo( QPaintDevice * pd ) { Q_ASSERT( pd != NULL ); QPainter painter( pd ); painter.setPen( privatePen ); painter.setBrush( palette().brush( QPalette::Button ) ); painter.drawEllipse( privateRect ); } ...};
QList<MyBaseWidget*> mywidgets; foreach ( MyBaseWidget * widget, mywidgets ) { QPixmap image; widget->drawTo( &image ); }