C++ 在QGraphicsView中滚动
我在QGraphicsView中滚动时遇到问题 我已在QGraphicsView子类中设置了scene rect:C++ 在QGraphicsView中滚动,c++,qt,qgraphicsview,C++,Qt,Qgraphicsview,我在QGraphicsView中滚动时遇到问题 我已在QGraphicsView子类中设置了scene rect: MyQGraphicsView::MyQGraphicsView{ setSceneRect(0,0,2000,2000) } 和事件: void MyQGraphicsView::paintEvent(QPaintEvent *event){ qDebug()<<"Paint event"; QPainter painter(viewport
MyQGraphicsView::MyQGraphicsView{
setSceneRect(0,0,2000,2000)
}
和事件:
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));
}
void MyQGraphicsView::paintEvent(QPaintEvent*event){
qDebug()QGraphicsView
继承了QAbstractScrollArea
。因此,它的内容显示在内部小部件中,可以使用viewport()
获得。如果您想绘制某个内容并能够滚动它,则需要将事件过滤器附加到viewport小部件并处理它的paintEvent,而不是view的事件
但是你不应该为QGraphicsView
这样做。你正试图做一些非常错误的事情。你不应该仅仅为了画一些东西而重新实现QGraphicsView::paintEvent
。它完全贬低了它的优点。你需要使用qgraphicscene
来向视图中添加一些东西