GridLayout 中的 QGraphicsView 大小

发布于 2024-12-11 14:39:47 字数 815 浏览 0 评论 0原文

我发现了这个: 获取 QGraphicsView 的大小

但我不能弄清楚“将我的初始化代码移至 showEvent”是什么意思,我无法对此答案发表评论。

我想调整 QPixmap 的大小,以便它适合我的 QGraphicsView。我已将图形视图放置在设计器中,并为主窗口设置 GridLayout。在 MainWindow 构造函数中,我编写了以下代码: ui->setupUi(this);

// Get GView size
g_sizeX = ui->mapView->width();
g_sizeY = ui->mapView->height();
// Init  scene
scene = new QGraphicsScene(this);
// Init MAP pixmap and add it to scene
mapImage = new QPixmap(":/Map/europe.jpg");
QPixmap newmapImage = mapImage->scaled(g_sizeX, g_sizeY);
scene->addPixmap(newmapImage);

// Display scene in gview.
ui->mapView->setScene(scene);

但我总是得到 100x30 的尺寸。如果我破坏 gridLayout,我会得到正确的大小。 那么,我应该如何处理这个问题呢?

谢谢。

I have found this: Getting the size of a QGraphicsView

But I can't figure out what does it mean to "move my initialization code to showEvent" and I can't comment on that answer.

I am want to resize a QPixmap so it could fit my QGraphicsView. I've placed my graphicsview in Designer and set GridLayout for my main window. In a MainWindow constructor I have written the following code:
ui->setupUi(this);

// Get GView size
g_sizeX = ui->mapView->width();
g_sizeY = ui->mapView->height();
// Init  scene
scene = new QGraphicsScene(this);
// Init MAP pixmap and add it to scene
mapImage = new QPixmap(":/Map/europe.jpg");
QPixmap newmapImage = mapImage->scaled(g_sizeX, g_sizeY);
scene->addPixmap(newmapImage);

// Display scene in gview.
ui->mapView->setScene(scene);

But I always get size of 100x30. If I break the gridLayout, I get the correct size.
So, how should I deal with this?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

财迷小姐 2024-12-18 14:39:47

显示小部件后,QGraphicsView 将由 QGridLayout 调整大小,并且稍后在窗口本身调整大小时也可以调整大小。

因此,您应该通过 QResizeEvent 来更改像素图的大小,方法是通过子类化 QGraphicsView 来重新定义 resizeEvent(),然后提升将视图对象添加到设计器中的新类中,以使用它来代替 QGraphicsView,或者将 MainWindow 对象安装为事件过滤器,以便视图处理调整大小事件从MainWindow::eventFilter 函数。

您可能不想更改场景中的像素图大小,而是调整视图矩阵,以便您的 QGraphicsPixmapItem 完全适合视图,其中 QGraphicsView::fitInView

例如:

/* QGraphicsPixmapItem *pixmapItem; as a MainWindow member */
pixmapItem = scene->addPixmap(newmapImage);

/* Either always disable or enable the scrollbars (see fitInView doc) */
ui->mapView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->mapView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

ui->mapView->installEventFilter(this);
...
bool MainWindow::eventFilter(QObject *obj, QEvent *evt) {
     if(obj == ui->mapView && evt->type() == QEvent::Resize) { 
          ui->mapView->fitInView(pixmapItem, Qt::KeepAspectRatioByExpanding);
     }
     // Call the base class implementation 
     return QMainWindow::eventFilter(obj, evt);
}

The QGraphicsView will be resized by the QGridLayout after the widget is shown, and can be also resized later when the window is itself resized.

So you should change the size of the pixmap as a result of a QResizeEvent, either by subclassing QGraphicsView to redefine resizeEvent(), and then promoting your view object to your new class in the designer to use it instead of QGraphicsView, or by installing your MainWindow object as an event filter for the view to handle to the resize event from the MainWindow::eventFilter function.

You probably don't want to change the pixmap size in the scene, but rather adjust the view matrix so that your QGraphicsPixmapItem fits perfectly inside the view, with QGraphicsView::fitInView.

For example:

/* QGraphicsPixmapItem *pixmapItem; as a MainWindow member */
pixmapItem = scene->addPixmap(newmapImage);

/* Either always disable or enable the scrollbars (see fitInView doc) */
ui->mapView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->mapView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

ui->mapView->installEventFilter(this);
...
bool MainWindow::eventFilter(QObject *obj, QEvent *evt) {
     if(obj == ui->mapView && evt->type() == QEvent::Resize) { 
          ui->mapView->fitInView(pixmapItem, Qt::KeepAspectRatioByExpanding);
     }
     // Call the base class implementation 
     return QMainWindow::eventFilter(obj, evt);
}
情丝乱 2024-12-18 14:39:47

我相信正在发生的事情是 Qt 仅在首次显示小部件时应用布局并设置小部件大小。

一种工作方法是重写 QWidget::showEvent(),并将调整大小的代码放在那里。

然而,一种通常在构造函数中使用的更简单的方法是向小部件询问其 sizeHint(),而不是询问其尚未布局的大小。

在您的情况下,这意味着将两行代码更改为:

g_sizeX = ui->mapView->sizeHint().width();
g_sizeY = ui->mapView->sizeHint().height();

如果您的布局不太复杂,并且您没有覆盖默认大小策略,这很可能会为您解决问题。

I believe that what is happening is that Qt only applies layouts and sets widget sizes when the widget is first displayed.

One way to work that is to override QWidget::showEvent(), and put your sizing code in there.

However, one simpler way, that often works in constructors, is to ask the widget for its sizeHint(), rather than for its not-yet-layed-out size.

In your case, that would mean changing two lines of code to:

g_sizeX = ui->mapView->sizeHint().width();
g_sizeY = ui->mapView->sizeHint().height();

If your layout isn't too complicated, and if you haven't overridden the default size policies, this may well fix things for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文