Home > database >  How to make the QGraphicsView size consistent with the size of the image and the main window in Qt?
How to make the QGraphicsView size consistent with the size of the image and the main window in Qt?

Time:11-17

I'm developing a graphic editor with Qt. I'm using a QGraphicsView to display the picture and need to resize it so that it's consistent with the size of the image and the main window.

Now I'm resizing it like this (in the method of the MainWindow class):

    ui->graphicsView->resize(picture->width, picture->height); // ui here is Ui::MainWindow* pointer

I call this method when I open the image. It looks normal for small pictures, but when I open a big one, its size becomes more than the size of the window and I can't scroll it. I can set the size of the graphicsView to the maximum of the picture and the main window sizes, but this doesn't solve the problem, because when I make the main window smaller, the picture will keep its size and will be larger than the window. So, I think I need to resize the image when resizing the main window. I know there is the system of signals and slots in Qt, but I can't find the suitable slot of QMainWindow. So, my question is: how to catch the changing of the size of the main window to resize the graphicsView?

CodePudding user response:

just override resizeEvent() in QMainWindow

const QSize &QResizeEvent::size() const

Returns the new size of the widget.

void MyMainWindow::resizeEvent(QResizeEvent* event)
{
   // ui->graphicsView->resize(event->size());
   QMainWindow::resizeEvent(event);
}
  • Related