Automatically resizing a window based on its contents

前端 未结 2 381
情深已故
情深已故 2021-01-23 03:32

I have a QDialog subclass that contains a spacer as its only immediate child; all of the window’s UI elements are contained in the spacer. The user cannot change the window size

相关标签:
2条回答
  • 2021-01-23 04:09

    void QWidget::adjustSize()

    Adjusts the size of the widget to fit its contents.

    0 讨论(0)
  • 2021-01-23 04:13

    You can resize the window to minimumSizeHint() after the number of widgets is changed :

    resize(minimumSizeHint());
    

    This will shrink the window to minimum size. But you should consider that the minimum size is not computed until some events are processed in the event loop. So after some widgets are hidden and some other are shown, just process the event loop for some iterations and then resize to minimum.

    It's like :

    for(int i=0;i<10;i++)
       qApp->processEvents();
    
    resize(minimumSizeHint());
    

    A better solution is to single shot a QTimer which calls a slot in which you resize the window to minimum. This way when you resize the window, the minimum size hint is computed correctly.

    0 讨论(0)
提交回复
热议问题