这个还是以前遇到的,今天突然想起来,就决定重现一下当初的错误,以及错误的代码。
报错是这个样子的:
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
其实这个原因很简单,来看看报错的代码:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPixmap> #include <QPainter> void MainWindow::paintEvent(QPaintEvent *e) { } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QPixmap pix; pix.load(":/new/prefix1/QT.png"); QPainter p(this); p.drawPixmap(100,100,100,100,pix); } MainWindow::~MainWindow() { delete ui; }
而去掉这个错误,只需要这样:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPixmap> #include <QPainter> void MainWindow::paintEvent(QPaintEvent *e) { QPixmap pix; pix.load(":/new/prefix1/QT.png"); QPainter p(this); p.drawPixmap(100,100,100,100,pix); } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; }
画图这类事情要放到paintevent函数里面去做。这个具体的机制不知道。
转 : https://blog.csdn.net/xuancailinggan/article/details/50603141
标签:longer,no,Should,pix,ui,QWidget,100,include,MainWindow From: https://www.cnblogs.com/fps2tao/p/17751606.html