QMainWindow - QGraphicsView 调整大小问题

发布于 2024-11-16 17:00:06 字数 596 浏览 2 评论 0原文

在 Ubuntu 10.4 上使用 Qt 4.6

我的主窗口中有一个 QGraphicsView 中央小部件。每次程序切换到新级别(推箱子游戏)时,我希望主窗口调整到新的视图大小。我主要通过将主窗口和视图的最大和最小宽度/高度设置为同一事物来完成此操作,这在大多数情况下都有效,但在某些较小的级别上,主窗口仅在一维上收缩,并在上留下白色边距一侧像这样:

my Sokoban game

它保持这样,直到我在视图中单击,此时主窗口缩小到的观点。我可以在此之前输入按键来移动我的人并执行其他命令,并且边距将保留,需要单击鼠标才能缩小它。我想出了一个黑客修复程序,

move( geometry().x() + 1, geometry().y() ); // force mainWindow to update
move( geometry().x() - 1, geometry().y() ); // hackish but only thing that works

但这似乎很蹩脚,而且很可能不可移植,尽管我不确定其他平台上是否存在这个问题。有什么想法吗?

Using Qt 4.6 on Ubuntu 10.4

I have a QGraphicsView central widget in my MainWindow. Every time the program switches to a new level (Sokoban game), I want the MainWindow to adjust to the new size of the view. I mostly accomplished this by setting the maximum and minimum width/heights of both the mainwindow and view to the same thing, and this works most of the time, but on some smaller levels MainWindow only shrinks in one dimension, and leaves a white margin on one side like so:

my Sokoban game

It stays like that until I click in the view, at which point the MainWindow shrinks to the view. I can enter keystrokes before that to move my guy around and perform other commands and the margin will stay, it takes a mouse click to shrink it. I came up with a hackish fix by

move( geometry().x() + 1, geometry().y() ); // force mainWindow to update
move( geometry().x() - 1, geometry().y() ); // hackish but only thing that works

but this seems lame and most likely not portable, although I'm not sure this problem exists on other platforms. Any ideas?

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

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

发布评论

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

评论(3

萌逼全场 2024-11-23 17:00:06

当您使用调整大小setGeometry 方法,您的矩形由 小部件的最小尺寸。

默认情况下,这是 0,0 但是您是否有可能通过布局在代码中设置最小大小,以便调整大小方法拒绝您给定的参数?

When you use resize or setGeometry methods, your rectangle is bound by minimum size of the widget.

By default this is 0,0 but is there any chance that you are setting minimum size in your code perhaps by the layout so that resize method rejects your given parameter ?

孤芳又自赏 2024-11-23 17:00:06

这是我在 MainWindow Edit 中构建和设置视图/场景所使用的所有代码

m_scene->setSceneRect( -width/2, -height/2, width, height );

m_view = new QGraphicsView( m_scene );
m_view->setRenderHint( QPainter::Antialiasing );
m_view->setResizeAnchor( QGraphicsView::AnchorViewCenter );
m_view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
m_view->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
m_view->setFixedWidth( width );
m_view->setFixedHeight( height );
setMaximumWidth( width );
setMinimumWidth( width );
setMaximumHeight( height );
setMinimumHeight( height );
setCentralWidget( m_view );
m_view->show();
move( geometry().x() + 1, geometry().y() ); // force mainWindow to update
move( geometry().x() - 1, geometry().y() ); // hackish but only thing that works
m_view->setFocus();

:我的程序中唯一的布局(我所知道的)是隐式构造的 MainWindow 布局,我没有使用任何 QGraphicsLayouts 等。

Here's all the code that I used with building and setting up the view/scene from MainWindow

m_scene->setSceneRect( -width/2, -height/2, width, height );

m_view = new QGraphicsView( m_scene );
m_view->setRenderHint( QPainter::Antialiasing );
m_view->setResizeAnchor( QGraphicsView::AnchorViewCenter );
m_view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
m_view->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
m_view->setFixedWidth( width );
m_view->setFixedHeight( height );
setMaximumWidth( width );
setMinimumWidth( width );
setMaximumHeight( height );
setMinimumHeight( height );
setCentralWidget( m_view );
m_view->show();
move( geometry().x() + 1, geometry().y() ); // force mainWindow to update
move( geometry().x() - 1, geometry().y() ); // hackish but only thing that works
m_view->setFocus();

Edit: the only layout in my program (that I know about) is the implicitly constructed MainWindow layout, I haven't used any QGraphicsLayouts or the like.

烟若柳尘 2024-11-23 17:00:06

我通过以下方式找到了更好的修复方法:

...
setCentralWidget( m_view );
m_view->show();
activateWindow();
raise();
m_view->setFocus();

我注意到当显示的新关卡视图具有与之前相同的 x 尺寸但 y 尺寸较小(反之亦然)时,边距无法缩小,因此显然 QMainWindow 仅调整其大小当子窗口小部件的宽度和高度发生变化,或者 MainWindow 接收到移动事件(至少在 GNOME 2.x 上)时,将其更改为它的子内容。在 MainWindow 上调用 activateWindow() 和 raise() 似乎可以解决问题,然后我可以将焦点返回到视图。

I found a better fix by the following:

...
setCentralWidget( m_view );
m_view->show();
activateWindow();
raise();
m_view->setFocus();

I noticed the margins fail to shrink when the new level view being shown has the same x dimensions as the previous, but a smaller y dimension (or vice versa), so apparently QMainWindow only adjusts its size to its child contents when both the width and height of the child widget changes, or the MainWindow receives a move event (at least on GNOME 2.x). Calling activateWindow() and raise() on the MainWindow seems to do the trick, and then I can return focus to the view.

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