QGraphicsView:禁用自动滚动
我想要一个永远不会自动滚动的 QGraphicsView。
类似:基本上,我的问题与 http://developer.qt.nokia.com/ 相同forums/viewthread/2220 ,但该帖子没有收到答复。
到目前为止我已经尝试过:
- 在 showEvent() 和 resizeEvent() 中,我执行 ui->graphicsView->fitInView(...) ,只要项目不超出
- 我已经设置的 屏幕矩形,它就可以正常工作也尝试操作视图变换,但除了缩放之外,它的系数永远不会改变,所以这也是徒劳的,禁用
- 滚动条外观也没有帮助,另
I want to have a QGraphicsView that never scrolls automatically.
Similar: Basically, my question is identical to http://developer.qt.nokia.com/forums/viewthread/2220 , but that thread didn't receive an answer.
What I have tried so far:
- Within showEvent() and resizeEvent(), I do ui->graphicsView->fitInView(...), which works fine as long as items do not overshoot the screen-rectangle
- I've also tried manipulating the view transform, but apart from scaling it's coefficients never change, so this was fruitless, too
- Diabling scroll bar appearance does not help, too
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我的解决方案有点粗略,但我认为它非常直观:如果您不希望 QGraphicsView 滚动您的内容,请覆盖虚拟方法scrollContentsBy。
My solution is a bit sketchy but I think it's pretty intuitive: If you don't want the QGraphicsView to ever scroll your stuff, override the virtual method scrollContentsBy.
我找到了一个解决方案(不要犹豫,发布您的替代方案:)),但我仍然认为这个答案可能会有所帮助,因为我在谷歌和文档上苦苦挣扎了大约 15 个小时。
关键是不仅要调用fitInView(),还要调用setSceneRect()。这对我来说是这样的(用你自己的类名替换 FooBar ):
I found a solution (don't hesitate to post your alternatives :) ), still I thought this answer might be helpful as I struggled on google and documentations for like 15 hours.
The key is to not only call fitInView(), but also setSceneRect(). This did it for me (replace FooBar with your own class name):
当然,使用 setSceneRect() 的解决方案是正确的,但是当有大量遗留代码时,它可能会太昂贵。
我也尝试了scrollContentsBy(),但它影响了我的情况下的场景定位。
所以我得出了以下解决方案:
Sure, the solution with setSceneRect() is right, but when there is a lot of legacy code, it can be too expensive.
I tried also scrollContentsBy(), but it affected scene positioning in my case.
So I came to the following solution:
我找到了完美的解决方案,通过调用
视图的构造函数,自动滚动行为被停止。
I found the perfect solution, by calling
in the constructor of your view, the automatic scrolling behavior is stopped.
view->setDragMode(QGraphicsView::NoDrag);
为我成功了。
anonvt 的解决方案也有效,只是它会干扰场景的刷新。
view->setDragMode(QGraphicsView::NoDrag);
did the trick for me.
The solution from anonvt also worked except that it interfered with the refreshing of the scene.
如果不需要交互性,另一个简单的解决方案是禁用 QGraphicsView:
这也将阻止它滚动,但它也不会再接收鼠标或键盘事件。
Another simple solution, if no interactivity is needed, is to disable the QGraphicsView:
This will also prevent it from scrolling, however it also won't receive mouse or keyboard events anymore.