2015-05-28 172 views
0

我在對話框中隱藏了一個控件。當我顯示它時,我希望對話窗口相應地展開,並在我選擇隱藏時再次縮小。在Qt中顯示隱藏控件時調整窗口大小

如何才能做到這一點?我已經嘗試瞭解Qt的佈局功能,但我發現很難理解。

+0

這可能會有所幫助:[resize-window-to-fit-content](http://stackoverflow.com/questions/16468235/resize-window-to-fit-content) – songziming

回答

1

嘗試使用了QWidget :: adjustSize()用於後顯示/隱藏內容容器。

+0

謝謝!像魅力一樣工作,並認真對待我遇到的最簡單的解決方案。 – chwi

1

不知道是否有一個內置的解決方案,但是這是我的說明書一:

Dialog::Dialog(QWidget *parent) : 
    QDialog(parent) 
{ 
    setupUi(this); 

    connect(toolButton, SIGNAL(toggled(bool)), SLOT(onToolButton(bool))); 

    onToolButton(false); 
} 

void Dialog::onToolButton(bool checked) 
{ 
    lineEdit->setVisible(checked); 

    int maxHeight = verticalLayout->contentsMargins().top() 
      + verticalLayout->contentsMargins().bottom(); 
    int itemsCount = 0; 
    for (int i = 0; i < verticalLayout->count(); ++i) { 
     QLayoutItem *item = verticalLayout->itemAt(i); 
     if (item->widget()) { 
      QWidget *w = item->widget(); 
      if (w->isVisible()) { 
       maxHeight += w->geometry().height(); 
       ++itemsCount; 
      } 
     } else if (item->layout()) { 
      QLayout *l = item->layout(); 
      maxHeight += l->geometry().height(); 
      ++itemsCount; 
     } 
    } 
    if (itemsCount > 1) 
     maxHeight += ((itemsCount - 1) * verticalLayout->spacing()); 

    setMaximumHeight(maxHeight); 
} 
相關問題