The code works but I get the following warning message while I press the mouse while it is executing: ??
代码可以工作,但是当我按鼠标时,我得到了以下警告信息:??
QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::setPen: Painter not active
QPainter::drawRects: Painter not active
added modification further down
添加修改进一步下降
#include <QTextEdit>
class QTextEditEnter : public QTextEdit
{
Q_OBJECT
public:
QTextEditEnter(QWidget *_parent);
protected:
virtual void paintEvent(QPaintEvent *_event);
void mousePressEvent(QMouseEvent *evt);
int m_color;
void mouseDoubleClickEvent(QMouseEvent *e);
signals:
void signalPressEnter();
};
#include "qtexteditenter.h"
#include <qpainter.h>
#include <QMouseEvent>
QTextEditEnter::QTextEditEnter(QWidget *_parent) :
QTextEdit(_parent)
{
this->setFrameStyle(QFrame::Sunken);
m_color = 0;
setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
}
void QTextEditEnter::paintEvent(QPaintEvent *_event)
{
QPainter pnt( viewport() );
pnt.setPen( QColor( 255, 0, 0, 0xff ));
pnt.drawRect( 0, 0, width()-1, height()-1);
}
void QTextEditEnter::mousePressEvent(QMouseEvent *e)
{
QPainter p(this->viewport());
p.setPen(QColor(0,0,0,0xff));
p.drawRect(this->viewport()->rect());
p.begin(this);
switch(m_color){
case 0:
p.setPen(Qt::red);
break;
case 1:
p.setPen(Qt::green);
break;
}
p.drawEllipse(e->pos(),2,2);
p.end();
}
void QTextEditEnter::mouseDoubleClickEvent(QMouseEvent *e)
{
m_color++;
if (m_color > 1) m_color = 0;
}
------------------- Modified ----------------
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -修改
void QTextEditEnter::paintEvent(QPaintEvent *_event)
{
if(1){
QPainter pnt( this->viewport());
pnt.setPen( QColor( 0xff, 0, 0, 0xff ));
pnt.drawRect( 0, 0, width()-1, height()-1);
pnt.setPen( QColor( 0, 0xff, 0, 0xff ));
pnt.drawRect( 10, 10, width()-20, height()-20);
}
if(flagModify == 1){
QPainter p(this->viewport());
switch(m_color){
case 0:
p.setPen(Qt::red);
break;
case 1:
p.setPen(Qt::green);
break;
}
p.begin(this);
p.drawEllipse(x, y, 2, 2);
p.end();
flagModify = 0;
}
}
void QTextEditEnter::mousePressEvent(QMouseEvent *e)
{
x = e->pos().x();
y = e->pos().y();
flagModify = 1;
this->update(this->rect());
}
2 个解决方案
#1
6
You get the message because painting to the widget's paint device is only allowed inside the paint event. If you use it outside the paint event, there's no guarantee it will work. So, instead of painting inside the mousePressEvent
, set a state variable (e.g. isPressed
) and call update
, passing the widget's rect. This will trigger the paint event, where you can check the state variable and paint the widget accordingly.
你得到消息,因为绘画到小部件的油漆设备只允许在油漆事件中。如果你在油漆事件之外使用它,就不能保证它会起作用。因此,不要在mousePressEvent中进行绘画,设置一个状态变量(例如isPressed)和调用update,传递小部件的rect,这会触发paint事件,在那里您可以检查状态变量并相应地绘制小部件。
#2
0
In your example do not call p.begin(this); inside
在您的示例中,不要调用.begin(this);内部
void QTextEditEnter::paintEvent(QPaintEvent *_event) {...}
if you want to get rid of the warning message.
如果你想摆脱警告信息。
#1
6
You get the message because painting to the widget's paint device is only allowed inside the paint event. If you use it outside the paint event, there's no guarantee it will work. So, instead of painting inside the mousePressEvent
, set a state variable (e.g. isPressed
) and call update
, passing the widget's rect. This will trigger the paint event, where you can check the state variable and paint the widget accordingly.
你得到消息,因为绘画到小部件的油漆设备只允许在油漆事件中。如果你在油漆事件之外使用它,就不能保证它会起作用。因此,不要在mousePressEvent中进行绘画,设置一个状态变量(例如isPressed)和调用update,传递小部件的rect,这会触发paint事件,在那里您可以检查状态变量并相应地绘制小部件。
#2
0
In your example do not call p.begin(this); inside
在您的示例中,不要调用.begin(this);内部
void QTextEditEnter::paintEvent(QPaintEvent *_event) {...}
if you want to get rid of the warning message.
如果你想摆脱警告信息。