C++ 无法在Qt小部件上绘制,显示错误;油漆引擎:不应再称为“油漆引擎”;
我使用Qt Creator创建了一个小部件,它在主窗口中有两个子窗口,还有一些按钮用于加载、保存图像、设置笔宽和颜色以在窗口上绘制。 但当我开始画画时,我会说C++ 无法在Qt小部件上绘制,显示错误;油漆引擎:不应再称为“油漆引擎”;,c++,qt,widget,qpainter,qwindow,C++,Qt,Widget,Qpainter,Qwindow,我使用Qt Creator创建了一个小部件,它在主窗口中有两个子窗口,还有一些按钮用于加载、保存图像、设置笔宽和颜色以在窗口上绘制。 但当我开始画画时,我会说 QWidget::paintEngine: Should no longer be called QPainter::begin: Paint device returned engine == 0, type: 1 QPainter::setPen: Painter not active QPainter::draw
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
有人知道我犯了什么错误吗?我检查了与此主题相关的线程,但找不到合适的解决方案。
我也是C++新手,请帮我找到一个解决方案<
下面是我的代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtCore>
#include <QImage>
#include <QColor>
#include <QPoint>
#include <QtGui>
#include <QPainter>
#include <QMainWindow>
#include <QFileDialog>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
bool isModified() const { return modified; }
QColor penColor() const { return newPenColor; }
int penWidth() const { return newPenWidth; }
protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
private slots:
void on_open_clicked();
void on_save_clicked();
void on_penWidth_clicked();
void on_penColor_clicked();
private:
Ui::Widget *ui;
QImage image;
QPixmap imageobject;
int newPenWidth;
QColor newPenColor;
bool modified;
bool scribbling;
QPoint firstPoint, secondPoint;
void drawFirstPoint(const QPoint);
void drawSecondPoint(const QPoint);
};
#endif // WIDGET_H
#ifndef小部件
#定义小部件
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
名称空间用户界面{
类控件;
}
类Widget:publicqwidget
{
Q_对象
公众:
显式小部件(QWidget*parent=0);
~Widget();
bool isModified()常量{return modified;}
QColor penColor()常量{return newPenColor;}
int penWidth()常量{return newPenWidth;}
受保护的:
无效油漆事件(QPaintEvent*事件);
无效鼠标压力事件(QMouseEvent*事件)Q_DECL_覆盖;
专用插槽:
打开时无效,单击时无效();
单击保存时无效();
单击时的空白宽度();
单击铅笔颜色时无效();
私人:
Ui::Widget*Ui;
图像;
QPixmap图像对象;
int newPenWidth;
QColor-newPenColor;
bool修饰;
涂鸦;
QPoint第一点,第二点;
void drawFirstPoint(const QPoint);
第二点(常数点)无效;
};
#endif//WIDGET\u H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#包括“widget.h”
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
小部件w;
w、 show();
返回a.exec();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QtWidgets>
#ifndef QT_NO_PRINTER
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrintDialog>
#endif
#include <QLabel>
#include <QWidget>
Widget::Widget(QWidget *parent) : QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setAttribute(Qt::WA_StaticContents);
modified = false;
scribbling = false;
newPenWidth = 1;
newPenColor = Qt::blue;
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_open_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("choose"), "", tr("Image(*.png *.jpg *.jpeg *.bmp *.gif)"));
if (QString::compare(filename, QString())!=0)
{
QImage image;
bool valid = image.load(filename);
if (valid)
{
image = image.scaledToWidth(ui->inputWindow->width(), Qt::SmoothTransformation);
ui->inputWindow->setPixmap(QPixmap::fromImage(image));
image = image.scaledToWidth(ui->outputWindow->width(), Qt::SmoothTransformation);
ui->outputWindow->setPixmap(QPixmap::fromImage(image));
}
else
{
//Error handling
}
}
}
void Widget::on_save_clicked()
{
QString filename = QFileDialog::getSaveFileName(this,tr("choose"), "", tr("PNG (*.png);; JPEG (*.jpg *.jpeg);; BMP(*.bmp);; GIF(*.gif)"));
QImage imageobject = image;
imageobject.save(filename);
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QRect dirtyRect = event->rect();
painter.drawImage(dirtyRect, image, dirtyRect);
}
void Widget::mousePressEvent(QMouseEvent *event)
{
scribbling = true;
if (event->button() == Qt::LeftButton && scribbling)
{
firstPoint = event->pos();
drawFirstPoint(firstPoint);
}
else if (event->button() == Qt::RightButton && scribbling) {
secondPoint = event->pos();
drawSecondPoint(secondPoint);
}
}
void Widget::drawFirstPoint(const QPoint)
{
QPainter painter(this);
painter.setPen(QPen(newPenColor, newPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.drawPoint(firstPoint);
modified = true;
int rad = (newPenWidth / 2) + 2;
update(QRect(firstPoint, firstPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
}
void Widget::drawSecondPoint(const QPoint)
{
QPainter painter(this);
painter.setPen(QPen(newPenColor, newPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.drawPoint(secondPoint);
modified = true;
int rad = (newPenWidth / 2) + 2;
update(QRect(secondPoint, secondPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
}
void Widget::on_penWidth_clicked()
{
bool ok;
int newWidth = QInputDialog::getInt(this, tr("Scribble"),
tr("Select pen width:"),
this->penWidth(), 1, 50, 1, &ok);
if (ok)
newPenWidth = newWidth;
}
void Widget::on_penColor_clicked()
{
QColor newColor = QColorDialog::getColor(this->penColor());
if (newColor.isValid())
newPenColor = newColor;
}
#包括“widget.h”
#包括“ui_widget.h”
#包括
#ifndef QT\u无\u打印机
#包括
#包括
#恩迪夫
#包括
#包括
Widget::Widget(QWidget*parent):QWidget(parent),
ui(新ui::小部件)
{
用户界面->设置用户界面(此);
setAttribute(Qt::WA_StaticContents);
修改=假;
涂鸦=假;
newPenWidth=1;
newPenColor=Qt::蓝色;
}
小部件::~Widget()
{
删除用户界面;
}
void小部件::打开时单击()
{
QString filename=QFileDialog::getOpenFileName(这个,tr(“选择”),“”,tr(“图像(*.png*.jpg*.jpeg*.bmp*.gif)”);
如果(QString::compare(文件名,QString())!=0)
{
图像;
bool valid=image.load(文件名);
如果(有效)
{
image=image.scaledToWidth(ui->inputWindow->width(),Qt::SmoothTransformation);
ui->inputWindow->setPixmap(QPixmap::fromImage(图像));
image=image.scaledToWidth(ui->outputWindow->width(),Qt::SmoothTransformation);
ui->outputWindow->setPixmap(QPixmap::fromImage(image));
}
其他的
{
//错误处理
}
}
}
void小部件::在保存时单击()
{
QString filename=QFileDialog::getSaveFileName(this,tr(“choose”),“”,tr(“PNG(*.PNG);;JPEG(*.jpg*.JPEG);;BMP(*.BMP);;GIF(*.GIF));
QImage imageobject=图像;
保存(文件名);
}
void小部件::paintEvent(QPaintEvent*事件)
{
油漆工(本);
QRect dirtyRect=event->rect();
绘画图像(dirtyRect,image,dirtyRect);
}
void小部件::MousePresseEvent(QMouseEvent*事件)
{
涂鸦=正确;
if(event->button()==Qt::LeftButton&&scribbling)
{
firstPoint=事件->位置();
drawFirstPoint(第一点);
}
else if(event->button()==Qt::RightButton&&scribbling){
第二点=事件->位置();
第二点(第二点);
}
}
void小部件::drawFirstPoint(常量QPoint)
{
油漆工(本);
painter.setPen(QPen)(新铅笔颜色、新铅笔宽度、Qt::实线、Qt::圆顶、,
Qt::RoundJoin);
绘制点(第一点);
修改=真;
int rad=(newPenWidth/2)+2;
更新(QRect(firstPoint,firstPoint).normalized().adjusted(-rad,-rad,+rad,+rad,+rad));
}
void小部件::drawSecondPoint(常量QPoint)
{
油漆工(本);
painter.setPen(QPen)(新铅笔颜色、新铅笔宽度、Qt::实线、Qt::圆顶、,
Qt::RoundJoin);
绘制点(第二点);
修改=真;
int rad=(newPenWidth/2)+2;
更新(QRect(secondPoint,secondPoint).normalized().adjusted(-rad,-rad,+rad,+rad,+rad));
}
void小部件::在_penWidth_clicked()上
{
布尔ok;
int newWidth=QInputDialog::getInt(this,tr(“Scribble”),
tr(“选择笔宽:”),
这->笔宽(),1,50,1,&ok);
如果(确定)
newPenWidth=newWidth;
}
void Widget::on_penColor_clicked()
{
QColor newColor=QColorDialog::getColor(this->penColor());
if(newColor.isValid())
newPenColor=newColor;
}
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1300</width>
<height>700</height>
</rect>
</property>
<property name="windowTitle">
<string>Test window</string>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<widget class="QLabel" name="inputWindow">
<property name="geometry">
<rect>
<x>40</x>
<y>120</y>
<width>600</width>
<height>500</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>600</width>
<height>16777215</height>
</size>
</property>
<property name="cursor">
<cursorShape>CrossCursor</cursorShape>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLabel" name="outputWindow">
<property name="geometry">
<rect>
<x>660</x>
<y>120</y>
<width>600</width>
<height>500</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>600</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>170</x>
<y>10</y>
<width>651</width>
<height>31</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="open">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>OPEN</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="save">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>SAVE</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="penWidth">
<property name="text">
<string>Pen Width</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="penColor">
<property name="text">
<string>Pen Color</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_5">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
小装置
真的
0
0
1300
700
测试窗口
Qt::LeftToRight
40
120
600
500
600
16777215
十字光标
文本标签
660
120
600
500
600
16777215
文本标签
170
10
651
31
指针指针指针
打开
指针指针指针
拯救
笔宽
笔色
按钮
小部件上的所有绘制都必须在paintEvent()
函数中进行,并且您试图在其外部进行绘制-这不起作用
您必须找到一种方法将所有绘图调用放入paintEvent()
函数中,或者使用buff进行绘图