添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
乐观的青蛙  ·  DESeq2: interpreting ...·  3 天前    · 
大鼻子的镜子  ·  Vue项目npm run ...·  1 月前    · 
逃课的硬币  ·  ASP.NET Core – ...·  2 月前    · 

标题问题同时在运行后会附带以下问题:

QPainter::setPen: Painter not active
QPainter::font: Painter not active
QPainter::setFont: Painter not active

QWidget::paintEngine: Should no longer be called

QPainter::begin: Paint device returned engine == 0, type: 1

以上问题的解决办法就是在 绘图事件函数内创建画家 基本即可解决,前提是你是想要在父窗口进行图像绘制。

父窗口:绘制图像,需要继承QWidget类然后重写绘图事件,最后在绘图事件函数内创建画家即可。( 构造函数内不要创建画家

protected:
    void paintEvent(QPaintEvent *event)override;
void CWidget::paintEvent(QPaintEvent *event)
    QPainter thePainter(this);
    drawText(thePainter);
    event->accept();

 父窗口内的小部件:绘制图像,需要安装事件过滤器,将父窗口的事件传入到小部件中,由小部件来执行该事件

ui.widget->installEventFilter(this);

安装完事件过滤器后,重写事件过滤器函数,在创建画家时记得把小部件对象给到画家。

protected:
    void paintEvent(QPaintEvent *event)override;
    bool eventFilter(QObject *watched, QEvent *event);
bool CWidget::eventFilter(QObject *watched, QEvent *event)
    if (watched == ui.widget && event->type() == QEvent::Paint)
        drawText();
        return true;
    return QWidget::eventFilter(watched, event);

即QPainter thePainter(ui.widget);

void CWidget::drawText()
    QPainter thePainter(ui.widget);
    thePainter.setPen(QColor::fromRgb(253, 210, 8));
    QFont myFont = thePainter.font();
    myFont.setPointSize(10);
    myFont.setBold(true);
    QFontMetrics myFontMetrics(myFont);
    QRect myWinRect = rect();
    int width = myWinRect.width();
    int height = myWinRect.height();
    int myOffset = myFont.pointSize();
    thePainter.setFont(myFont);
    QRect myRect = myFontMetrics.boundingRect(u8"祖国统一");
    thePainter.drawText(myWinRect.width()*0.3f, myRect.height() + myOffset, u8"祖国统一");

完成代码代码如下:

CWidget.h

#pragma once
#include <QtWidgets/QWidget>
#include "ui_CWidget.h"
#include <QString>
#include <QDebug>
#include <QPainter>
#include <QTimer>
#include <QPaintEvent>
#include <QFontMetrics>
#include <QPaintEvent>
class CWidget : public QWidget
    Q_OBJECT
public:
    CWidget(QWidget *parent = Q_NULLPTR);
    ~CWidget();
    void drawText(QPainter& thePainter);
    void drawText();
protected:
    void paintEvent(QPaintEvent *event)override;
    bool eventFilter(QObject *watched, QEvent *event);
protected slots:
    void slotTimerOut();
private:
    Ui::CWidgetClass ui;
    QPainter m_painter;
    QTimer* m_pTimer;

CWidget.cpp

#include "CWidget.h"
CWidget::CWidget(QWidget *parent)
    : QWidget(parent)
    , m_pTimer(nullptr)
    ui.setupUi(this);
    m_pTimer = new QTimer(this);
    m_pTimer->start(1000);
    ui.widget->installEventFilter(this);
    connect(m_pTimer, &QTimer::timeout, this, &CWidget::slotTimerOut);
CWidget::~CWidget()
    m_pTimer->stop();
    delete m_pTimer;
void CWidget::drawText(QPainter& thePainter)
    thePainter.setPen(QColor::fromRgb(253, 210, 8));
    QFont myFont = thePainter.font();
    myFont.setPointSize(10);
    myFont.setBold(true);
    QFontMetrics myFontMetrics(myFont);
    QRect myWinRect = rect();
    int width = myWinRect.width();
    int height = myWinRect.height();
    int myOffset = myFont.pointSize();
    thePainter.setFont(myFont);
    QRect myRect = myFontMetrics.boundingRect(u8"华夏民族");
/*    thePainter.drawText(myWinRect.width()*0.5f, myRect.height()*15, u8"H/TJW-504A型");*/
    thePainter.drawText(50, 50, u8"华夏民族");
void CWidget::drawText()
    QPainter thePainter(ui.widget);
    thePainter.setPen(QColor::fromRgb(253, 210, 8));
    QFont myFont = thePainter.font();
    myFont.setPointSize(10);
    myFont.setBold(true);
    QFontMetrics myFontMetrics(myFont);
    QRect myWinRect = rect();
    int width = myWinRect.width();
    int height = myWinRect.height();
    int myOffset = myFont.pointSize();
    thePainter.setFont(myFont);
    QRect myRect = myFontMetrics.boundingRect(u8"祖国统一");
    thePainter.drawText(myWinRect.width()*0.3f, myRect.height() + myOffset, u8"祖国统一");
    qDebug() << "222";
void CWidget::paintEvent(QPaintEvent *event)
    QPainter thePainter(this);
    drawText(thePainter);
    event->accept();
bool CWidget::eventFilter(QObject *watched, QEvent *event)
    if (watched == ui.widget && event->type() == QEvent::Paint)
        drawText();
        qDebug() << "33";
        return true;
    return QWidget::eventFilter(watched, event);
void CWidget::slotTimerOut()
    update();
    int i = 0;
    qDebug() << u8"---" << i;