QGraphicsView:禁用自动滚动

发布于 2024-10-12 00:04:42 字数 554 浏览 12 评论 0原文

我想要一个永远不会自动滚动的 QGraphicsView。

类似:基本上,我的问题与 http://developer.qt.nokia.com/ 相同forums/viewthread/2220 ,但该帖子没有收到答复。

到目前为止我已经尝试过:

  • 在 showEvent() 和 resizeEvent() 中,我执行 ui->graphicsView->fitInView(...) ,只要项目不超出
  • 我已经设置的 屏幕矩形,它就可以正常工作也尝试操作视图变换,但除了缩放之外,它的系数永远不会改变,所以这也是徒劳的,禁用
  • 滚动条外观也没有帮助,另

请参阅http://doc.qt.io/qt-4.8/qgraphicsview.html

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

See also http://doc.qt.io/qt-4.8/qgraphicsview.html.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

柠檬心 2024-10-19 00:04:42

我的解决方案有点粗略,但我认为它非常直观:如果您不希望 QGraphicsView 滚动您的内容,请覆盖虚拟方法scrollContentsBy。

void QGraphicsViewDerived::scrollContentsBy(int, int)
{
    //don't do anything hah!
}

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.

void QGraphicsViewDerived::scrollContentsBy(int, int)
{
    //don't do anything hah!
}
撞了怀 2024-10-19 00:04:42

我找到了一个解决方案(不要犹豫,发布您的替代方案:)),但我仍然认为这个答案可能会有所帮助,因为我在谷歌和文档上苦苦挣扎了大约 15 个小时。

关键是不仅要调用fitInView(),还要调用setSceneRect()。这对我来说是这样的(用你自己的类名替换 FooBar ):

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);
}

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):

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);
}
饮惑 2024-10-19 00:04:42

当然,使用 setSceneRect() 的解决方案是正确的,但是当有大量遗留代码时,它可能会太昂贵。

我也尝试了scrollContentsBy(),但它影响了我的情况下的场景定位。

所以我得出了以下解决方案:

class GraphicsView : public QGraphicsView
{
protected:
    void wheelEvent(QWheelEvent*) override {}
    void keyPressEvent(QKeyEvent* e) override { QWidget::keyPressEvent(e); }
};

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:

class GraphicsView : public QGraphicsView
{
protected:
    void wheelEvent(QWheelEvent*) override {}
    void keyPressEvent(QKeyEvent* e) override { QWidget::keyPressEvent(e); }
};
薄暮涼年 2024-10-19 00:04:42

我找到了完美的解决方案,通过调用

QGraphicsView::setSceneRect(yourScene->sceneRect()) 

视图的构造函数,自动滚动行为被停止。

I found the perfect solution, by calling

QGraphicsView::setSceneRect(yourScene->sceneRect()) 

in the constructor of your view, the automatic scrolling behavior is stopped.

北座城市 2024-10-19 00:04:42

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.

爱的那么颓废 2024-10-19 00:04:42

如果不需要交互性,另一个简单的解决方案是禁用 QGraphicsView:

view->setEnabled(false);

这也将阻止它滚动,但它也不会再接收鼠标或键盘事件。

Another simple solution, if no interactivity is needed, is to disable the QGraphicsView:

view->setEnabled(false);

This will also prevent it from scrolling, however it also won't receive mouse or keyboard events anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文