#include <QPainter> void MyView::paintEvent(QPaintEvent * event) { qDebug("************MyView::paintEvent*****************"); // QPainter painter; //QWidget::paintEngine: Should no longer be called QPainter painter(this->viewport()); //關鍵這一句 QLinearGradient ling(QPointF(70, 70), QPoint( 140, 140 ) ); //從起點到終點的漸變 ling.setColorAt(0, Qt::blue); //起點到中心要顯示的顏色 ling.setColorAt(1, Qt::green); ling.setSpread( QGradient::PadSpread ); //默認顯示模式 QBrush brush(ling); painter.setBrush(brush); painter.drawRect(0, 0, 200, 200); painter.end(); QGraphicsView::paintEvent(event); }