Two question about QGraphicsView , Thanks
-
1:Why I have to call setHorizontalScrollBarPolicy(Qt:crollBarAlwaysOff) to disable scrollbar, otherwise they will show even the view equal scene.
2:After call fitInView in resizeEvent, there is always a margin in view. what should I do?
@
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),
ui(new Ui::MainWindow),myView(new View)
{
ui->setupUi(this);this->myView->setFixedSize(400, 300);
this->setCentralWidget(this->myView);
this->myScene = new QGraphicsScene;
this->myScene->setSceneRect(this->myView->rect());
this->myView->setScene(this->myScene);
//why I have to call these function to diable scrollbar,otherwise they will show no matter the view and scene size
this->myView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->myView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);}
void MainWindow::resizeEvent(QResizeEvent *event)
{
//if I call this fitInView, there will be a margin in view. why
this->myView->fitInView(0,0,400,300,Qt::KeepAspectRatio);}
@ -
Hi.
you might try to replace
@this->myView->setFixedSize(400, 300);@with
@this->myView->viewport()->setFixedSize(400, 300);@The QGraphicsView is not the widget that actually contains the scene, because the QGraphicsView also manages the scrollbars. The scene is drawn in the viewport widget, which is a little smaller than the QGraphicsView.
I hope this helps. Let me know if it did not.