Qt 小部件中的选择区域

问题描述 投票:0回答:2

我的选择区域有问题。

如果您单击 Windows 桌面,然后拖动鼠标,您将看到选择区域。我正在努力实现大致相似的目标。

您有什么想法如何实现这一目标吗?

c++ qt graphics selection area
2个回答
7
投票

您可以使用

QRubberBand
。当您想在小部件中实现它时,这是 Qt 文档中的示例:

 void Widget::mousePressEvent(QMouseEvent *event)
 {
     origin = event->pos();
     if (!rubberBand)
         rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
     rubberBand->setGeometry(QRect(origin, QSize()));
     rubberBand->show();
 }

 void Widget::mouseMoveEvent(QMouseEvent *event)
 {
     rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
 }

 void Widget::mouseReleaseEvent(QMouseEvent *event)
 {
     rubberBand->hide();
     // determine selection, for example using QRect::intersects()
     // and QRect::contains().
 }

如果您在其他类中实现它并希望在小部件中显示,您应该小心坐标系。这是因为

event->pos()
与您的小部件位于不同的坐标系中,因此您应该使用 :
 而不是 
event->pos()

myWidget->mapFromGlobal(this->mapToGlobal(event->pos()))


3
投票

它被称为“橡皮筋”。您需要找到使用 QRubberBand 类的示例。我无法将小样本与相对较大的项目分开,但总的来说,它不是很复杂,而且很简单。

© www.soinside.com 2019 - 2024. All rights reserved.