我正在尝试建立橡皮筋。这是我的代码。
void TOTMain::mousePressEvent(QMouseEvent * event)
{
QPoint origin = event->pos();
_selectionSquare = new QRubberBand(QRubberBand::Rectangle, this);
_selectionSquare->setGeometry(QRect(origin, QSize()));
_selectionSquare->raise();
_selectionSquare->show();
}
void TOTMain::mouseMoveEvent(QMouseEvent *event)
{
QPoint origin = event->pos();
_selectionSquare->setGeometry(QRect(origin, event->pos()).normalized());
}
void TOTMain::mouseReleaseEvent(QMouseEvent *event)
{
_selectionSquare->hide();
// determine selection, for example using QRect::intersects()
// and QRect::contains().
}
问题是没有绘制乐队。我确认它正在构建并且在拖动过程中仍然存在,但是没有渲染。
任何想法表示赞赏。
最佳答案
看看您的mouseMoveEvent
实现...
void TOTMain::mouseMoveEvent(QMouseEvent *event)
{
QPoint origin = event->pos();
_selectionSquare->setGeometry(QRect(origin, event->pos()).normalized());
}
您将橡皮筋的几何形状设置为
QRect(origin, event->pos())
,但是之前已将origin = event->pos()
设置为矩形的左上角和右下角相同。使
origin
成为您类的成员变量变量(未经测试)...class TOTMain: public QWidget {
public:
protected:
virtual void mousePressEvent (QMouseEvent *event) override
{
_origin = event->pos();
_selectionSquare = new QRubberBand(QRubberBand::Rectangle, this);
_selectionSquare->setGeometry(QRect(_origin, QSize()));
_selectionSquare->raise();
_selectionSquare->setStyleSheet("{ background-color : red; }");
_selectionSquare->show();
}
virtual void mouseMoveEvent (QMouseEvent *event) override
{
_selectionSquare->setGeometry(QRect(_origin, event->pos()).normalized());
}
virtual void mouseReleaseEvent (QMouseEvent *event) override
{
_selectionSquare->hide();
// determine selection, for example using QRect::intersects()
// and QRect::contains().
}
private:
QPoint _origin;
QRubberBand *_selectionSquare = nullptr;
};
关于c++ - Qt橡皮筋未绘制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58523864/