【发布时间】:2015-01-18 21:32:10
【问题描述】:
我写这篇文章是因为我的选择区域有问题。
如果您单击 Windows 桌面,然后拖动鼠标,您将看到选择区域。我正在尝试实现通常类似的事情。
您有什么想法,如何实现?
感谢您的任何建议。
【问题讨论】:
标签: c++ qt graphics selection area
我写这篇文章是因为我的选择区域有问题。
如果您单击 Windows 桌面,然后拖动鼠标,您将看到选择区域。我正在尝试实现通常类似的事情。
您有什么想法,如何实现?
感谢您的任何建议。
【问题讨论】:
标签: c++ qt graphics selection area
它被称为"rubber band"。您需要找到一个使用 QRubberBand 类的示例。我无法从相对较大的项目中分离出一个小样本,但总的来说它不是很复杂,而且很简单。
【讨论】:
您可以使用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()))
【讨论】: