Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QLabel paintEvent()在更新()时未触发_C++_Qt_Qlabel - Fatal编程技术网

C++ QLabel paintEvent()在更新()时未触发

C++ QLabel paintEvent()在更新()时未触发,c++,qt,qlabel,C++,Qt,Qlabel,这个问题已经被问了很多次,但似乎没有一个适合我。我正在尝试在QOpenGLWidget上绘制多个可调整大小/可移动的矩形。我有一个类Shape,它继承自QLabel,我已经覆盖了paintEvent函数 //local #include "Shape.h" Shape::Shape(const QColor& color, uint width, const QRect& rect, QWidget* parent) : QLabel(parent),

这个问题已经被问了很多次,但似乎没有一个适合我。我正在尝试在
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();
}
我正在类
GLWidget
的mousevents中创建一个
Shape
对象,希望看到实时绘制的矩形。它看起来像这样:

/*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()或repaint()
不起作用。我是否遗漏了与
QLabel
事件相关的内容?

您是否在任何地方设置了标签的大小?我忘了在构造函数中提到尝试了
setMinimumSize(20,20)
,但说没有区别。同样的问题。可能是重复的