最近初学qt绘图,在MainWindow的paintEvent()中绘图,直接在MainWindow中绘图是可以的,但如果要在MainWindow中的widget中绘图,就报这个错
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
,代码如下,到底哪里有问题
void MainWindow::paintEvent(QPaintEvent *event)
{
// QPainter painter(this); ①
QPainter painter(ui->widget); ②
{
painter.setPen(QPen(QColor(255, 0, 0), 3));
painter.drawLine(QPointF(50, 0), QPointF(60, 50));
painter.setBrush(QColor(255, 160, 90));
painter.drawRect(50, 50, 100, 100);
painter.setFont(QFont("Arial", 30));
painter.drawText(200, 200, 100, 100, Qt::AlignCenter, "Qt");
// QImage image("./img103.png");
// painter.drawImage(0, 0, image, );
// painter.drawPixmap(50, 50, 200, 100, QPixmap("./img103.png"));
painter.end();
}
}
①的写法,代表直接在MainWindow绘图,没问题
但②的写法,是想要在widget中绘图,编译没问题,运行时就报错,到底该怎么做呢
MainWindow布局如下