2016-11-14 113 views
1

我使用QtCreator,但是我在UI生成器中找不到QSplitter,所以我猜測它會在後面添加其餘的代碼。我的問題是:用PyQt實現分離器(QtCreator生成的文件)

如何添加一個QSplitter而不擰我的圖形用戶界面?

這是我的主窗口的基礎佈局看: Layout

我需要有tabWidget和左邊的QlistWidget之間的分離器。我只需要這兩個要素受到影響。我發現他們需要在一個單獨的佈局(他們),但如何添加一個Qsplitter而不會影響其他人。另外,當我將它們添加到分割器時,應該在將它們添加到後面的父佈局之前。爲了更清楚這是從截圖中生成的代碼:

def setupUi(self, MainWindow): 
    MainWindow.setObjectName("MainWindow") 
    MainWindow.resize(1135, 640) 
    self.centralwidget = QtWidgets.QWidget(MainWindow) 
    self.centralwidget.setObjectName("centralwidget") 
    self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) 
    self.verticalLayout.setObjectName("verticalLayout") 
    self.horizontalLayout_3 = QtWidgets.QHBoxLayout() 
    self.horizontalLayout_3.setObjectName("horizontalLayout_3") 
    self.verticalLayout_3 = QtWidgets.QVBoxLayout() 
    self.verticalLayout_3.setObjectName("verticalLayout_3") 
    self.label = QtWidgets.QLabel(self.centralwidget) 
    self.label.setObjectName("label") 
    self.verticalLayout_3.addWidget(self.label) 
    self.horizontalSlider_2 = QtWidgets.QSlider(self.centralwidget) 
    self.horizontalSlider_2.setOrientation(QtCore.Qt.Horizontal) 
    self.horizontalSlider_2.setObjectName("horizontalSlider_2") 
    self.verticalLayout_3.addWidget(self.horizontalSlider_2) 
    self.horizontalLayout_4 = QtWidgets.QHBoxLayout() 
    self.horizontalLayout_4.setObjectName("horizontalLayout_4") 
    self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget) 
    self.pushButton_4.setObjectName("pushButton_4") 
    self.horizontalLayout_4.addWidget(self.pushButton_4) 
    self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget) 
    self.pushButton_6.setObjectName("pushButton_6") 
    self.horizontalLayout_4.addWidget(self.pushButton_6) 
    self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget) 
    self.pushButton_5.setObjectName("pushButton_5") 
    self.horizontalLayout_4.addWidget(self.pushButton_5) 
    self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget) 
    self.pushButton_7.setObjectName("pushButton_7") 
    self.horizontalLayout_4.addWidget(self.pushButton_7) 
    self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget) 
    self.pushButton_8.setObjectName("pushButton_8") 
    self.horizontalLayout_4.addWidget(self.pushButton_8) 
    self.verticalLayout_3.addLayout(self.horizontalLayout_4) 
    self.horizontalLayout_3.addLayout(self.verticalLayout_3) 
    self.verticalLayout_4 = QtWidgets.QVBoxLayout() 
    self.verticalLayout_4.setObjectName("verticalLayout_4") 
    self.horizontalLayout_5 = QtWidgets.QHBoxLayout() 
    self.horizontalLayout_5.setObjectName("horizontalLayout_5") 
    self.label_3 = QtWidgets.QLabel(self.centralwidget) 
    self.label_3.setObjectName("label_3") 
    self.horizontalLayout_5.addWidget(self.label_3) 
    self.label_2 = QtWidgets.QLabel(self.centralwidget) 
    self.label_2.setObjectName("label_2") 
    self.horizontalLayout_5.addWidget(self.label_2) 
    self.verticalLayout_4.addLayout(self.horizontalLayout_5) 
    self.horizontalSlider_3 = QtWidgets.QSlider(self.centralwidget) 
    self.horizontalSlider_3.setOrientation(QtCore.Qt.Horizontal) 
    self.horizontalSlider_3.setObjectName("horizontalSlider_3") 
    self.verticalLayout_4.addWidget(self.horizontalSlider_3) 
    self.horizontalLayout_3.addLayout(self.verticalLayout_4) 
    self.verticalLayout.addLayout(self.horizontalLayout_3) 
    self.horizontalLayout_2 = QtWidgets.QHBoxLayout() 
    self.horizontalLayout_2.setObjectName("horizontalLayout_2") 
    self.listWidget = QtWidgets.QListWidget(self.centralwidget) 
    self.listWidget.setObjectName("listWidget") 
    self.horizontalLayout_2.addWidget(self.listWidget) 
    self.tabWidget = QtWidgets.QTabWidget(self.centralwidget) 
    self.tabWidget.setObjectName("tabWidget") 
    self.tab = QtWidgets.QWidget() 
    self.tab.setObjectName("tab") 
    self.tabWidget.addTab(self.tab, "") 
    self.tab_2 = QtWidgets.QWidget() 
    self.tab_2.setObjectName("tab_2") 
    self.tabWidget.addTab(self.tab_2, "") 
    self.horizontalLayout_2.addWidget(self.tabWidget) 
    self.verticalLayout.addLayout(self.horizontalLayout_2) 
    self.horizontalLayout = QtWidgets.QHBoxLayout() 
    self.horizontalLayout.setObjectName("horizontalLayout") 
    self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) 
    self.pushButton_2.setObjectName("pushButton_2") 
    self.horizontalLayout.addWidget(self.pushButton_2) 
    self.pushButton = QtWidgets.QPushButton(self.centralwidget) 
    self.pushButton.setObjectName("pushButton") 
    self.horizontalLayout.addWidget(self.pushButton) 
    self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) 
    self.pushButton_3.setObjectName("pushButton_3") 
    self.horizontalLayout.addWidget(self.pushButton_3) 
    spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) 
    self.horizontalLayout.addItem(spacerItem) 
    self.horizontalSlider = QtWidgets.QSlider(self.centralwidget) 
    self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal) 
    self.horizontalSlider.setObjectName("horizontalSlider") 
    self.horizontalLayout.addWidget(self.horizontalSlider) 
    self.verticalLayout.addLayout(self.horizontalLayout) 
    MainWindow.setCentralWidget(self.centralwidget) 
    self.menubar = QtWidgets.QMenuBar(MainWindow) 
    self.menubar.setGeometry(QtCore.QRect(0, 0, 1135, 23)) 
    self.menubar.setObjectName("menubar") 
    MainWindow.setMenuBar(self.menubar) 
    self.statusbar = QtWidgets.QStatusBar(MainWindow) 
    self.statusbar.setObjectName("statusbar") 
    MainWindow.setStatusBar(self.statusbar) 

    self.retranslateUi(MainWindow) 
    QtCore.QMetaObject.connectSlotsByName(MainWindow) 

回答

2

一個QSplitter是一個佈局類,並且是在Qt Designer中可用。

您需要用水平分離器替換當前包含list-widget和tab-widget的水平佈局。這樣做的步驟是:

  1. 右鍵單擊主窗口中,選擇鋪陳 - 從菜單>中斷版面

  2. 右鍵單擊該選項卡的小部件(或列表控件),選擇鋪陳 - >中斷版面一次。

  3. 選擇標籤,窗口小部件和列表控件都,並選擇鋪陳 - >從右鍵菜單中水平放置退房分配器

  4. 右鍵單擊主窗口中,選擇鋪陳 - >從菜單佈局垂直

  5. 點擊中央的小部件(在對象樹),然後向下滾動到屬性編輯器的底部,並更改layoutStretch0,1,0

+0

完美地工作。謝謝你的好解釋。 – nephilimrising