首页 > 其他分享 >解决QWidget::paintEngine: Should no longer be called QPainter::begin: Paint device returned engine

解决QWidget::paintEngine: Should no longer be called QPainter::begin: Paint device returned engine

时间:2023-10-09 14:12:07浏览次数:34  
标签:engine begin thePainter longer myWinRect drawText CWidget myFont event

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

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类然后重写绘图事件,最后在绘图事件函数内创建画家即可。(构造函数内不要创建画家)

.h

protected:
    void paintEvent(QPaintEvent *event)override;

.cpp

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;
}

 

 

 

转 : https://blog.csdn.net/weixin_45151866/article/details/131977177

https://blog.csdn.net/xuancailinggan/article/details/50603141

 

标签:engine,begin,thePainter,longer,myWinRect,drawText,CWidget,myFont,event
From: https://www.cnblogs.com/fps2tao/p/17751583.html

相关文章

  • AtCoder Beginner Contest 323 (ABC 323) D、E、F 题解
    AtCoderBeginnerContest323(ABC323)D、E、F题解D题目大意给\(n\)种数\(s_i\),每一种数有\(c_i\)个,每次可以把两个相同的数合并为一个数,问最后会剩下多少数?分析对于每一个数\(s_i\),它最多被分解\(log_2c_i\)次,并且合并出来最大的数的大小小于\(s_i\timesc_i......
  • AtCoder Beginner Contest 323
    E-Playlist首先需要算出第x+0.5秒后,第一首歌播放的概率1.要在x+0.5秒后播放第一首,需要在x,x-1,x-2,...,x-t[1]+1,时就要开始播放第一首,并且概率是1/n,概率之和除以n2.概率dp,dp[i]表示播放i的概率,那么可以转换成,dp[i]+=dp[i-j]/n%mod(i>=t[j])3.答案就是x,x-1,...,x-t[1]+1概率之和......
  • UNIQUE VISION Programming Contest 2023 Autumn(AtCoder Beginner Contest 323)
    UNIQUEVISIONProgrammingContest2023Autumn(AtCoderBeginnerContest323)A.WeakBeats解题思路:按题意模拟即可。代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;voidsolve(){strings;cin>>s;intn=s.size();......
  • AtCoder Beginner Contest 323
    有的人边上课边打abcA-WeakBeats(abc323A)题目大意给定一个\(01\)字符串,问偶数位(从\(1\)开始)是否全为\(0\)。解题思路遍历判断即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL=longlong;intmain(void){ios::sync_with_stdio......
  • TDengine OSS 与 qStudio 实现无缝协同,革新数据分析和管理方式
    在数字化转型如火如荼的当下,海量爆发的时序数据处理成为转型成功的关键因素之一。为了帮助社区用户更好地进行数据分析和管理,丰富可视化解决方案的多样性,我们将开源的时序数据库(TimeSeriesDatabase)TDengineOSS与开源的数据库分析工具进行了集成,相信这对终极开源工具一定能帮助......
  • AtCoder Beginner Contest 288 Ex A Nameless Counting Problem
    洛谷传送门AtCoder传送门考虑到规定单调不降比较难搞。先设\(g_t\)为长度为\(t\)的满足条件的序列个数(可重且有顺序)。求这个可以设个dp,\(f_{d,i}\)表示考虑到从高到低第\(d\)位,当前\(t\)个数中有\(i\)个仍然顶上界,并且之前的位都满足异或分别等于\(X\)的限制。......
  • AtCoder Beginner Contest 178 E
    AtCoderBeginnerContest178EE-DistMax曼哈顿距离最大点对\(ans=max(|x_i-x_j|+|y_i-y_j|)\)考虑去绝对值,4种情况。sort一下取max即可。#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=2e5+10;intx[N],y[N];intp[4][N];......
  • AtCoder Beginner Contest 322
    A-FirstABC2解题思路签到Code#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;voidsolve(){ intn; cin>>n; strings; cin>>s; intp=s.find("ABC"); if(p==-1)cout<<p<<'\n&......
  • Begin of PHP
    打开直接就是一份php代码,分析代码发现需要闯关,一共有五关 直接用ai给我翻译一下Level1:用户需要提供名为'key1'和'key2'的GET参数。这两个参数的内容不应相同,但它们的MD5哈希值应该相同。如果条件满足,将设置变量$flag1为True,否则会显示"nope,thisislevel1"。......
  • Begin of HTTP
    打开 GETget方式上传,直接在网址栏上传即可 POST先找到secret,一般藏在网页前端代码里  解码得 得F12,用hackbar上传   Cookie 直接点击cookie,把他改成ctfer即  User-Agent 直接点击user-agent然后改变浏览器直接上传,注意此处不能有中文 ......