这个问题已被多次询问,但似乎没有一个对我有效。我试图在QOpenGLWidget
上绘制多个可调整大小/可移动的矩形。我有一个继承自Shape
的课程QLabel
,我已经覆盖了paintEvent
功能。
//local
#include "Shape.h"
Shape::Shape(const QColor& color, uint width, const QRect& rect, QWidget* parent) :
QLabel(parent),
m_color(color),
m_rectangle(rect),
m_width(width)
{
//One time update at creation
update(); //Doesn't work
}
void Shape::draw(const QRect& rect)
{
m_rectangle = rect;
update(); //Doesn't work
}
/*virtual*/ void Shape::paintEvent(QPaintEvent* /*event*/)
{
m_painter.begin(this);
QPen pen(m_color);
pen.setWidth(m_width);
m_painter.setPen(pen);
m_painter.drawRect(m_rectangle);
m_painter.end();
}
我正在类Shape
的mousevents中创建一个GLWidget
对象,并希望看到实时绘制的矩形。它看起来像这样:
/*virtual*/ void GLWidget::mousePressEvent(QMouseEvent* event)
{
m_mousePressed = true;
m_cursorPos = event->pos();
QPoint pos(((double)(m_cursorPos.x())) / width() * m_cols,
((double)(m_cursorPos.y())) / height() * m_rows);
m_selection.setTopLeft(pos);
m_selection.setBottomRight(pos);
m_rectangle.reset(new Shape(Qt::yellow, 2, m_selection, this));
}
/*virtual*/ void GLWidget::mouseMoveEvent(QMouseEvent* event)
{
if(m_mousePressed && event->type() == QEvent::MouseMove)
{
m_selection.setBottomRight(event->pos());
m_rectangle->draw(m_selection);
}
}
/*virtual*/ void GLWidget::mouseReleaseEvent(QMouseEvent* event)
{
m_mousePressed = false;
//Update one last time
m_rectangle->draw(m_selection);
}
我已经尝试调试我可以看到正在创建的对象,但update() or repaint()
无法正常工作。我错过了与QLabel
事件相关的内容吗?