I have created a main window with a QGraphicsView and worked it such that when the window is resized, the scene will remain in the window and scale itself to always fit within the QGraphicsView no matter what size it is. I used the following method to do this:
setupUi(this);
database = db;
cview->installEventFilter(this);//set event filter
setCentralWidget(cview);
populateScene(); //put some junk in the scene for testing
cview->setScene(cscene);
createDockWindows();
//add dock widget to the side
}
//process resize event of needed widget
if (o
== cview
&& e
->type
() == QEvent::Resize) { //rescale the scene to fit the view
cscene->views().first()->fitInView(cview->sceneRect().toRect(),Qt::KeepAspectRatio);
return true;
}
}
WindowScene::WindowScene(QWidget *parent) : QMainWindow(parent) {
setupUi(this);
database = db;
cview = new QGraphicsView;
cview->installEventFilter(this);//set event filter
setCentralWidget(cview);
cscene = new QGraphicsScene;
populateScene(); //put some junk in the scene for testing
cview->setScene(cscene);
createDockWindows();
//add dock widget to the side
}
bool WindowScene::eventFilter(QObject *o, QEvent *e) {
//process resize event of needed widget
if (o == cview && e->type() == QEvent::Resize) {
//rescale the scene to fit the view
cscene->views().first()->fitInView(cview->sceneRect().toRect(),Qt::KeepAspectRatio);
return true;
}
return QMainWindow::eventFilter(o, e);
}
To copy to clipboard, switch view to plain text mode
This seems to work for the most part, but there is a strange issue where the scene will not show up when I drag the window in certain ways:
- If I expand the window to the side only, then the scene scaling works perfect
- If I expand the window to up and down only then the scene scaling works perfect.
- If I make the window smaller in the vertical and horizontal directions at the same time, the scene scales down.
- If I make the window larger vertically and horizontally at the same time, then the scene moves, centered with the window, but the viewport does not expand and the scaling stops short of where it should in order to fill the viewable area.
What might be causing this issue only when enlarging the window in both directions at same time? I have attached pictures so you can see what it is doing
Bookmarks