删除所有DockWidgets后如何调整QMainWindow的大小? [英] How to resize QMainWindow after removing all DockWidgets?

查看:279
本文介绍了删除所有DockWidgets后如何调整QMainWindow的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个由QMainWindow组成的应用程序,其中央小部件为QToolBar(这可能不常见,但出于我的目的,工具栏非常适合).船坞仅在下方允许入内.我在其中添加了QDockWidget,并且QToolBar上的QAction使用removeDockWidget()restoreDockWidget()来打开和关闭QDockWidget.

I’m trying to make an application consisting of a QMainWindow, the central widget of which is a QToolBar (it may not be usual, but for my purpose the toolbar’s well suited). Docks are allowed below only. I added a QDockWidget to it, and a QAction on the QToolBar toggles the QDockWidget on and off with removeDockWidget() and restoreDockWidget().

QMainWindow的默认大小为800 x 24,QToolBarmaximumHeight也设置为24.在调用removeDockWidget()之后,将QMainWindow的几何图形设置为通过setGeometry()设置为(0,0,800,24).

The default size of the QMainWindow is 800 by 24, QToolBar’s maximumHeight is set to 24 too. Right after the removeDockWidget() is called, QMainWindow’s geometry is set back to (0,0,800,24) with setGeometry().

我想要实现的是在DockWidget删除后将QMainWindow的高度调整为24. setGeometry()似乎可以工作,因为宽度和位置会相应地变化,但是有趣的是,高度没有变化.那真的是我的问题:)

What I want to achieve is to resize the QMainWindow’s height to 24 when the DockWidget’s removed. The setGeometry() seems to work since width and position change accordingly, but funnily enough, the height doesn’t budge. And that’s my problem really :)

你在想什么?

此处是一个截屏视频,展示了当前的问题.

Here is a screen-cast illustrating the issue at hand.

注意:如果我使用QWidget而不是QMainWindow并在子窗口小部件上使用show()hide()来创建相同的场景,那么我可以使用adjustSize()调整父窗口大小而没有问题:这似乎是问题所在上面是QMainWindow特有的.

NB: if i create the same scenario using a QWidget rather than QMainWindow, and using a show() or hide() on the child widget, then I can resize the parent with adjustSize() without problem: it seems the problem here above is QMainWindow specific.

推荐答案

选项

a)您可以重载size​​Hint()虚拟函数.让它返回您想要的主窗口大小.

a) You can overload sizeHint() a virtual function. Let it return the size you want for your main window.

b)在主窗口的构造函数中,可以使用所需的主窗口大小依次调用setMinimumSize()和setMaximumSize().如果两者保持相同,则将获得固定大小.

b) In the main window's constructor you can call setMinimumSize() and setMaximumSize() one after another, both with the the desired main window size. If you keep both same you get a fixed size.

c)看一下layout()-> setResizeMode(Fixed).

c) Take a look at layout()->setResizeMode(Fixed).

这篇关于删除所有DockWidgets后如何调整QMainWindow的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆