gpt4 book ai didi

c++ - 在 QGraphicsView 中滚动

转载 作者:行者123 更新时间:2023-11-30 04:17:21 26 4
gpt4 key购买 nike

我在 QGraphicsView 中滚动有问题。

我在我的 QGraphicsView 子类中设置了场景矩形:

MyQGraphicsView::MyQGraphicsView{
setSceneRect(0,0,2000,2000)
}

并重载了 paintEvent:

void MyQGraphicsView::paintEvent(QPaintEvent *event){
qDebug()<<"Paint event";
QPainter painter(viewport());
painter.setRenderHint(QPainter::Antialiasing);
paint(painter);
}
void MyQGraphicsView::paint(QPainter &painter){
painter.setPen(Qt::NoPen);
painter.fillRect(QRect(0,0,200,200),Qt::gray);
painter.fillRect(QRect(500,500,1000,100),Qt::green);
painter.setPen(QPen(Qt::white,4,Qt::DashLine));
painter.drawLine(QLine(0,35,200,35));
painter.drawLine(QLine(0,165,200,165));
}

当我滚动时,第二个矩形不可见。当我调整窗口大小时。此外,当滚动矩形以有线方式扩展时。

在这种情况下应该如何实现滚动?我发现了几个关于在 QGraphicsView 中滚动的话题,但没有一个能解决我的问题。

最佳答案

QGraphicsView继承QAbstractScrollArea。所以它的内容显示在它的内部小部件中,可以使用 viewport() 获取。如果你想绘制一些东西并能够滚动它,你需要将一个事件过滤器附加到视口(viewport)小部件并处理它的 paintEvent,而不是 View 的事件。

但是您不应该为 QGraphicsView 这样做。你正试图做一些非常错误的事情。你不应该为了画东西而重新实现 QGraphicsView::paintEvent!它完全贬低了它的优势。您需要使用 QGraphicsScene 向 View 添加内容。

关于c++ - 在 QGraphicsView 中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17141987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com