简体   繁体   English

Qt中如何让QGraphicsView大小与图片大小和主window保持一致?

[英]How to make the QGraphicsView size consistent with the size of the image and the main window in Qt?

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.我正在用 Qt 开发一个图形编辑器。我正在使用 QGraphicsView 来显示图片,需要调整它的大小,使其与图像和主要 window 的大小一致。

Now I'm resizing it like this (in the method of the MainWindow class):现在我像这样调整它的大小(在 MainWindow 类的方法中):

    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.小图看正常,打开大图就比window大了,滚动不起来了。 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.我可以将 graphicsView 的大小设置为图片的最大值和主要的 window 大小,但这并不能解决问题,因为当我将主要的 window 变小时,图片将保持其大小并且会大于window。所以,我想在调整主window大小时需要调整图像大小。我知道Qt中有信号槽系统,但我找不到合适的QMainWindow槽。 So, my question is: how to catch the changing of the size of the main window to resize the graphicsView?所以,我的问题是:如何捕捉主 window 大小的变化来调整 graphicsView 的大小?

just override resizeEvent () in QMainWindow只需重写QMainWindow中的resizeEvent ()

const QSize &QResizeEvent::size() const常量 QSize &QResizeEvent::size() 常量

Returns the new size of the widget.返回小部件的新大小。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM