带有特定键盘键的 Qt 橡皮筋选择
qt rubberband selection with specific keyboard key
我有一个QGraphicsView
和一个QGraphicsScene
,我启用了
this->setDragMode(QGraphicsView::RubberBandDrag);
用于橡皮筋选择。但是,在我的应用程序中,您需要按 CTRL 键,然后移动鼠标以开始橡皮筋选择是有意义的。我可以在不制作自己的QRubberBand的情况下完成此操作吗?如果没有,我该如何重新实现它?
如果你有一个包含你的QGraphicsView
和场景的QMainWindow
,一种方法是重载QMainWindow的keyPressEvent
和keyReleaseEvent
方法,如下所示:
void MyMainWindow::keyPressEvent( QKeyEvent * event )
{
if( event->key() == Qt::Key_Control ) {
graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
}
QMainWindow::keyPressEvent(event);
}
void MyMainWindow::keyReleaseEvent( QKeyEvent * event )
{
if( event->key() == Qt::Key_Control ) {
graphicsView->setDragMode(QGraphicsView::NoDrag);
}
QMainWindow::keyReleaseEvent(event);
}
只要按下 CTRL,就会将选择模式设置为 RubberBandDrag
。再次释放键时,拖动模式将设置回默认NoDrag
并且不执行任何选择。在这两种情况下,事件也会转发到 QMainWindow 基类实现,这可能与您相关,也可能与您无关。
埃里克的回答对我来说效果不佳。如果我在仍然拖动的同时释放键,橡皮筋不会清除,并且在屏幕上保持可见,直到下一个选择。
由于 QT 仅在鼠标释放时清除橡皮筋,我的解决方法是在仍处于橡皮筋模式时强制人工鼠标释放事件以正确清除它:
void MyQGraphisView::keyReleaseEvent( QKeyEvent * event )
{
if( event->key() == Qt::Key_Control ) {
if(QApplication::mouseButtons() & Qt::LeftButton)
mouseReleaseEvent(new QMouseEvent(QApplicationStateChangeEvent::MouseButtonRelease, mousePosOnScene, Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
setDragMode(QGraphicsView::NoDrag);
}
QMainWindow::keyReleaseEvent(event);
}
更新:Qt修复了这个错误(https://bugreports.qt.io/browse/QTBUG-65186),并将在5.15中部署
相关文章:
- Qt VTK交互风格的信号到小部件
- QT在错误的班级中寻找空位
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在Qt C++中更改光标
- 如何在MS Visual Studio 2019中运行QT UI
- QT通过C++添加映射QML项目
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用Qt C++计算类似Git的SHA1哈希
- Qt focusObjectChanged example?
- 如何在cpp文件之间切换窗口?在Qt中
- Qt和C++:将QLineEdit添加到QTabWidget中
- 从数据库实时显示QT c++中的数据
- Qt Quick-如何仅从c++代码与qml属性交互
- 无法使用Qt Creator在Windows中构建yaml-cpp
- 如何在Qt中取消捕获字符串
- Qt橡皮筋未被拉
- 带有特定键盘键的 Qt 橡皮筋选择