gpt4 book ai didi

c++ - Qt橡皮筋没有被绘制

转载 作者:行者123 更新时间:2023-11-27 22:33:02 25 4
gpt4 key购买 nike

我正在尝试实现橡皮筋。这是我的代码。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com