解决Qt6 C++ Qpainter 画图时 Painter not active QWidget::paintEngine: Should no longer be calledQPaint的问题
画图出现问题如下:QWidget::paintEngine: Should no longer be calledQPainter::begin: Paint device returned engine == 0, type: 1QPainter::setPen: Painter not activeQPainter::setPen: Painter not activeQPainter::en
·
画图出现问题如下:
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not active
QPainter::setPen: Painter not active
QPainter::end: Painter not active, aborted
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not active
QPainter::setPen: Painter not active
QPainter::end: Painter not active, aborted
0
QPainter::setFont: Painter not active
QPainter::setPen: Painter not active
QPainter::setPen: Painter not active
QPainter::end: Painter not active, aborted
0
解决方法:在Mainwindow的类中重载paintEvent()函数,在函数小括号后添加override,之后即便报错,也能够画出我们要求的图像,不仅可以,也可以更新图像。
源代码mainwindow.h如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QPainter>
#include<QWidget>
#include<QFont>
#include<QPen>
#include<QGraphicsView>
#include<QPaintEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
//void paintEvent(QPaintEvent *event);
QPaintEvent *e;//如果去掉星号会变成error
QPainter painter2;
private:
Ui::MainWindow *ui;
protected:
void paintEvent(QPaintEvent *event)override;
};
#endif // MAINWINDOW_H
mainwidow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include<iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
//QPainter p(this); //QWidget::paintEngine: Should no longer be called
ui->setupUi(this);
paintEvent(this->e);
}
MainWindow::~MainWindow()
{
delete ui;
}
//void MainWindow::painter(QPainter *painter1){
void MainWindow::paintEvent(QPaintEvent *event){
QPainter painter2(this);
cout<<painter2.isActive()<<endl;
int BASESIZE = 8;
int SIZE = 4;
int gate = 1;
int enemyNum = 23;
//painter2.begin(ui->centralwidget); //QWidget::paintEngine: Should no longer be called
//painter2.beginNativePainting();
painter2.begin(this);
QPoint x(560,55);
painter2.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter2.fillRect(100 - 20, 100 - 20,
80, 80, QColor(255,0,0,255));
//设置字体属性
QFont font;//字体属性类对象,可以设置字体的属性
font.setPixelSize(18);//设置字体大小
font.setUnderline(true);//设置下划线
font.setOverline(true);//设置上划线
font.setItalic(true);//设置斜体
font.setBold(true);//设置加粗
font.setCapitalization(QFont::SmallCaps);//设置字母大小写
font.setLetterSpacing(QFont::AbsoluteSpacing,10);//设置字符间距
painter2.setFont(font);//使用字体属性
painter2.setPen(Qt::red);//设置画笔颜色
painter2.rotate(10);
painter2.drawText(6*SIZE/4,BASESIZE,"第"+QString::number(gate)+"关,现存敌人"+QString::number(enemyNum));
QColor color(0,0,0,255);
QPen pen;
pen.setWidth(4);
pen.setColor(color);
painter2.setPen(pen);
painter2.drawLine(QPoint(560-400,55),QPoint(620-400,55));
painter2.drawLine(QPoint(590-400,45),QPoint(620-400,55));
painter2.drawLine(QPoint(590-400,65),QPoint(620-400,55));
painter2.end();
// this->update(); //此处没有意义
}
在主程序中调用画图函数,main.cpp不做改动:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
效果图如下,里面有三个图像:
如有问题,请您留言。
更多推荐
已为社区贡献1条内容
所有评论(0)