今天在使用QPainter的时候QPainter不起作用,同时出现了如下错误:

QWidget::paintEngine: Should no longer be called

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

QPainter::setPen: Painter not active

QPainter::drawPoints: Painter not active

我的部分代码如下:

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);painter = new QPainter(this);index = 0;
}Widget::~Widget()
{delete ui;
}void Widget::mouseMoveEvent(QMouseEvent *event)
{//qDebug() << event->pos();
}void Widget::mousePressEvent(QMouseEvent *event)
{painter->setPen(QPen(Qt::red, 2, Qt::SolidLine));painter->drawPoint(50 + index, 50 + index);
}

经过谷歌知道了错误的原因:

All painting on a widget must happen in the paintEvent() function, and you are trying to paint outside of it - that won't work.

You must either find a way to put all your drawing calls inside the paintEvent() function, or draw on a buffer, for example a QPixmap and then draw that pixmap onto the widget in the paintEvent() When you draw on a buffer you can draw from everywhere, the limitation is only for widget drawing. For pixmaps you (usually) must draw from the main thread, if you want to draw from another thread, use QImage instead.

于是将代码修改为:

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);index = 0;
}Widget::~Widget()
{delete ui;
}void Widget::mouseMoveEvent(QMouseEvent *event)
{//qDebug() << event->pos();
}void Widget::mousePressEvent(QMouseEvent *event)
{
//    painter->drawPoint(50 + index, 50 + index);update();
}void Widget::paintEvent(QPaintEvent *event)
{painter = new QPainter(this);painter->setPen(QPen(Qt::red, 2, Qt::SolidLine));painter->drawPoint(50 + index, 50 + index);index += 50;
}

就可以正常工作了,注意QPainter的内存分配也必须在 Widget :: paintEvent ( QPaintEvent *event)函数中进行。

Unable to paint on Qt Widget, shows error “paintEngine: Should no longer be called”相关推荐

  1. Qt: QWidget::paintEngine: Should no longer be called

    目录 由来 注意下面我的创建过程: 下面是解决方法: 产生这个错误的原因: 由来 今天在QT Creater使用QPainter的时候出现了这个错误,在引用输出窗口还有这么一些错误 之前在使用其他类的 ...

  2. Qt5.1.1 + VS2010安装错误(Unable to find a Qt build)

    本文旨在这对Qt+Vs安装过程中,遇到Unable to find a Qt build问题的解决办法. 首先,介绍安装过程, 安装过程可以分为三步. 第一步:VS2010旗舰版,安装过程可以默认或者 ...

  3. 编译Qt“NMAKE:fatal error U1077”错误的解决方法

    编译Qt"NMAKE:fatal error U1077"错误的解决方法 分类: 错误与解决方法 2013-03-18 20:17 1775人阅读 评论(0) 收藏 举报 Qtfa ...

  4. Qt Creator创建基于Qt Widget的应用程序

    Qt Creator创建基于Qt Widget的应用程序 创建基于Qt Widget的应用程序 创建文本查找器项目 设计用户界 头文件 源文件 创建资源文件 编译并运行程序 创建基于Qt Widget ...

  5. BUG: Setup Was Unable to Create a DCOM User Account Error Message in Visual Studio 6.0

    今天在安装VS 6.0时,遇见这个问题:Setup was unable to create a DCOM user account in order to register <path> ...

  6. Qt widget事件传递顺序以及监听特定控件是否接收某个事件

    序言 其实刚开始接触的时候,是没注意到Qt widget中事件传递顺序,当时也才接触到Qt,也就没怎么去梳理,如今把我当前所掌握的先整理如下,后续如果有更深的理解,会不断完善这篇博客.(代码不是比较规 ...

  7. QT Widget.cpp重新命名、报错“xxxx“ was not declared in this scope

    这里写自定义目录标题 #QT Widget.cpp重新命名.报错"xxxx" was not declared in this scope 将刚创建的新文件 widget.cpp ...

  8. qt 5.0.2 vs addin 安装错误 Unable to find a Qt build!

    本人安装的版本 qt-windows-opensource-5.0.2-msvc2010_32_opengl-x86-offline qt-vs-addin-1.2.1-opensource 当点击q ...

  9. windos下Qt Creator CMake error: Generator: execution of make failed. Make command was: jom /nologo al

    本文链接 github.com/BeyondXinXin windos下Qt Creator CMake error: Generator: execution of make failed. Mak ...

最新文章

  1. 使用 Python 和 OpenCV 进行数据增广
  2. maven-resources-plugin include 通配符
  3. [leetcode] 704.二分查找
  4. C++转换构造函数和类型转换函数
  5. java自定义线程_Java自定义线程池详解
  6. Bootstrap表格内容垂直水平居中
  7. tomcat中间件的默认端口号_Tomcat下载安装及配置
  8. jq跨域代理_jQuery中的跨域问题
  9. android 字符串格式化
  10. aiohttp保存MySQL_python链家网高并发异步爬虫asyncio+aiohttp+aiomysql异步存入数据
  11. VS2015搭建汇编语言环境和代码高亮插件AsmDude
  12. 灯亮怎么办_发动机故障灯亮了怎么办?看了这些,也许根本不用去4S店
  13. 致远项目管理SPM系统五大技术平台层-CMP能力概述
  14. 这应该是史上最强的物理学科普(雄文)
  15. 什么是GC,GC是什么意思?为什么要有GC?
  16. 如何查看xshell保存的密码
  17. 解决vmbox虚拟机 能上网,能ping通宿机,但是宿机 ping 不通 虚拟机
  18. 综合布线系统 (布线系统的一种)
  19. 通过扫描二维码进入下载APP的流程
  20. C++程序设计(六)—— 继承和派生

热门文章

  1. 【实战】使用Bert微调完成文本二分类
  2. 论文多次查重会影响重复率吗?
  3. 基于JavaWeb的酒店客房预定管理系统
  4. spring cloud灰度发布快速上下线问题解决
  5. App Icon Gear App 图标制作工具
  6. SLsec题目wp(2)
  7. java程序员更是_2019年 Java程序员面临更加激烈的竞争
  8. 创业团队 磨合 团队稳定 团队成员
  9. 基于JAVA小型健身俱乐部网站计算机毕业设计源码+数据库+lw文档+系统+部署
  10. float,double,decimal的区别于误解