5

When I have a QMainWindow with a grid layout, when resizing it with the mouse, it won't go below some minimum size that's necessary for all the controls in it to show properly. In my app I sometimes programatically hide controls, but then the window remains the same size and the rest of the controls look spread out, with too much space between them. I end up resizing the dialog manually so it doesn't look ugly.

Can I programatically set the dialog's vertical size to this minimum I get when manually resizing after I've hidden controls in it?

4

3 に答える 3

2

centralWidgetのサイズを変更してから、それ自体のQMainWindowサイズを変更するとうまくいくことがわかりました。QMainWindow言い換えると:

from PyQt4 import QtGui
class MyMainWindow(QtGui.QMainWindow):
    def __init__(self):
        # define some widgets and stuff
    def whenWidgetsAreHidden(self):
        # this method should be triggered when you hide your widgets
        self.centralWidget.adjustSize()
        self.adjustSize()

QMainWindowLayout には他のウィジェットがあることに注意してください。Qt Documentationのこの画像によると、Dock Widgets やその他の存在する可能性のあるものもあります。私は のみを使用するcentralWidgetため、このソリューションは私にとってはうまくいきます。

QMainWindow レイアウト

于 2016-05-24T18:31:40.080 に答える
1

QLayout::SizeConstraintQWidget::minimumSizeHintQWidget:: minimumSizeの間に階層があり、ドキュメントで見つけることができます。

  • QWidget::minimumSizeデフォルトでは設定されていません。あるときは勝つ QWidget::minimumSizeHint
  • QWidget::minimumSizeHintウィジェットがレイアウト内にない場合 (つまり、マウスで 0 にサイズ変更できる場合) は無効です。それ以外の場合は、レイアウトで定義されたものを使用します。
  • QLayout::SizeConstraint*直接*管理するウィジェットのデフォルトのレイアウト動作を保持します。Alayout内にlayout をネストするとB、追加されたすべてのウィジェットAがそのプロパティを使用します。また、ウィジェットが独自のレイアウトWを定義する場合、このレイアウト制約がウィジェットに適用されます。BW

それを知って、次の手順に従ってください。それはうまくいくかもしれません、私は試しませんでした:):

  • 使用するすべてのウィジェットの最小サイズを設定します。
  • すべてのレイアウトのサイズ制限が に設定されていることを確認してくださいQLayout::SetDefaultConstraint
于 2012-01-08T11:01:53.323 に答える
0

このコードはトリックを行います (高さのみをサイズ変更します):

    QWidget widget;
    widget.resize(widget.width(), widget.minimumHeight());
于 2012-01-08T13:52:59.237 に答える