问题描述
我想要一个从不自动滚动的 QGraphicsView.
I want to have a QGraphicsView that never scrolls automatically.
类似:基本上,我的问题与 http://developer.qt.nokia 相同.com/forums/viewthread/2220 ,但该线程没有收到答复.
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:
- 在 showEvent() 和 resizeEvent() 中,我执行 ui->graphicsView->fitInView(...),只要项目不超过屏幕矩形,它就可以正常工作
- 我也尝试过操纵视图变换,但除了缩放之外,它的系数永远不会改变,所以这也是徒劳的
- 禁用滚动条外观也无济于事
另见http://doc.qt.io/qt-4.8/qgraphicsview.html.
推荐答案
我找到了一个解决方案(不要犹豫发布您的替代方案 :) ),但我仍然认为这个答案可能会有所帮助,因为我在 google 和文档上苦苦挣扎大约 15 小时.
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.
关键不仅要调用fitInView(),还要调用setSceneRect().这是为我做的(用你自己的类名替换 FooBar):
The key is to not only call fitInView(), but also setSceneRect(). This did it for me (replace FooBar with your own class name):
void FooBar::resizeEvent(QResizeEvent *) {
fitView();
}
void FooBar::showEvent(QShowEvent *) {
fitView();
}
void FooBar::fitView() {
const QRectF rect = QRectF(-0.5,-0.5, 1, 1);
ui->graphicsView->fitInView(rect,
Qt::KeepAspectRatio);
ui->graphicsView->setSceneRect(rect);
}
这篇关于QGraphicsView:禁用自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!