GridLayout 中的 QGraphicsView 大小
我发现了这个: 获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显示小部件后,
QGraphicsView
将由QGridLayout
调整大小,并且稍后在窗口本身调整大小时也可以调整大小。因此,您应该通过
QResizeEvent
来更改像素图的大小,方法是通过子类化QGraphicsView
来重新定义resizeEvent()
,然后提升将视图对象添加到设计器中的新类中,以使用它来代替 QGraphicsView,或者将MainWindow
对象安装为事件过滤器,以便视图处理调整大小事件从MainWindow::eventFilter
函数。您可能不想更改场景中的像素图大小,而是调整视图矩阵,以便您的
QGraphicsPixmapItem
完全适合视图,其中QGraphicsView::fitInView
。例如:
The
QGraphicsView
will be resized by theQGridLayout
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 subclassingQGraphicsView
to redefineresizeEvent()
, and then promoting your view object to your new class in the designer to use it instead ofQGraphicsView
, or by installing yourMainWindow
object as an event filter for the view to handle to the resize event from theMainWindow::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, withQGraphicsView::fitInView
.For example:
我相信正在发生的事情是 Qt 仅在首次显示小部件时应用布局并设置小部件大小。
一种工作方法是重写 QWidget::showEvent(),并将调整大小的代码放在那里。
然而,一种通常在构造函数中使用的更简单的方法是向小部件询问其 sizeHint(),而不是询问其尚未布局的大小。
在您的情况下,这意味着将两行代码更改为:
如果您的布局不太复杂,并且您没有覆盖默认大小策略,这很可能会为您解决问题。
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:
If your layout isn't too complicated, and if you haven't overridden the default size policies, this may well fix things for you.