2014-10-11 60 views
2

我想要創建一個應用程序,它由垂直佈局中的QLineEdit和兩個QTableView小部件組成。如何讓Qt小部件可調整大小?

示例代碼:

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>Dialog</class> 
<widget class="QDialog" name="Dialog"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>400</width> 
    <height>300</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>Dialog</string> 
    </property> 
    <widget class="QWidget" name="verticalLayoutWidget"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>401</width> 
    <height>301</height> 
    </rect> 
    </property> 
    <layout class="QVBoxLayout" name="verticalLayout"> 
    <item> 
    <widget class="QLineEdit" name="lineEdit"/> 
    </item> 
    <item> 
    <widget class="QTableView" name="tableView_2"/> 
    </item> 
    <item> 
    <widget class="QTableView" name="tableView"/> 
    </item> 
    </layout> 
    </widget> 
</widget> 
<resources/> 
<connections> 
    <connection> 
    <sender>lineEdit</sender> 
    <signal>returnPressed()</signal> 
    <receiver>Dialog</receiver> 
    <slot>close()</slot> 
    <hints> 
    <hint type="sourcelabel"> 
    <x>200</x> 
    <y>14</y> 
    </hint> 
    <hint type="destinationlabel"> 
    <x>199</x> 
    <y>149</y> 
    </hint> 
    </hints> 
    </connection> 
</connections> 
</ui> 

但這些QTableView小窗口不是調整大小。我沒有在Qt找到任何財產。

那麼我該如何讓這些QTableView小部件可調整大小或自動擴展和縮小呢?

意思是說,如果我減小第一個零件的尺寸然後自動調整第二個零件的尺寸QTableView小部件應該增加意味着如果在運行時更改垂直佈局內的任何小部件的尺寸,則每個小部件都應自動展開或收縮。

樣本代碼設計在Qt4 DesignerUbuntu 14.04

+0

@Chernobyl我想通過鼠標調整窗口小部件的大小 – Patrick 2014-10-11 08:26:41

回答

3

打破當前佈局,選擇兩個小工具,點擊Layout Vertically in Splitter,最後選擇表格並點擊layout verticallyenter image description here

+0

垂直佈局中的佈局? – Patrick 2014-10-11 08:39:40

+1

@Patrick對不起,但我不知道python。反正Zlatomir的解決方案也很好。爲他+1。 – Chernobyl 2014-10-11 08:48:37

+0

@Zlatomir完成說,但在QTableView小部件的邊界預覽中沒有顯示調整它的任何符號。 – Patrick 2014-10-11 08:48:47

1

這是因爲您的QDialog沒有佈局。你做的是你在對話框上拖動了一個垂直佈局,現在這個對話框包含的佈局是absolute,並且沒有鏈接到對話框。你想要做的是將所有的小部件從QSplitter頂部的垂直佈局中移出,右鍵單擊QDialogLay out上下文菜單項,選擇Lay Out Vertically,或者點擊QDialog並按Ctrl + 2。結果應該是以下幾點:

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>Dialog</class> 
<widget class="QDialog" name="Dialog"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>405</width> 
    <height>305</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>Dialog</string> 
    </property> 
    <layout class="QVBoxLayout" name="verticalLayout"> 
    <item> 
    <widget class="QSplitter" name="splitter"> 
    <property name="orientation"> 
     <enum>Qt::Vertical</enum> 
    </property> 
    <widget class="QLineEdit" name="lineEdit"/> 
    <widget class="QTableView" name="tableView_2"/> 
    <widget class="QTableView" name="tableView"/> 
    </widget> 
    </item> 
    </layout> 
</widget> 
<resources/> 
<connections/> 
</ui> 

enter image description here

我的建議是閱讀Qt Layout Management Documentation關於這一主題的更多信息。

+0

@luliu我試過你的代碼,當鼠標放在邊框上時,它也不給出調整窗口小部件大小的選項。 – Patrick 2014-10-11 12:34:57

+0

@Patrick,所以你想通過拖動小部件之間的區域來改變每個小部件的高度? – Iuliu 2014-10-11 12:38:52

+0

@帕特里克如果是的話,請參閱我的編輯。 – Iuliu 2014-10-11 12:45:03

相關問題