[PyQt] Implementing Qt-Designer Widgets in Main Window and lay them out

Michael Adolph Michael.Adolph at eonerc.rwth-aachen.de
Wed Jul 15 15:46:30 BST 2009


Hi everybody,

I've got a problem with Qt-Designer and Python. It's pretty basic but I can't find any solution.

I designed to forms in Qt-Designer and I'm writing a QMainWindow class in Python. The widgets are imported and should be layouted.
My Problem ist, that both widgets are shown one over another, not caring about my layout. This reduces usability pretty much.

Any hints how to layout imported Widgets? Code is appended. I had to do some minor changes by hand in the Qt-Designer generated code (or I'm just to much of a newbee...), so I added comments, wherever I tried to be smarter then Qt-Designer

cheers and thanks for any help

Michael


########## Begin Main-Programm:

import sys
from PyQt4 import QtGui,QtCore
import Uebersicht #First imported widget
import liegenschaftAuswahlDlg # second imported widget

class BasisTest(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.centralWidget=QtGui.QWidget(self)
        self.setCentralWidget(self.centralWidget)
        self.label=QtGui.QLabel("Test",self.centralWidget) # two Labels for Testing
        self.label2=QtGui.QLabel("schalalala",self.centralWidget)
        self.Uebersicht=Uebersicht.Ui_Uebersicht() # instanciate first Ui-Widget
        self.Uebersicht.setupUi(self) #show Widget
        self.Dialog=liegenschaftAuswahlDlg.Ui_liegenschaftAuswahlDlg()
        self.Dialog.setupUi(self)

# Create Grid Layout
        layout=QtGui.QGridLayout()
        layout.addWidget(self.label,0,0)
        layout.addWidget(self.label2,0,1)
        layout.addWidget(self.Dialog,1,1)
        self.centralWidget.setLayout(layout)

app=QtGui.QApplication(sys.argv)
main=BasisTest()
main.show()
sys.exit(app.exec_())

########### End Main Programm

########### Begin first Ui  - created by Qt-Designer

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'Uebersicht.ui'
#
# Created: Wed Jul 15 09:32:35 2009
#      by: PyQt4 UI code generator 4.4.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_Uebersicht(QtGui.QWidget): # changed manually from object to QtGui.QWidget, got invalid type error otherwise if trying to use QGridLayout
    def setupUi(self, Uebersicht):
        Uebersicht.setObjectName("Uebersicht") # I don't get, whats done here... . and why!
        Uebersicht.resize(268, 172)
        self.groupBox = QtGui.QGroupBox(Uebersicht)
        self.groupBox.setGeometry(QtCore.QRect(10, 10, 251, 151))
        self.groupBox.setObjectName("groupBox")
        self.layoutWidget = QtGui.QWidget(self.groupBox)
        self.layoutWidget.setGeometry(QtCore.QRect(20, 20, 211, 111))
        self.layoutWidget.setObjectName("layoutWidget")
        self.gridLayout = QtGui.QGridLayout(self.layoutWidget)
        self.gridLayout.setObjectName("gridLayout")
        self.label = QtGui.QLabel(self.layoutWidget)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.label_5 = QtGui.QLabel(self.layoutWidget)
        self.label_5.setObjectName("label_5")
        self.gridLayout.addWidget(self.label_5, 0, 1, 1, 1)
        self.label_2 = QtGui.QLabel(self.layoutWidget)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
        self.label_6 = QtGui.QLabel(self.layoutWidget)
        self.label_6.setObjectName("label_6")
        self.gridLayout.addWidget(self.label_6, 1, 1, 1, 1)
        self.label_3 = QtGui.QLabel(self.layoutWidget)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
        self.label_7 = QtGui.QLabel(self.layoutWidget)
        self.label_7.setObjectName("label_7")
        self.gridLayout.addWidget(self.label_7, 2, 1, 1, 1)
        self.label_4 = QtGui.QLabel(self.layoutWidget)
        self.label_4.setObjectName("label_4")
        self.gridLayout.addWidget(self.label_4, 3, 0, 1, 1)
        self.label_8 = QtGui.QLabel(self.layoutWidget)
        self.label_8.setObjectName("label_8")
        self.gridLayout.addWidget(self.label_8, 3, 1, 1, 1)

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

    def retranslateUi(self, Uebersicht):
        Uebersicht.setWindowTitle(QtGui.QApplication.translate("Uebersicht", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox.setTitle(QtGui.QApplication.translate("Uebersicht", "Label1", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Uebersicht", "Label2:", None, QtGui.QApplication.UnicodeUTF8))
        self.label_5.setText(QtGui.QApplication.translate("Uebersicht", "nicht ausgewählt", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("Uebersicht", "Label3", None, QtGui.QApplication.UnicodeUTF8))
        self.label_6.setText(QtGui.QApplication.translate("Uebersicht", "nicht ausgewählt", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("Uebersicht", Label4", None, QtGui.QApplication.UnicodeUTF8))
        self.label_7.setText(QtGui.QApplication.translate("Uebersicht", "nicht ausgewählt", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setText(QtGui.QApplication.translate("Uebersicht", "Label5", None, QtGui.QApplication.UnicodeUTF8))
        self.label_8.setText(QtGui.QApplication.translate("Uebersicht", "nicht ausgewählt", None, QtGui.QApplication.UnicodeUTF8))

########### End first Ui  - created by Qt-Designer

########### Begin second Ui  - created by Qt-Designer

[...] ommited comment lines

from PyQt4 import QtCore, QtGui

class Ui_liegenschaftAuswahlDlg(QtGui.QWidget): # changed manually from object to QtGui.QWidget, got invalid type error otherwise if trying to use QGridLayout
    def setupUi(self, liegenschaftAuswahlDlg):
        liegenschaftAuswahlDlg.setObjectName("liegenschaftAuswahlDlg")
        liegenschaftAuswahlDlg.resize(599, 200)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(liegenschaftAuswahlDlg.sizePolicy().hasHeightForWidth())
        liegenschaftAuswahlDlg.setSizePolicy(sizePolicy)
        liegenschaftAuswahlDlg.setSizeIncrement(QtCore.QSize(1, 2))
       # liegenschaftAuswahlDlg.setSizeGripEnabled(True) #code from Qt-Designer, but let to errors. therefore 
        self.widget = QtGui.QWidget(liegenschaftAuswahlDlg)
        self.widget.setGeometry(QtCore.QRect(20, 160, 541, 28))
        self.widget.setObjectName("widget")
        self.horizontalLayout = QtGui.QHBoxLayout(self.widget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.neuPushButton = QtGui.QPushButton(self.widget)
        self.neuPushButton.setFocusPolicy(QtCore.Qt.NoFocus)
        self.neuPushButton.setAutoDefault(False)
        self.neuPushButton.setObjectName("neuPushButton")
        self.horizontalLayout.addWidget(self.neuPushButton)
        spacerItem = QtGui.QSpacerItem(98, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.buttonBox = QtGui.QDialogButtonBox(self.widget)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.horizontalLayout.addWidget(self.buttonBox)
        self.widget1 = QtGui.QWidget(liegenschaftAuswahlDlg)
        self.widget1.setGeometry(QtCore.QRect(20, 30, 541, 100))
        self.widget1.setObjectName("widget1")
        self.gridLayout = QtGui.QGridLayout(self.widget1)
        self.gridLayout.setSizeConstraint(QtGui.QLayout.SetNoConstraint)
        self.gridLayout.setObjectName("gridLayout")
        self.label_2 = QtGui.QLabel(self.widget1)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 0, 0, 1, 1)
        self.comboBox = QtGui.QComboBox(self.widget1)
        self.comboBox.setObjectName("comboBox")
        self.gridLayout.addWidget(self.comboBox, 0, 1, 1, 1)
        self.label = QtGui.QLabel(self.widget1)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 2, 1, 1)
        self.comboBox_6 = QtGui.QComboBox(self.widget1)
        self.comboBox_6.setEnabled(False)
        self.comboBox_6.setObjectName("comboBox_6")
        self.gridLayout.addWidget(self.comboBox_6, 0, 3, 1, 1)
        self.label_3 = QtGui.QLabel(self.widget1)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 1, 0, 1, 1)
        self.comboBox_2 = QtGui.QComboBox(self.widget1)
        self.comboBox_2.setEnabled(False)
        self.comboBox_2.setObjectName("comboBox_2")
        self.gridLayout.addWidget(self.comboBox_2, 1, 1, 1, 1)
        self.label_4 = QtGui.QLabel(self.widget1)
        self.label_4.setObjectName("label_4")
        self.gridLayout.addWidget(self.label_4, 1, 2, 1, 1)
        self.comboBox_5 = QtGui.QComboBox(self.widget1)
        self.comboBox_5.setEnabled(False)
        self.comboBox_5.setObjectName("comboBox_5")
        self.gridLayout.addWidget(self.comboBox_5, 1, 3, 1, 1)
        self.label_5 = QtGui.QLabel(self.widget1)
        self.label_5.setObjectName("label_5")
        self.gridLayout.addWidget(self.label_5, 2, 0, 1, 1)
        self.comboBox_3 = QtGui.QComboBox(self.widget1)
        self.comboBox_3.setEnabled(False)
        self.comboBox_3.setObjectName("comboBox_3")
        self.gridLayout.addWidget(self.comboBox_3, 2, 1, 1, 1)
        self.label_6 = QtGui.QLabel(self.widget1)
        self.label_6.setObjectName("label_6")
        self.gridLayout.addWidget(self.label_6, 3, 0, 1, 1)
        self.comboBox_4 = QtGui.QComboBox(self.widget1)
        self.comboBox_4.setEnabled(False)
        self.comboBox_4.setFrame(True)
        self.comboBox_4.setObjectName("comboBox_4")
        self.gridLayout.addWidget(self.comboBox_4, 3, 1, 1, 1)
        self.label_2.setBuddy(self.comboBox)
        self.label.setBuddy(self.comboBox_6)
        self.label_3.setBuddy(self.comboBox_2)
        self.label_4.setBuddy(self.comboBox_5)
        self.label_5.setBuddy(self.comboBox_3)
        self.label_6.setBuddy(self.comboBox_4)

        self.retranslateUi(liegenschaftAuswahlDlg)
  #      QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), liegenschaftAuswahlDlg.accept) #led to errors, I didn't care and just deactivated it.
  #      QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), liegenschaftAuswahlDlg.reject)
        QtCore.QMetaObject.connectSlotsByName(liegenschaftAuswahlDlg)
        liegenschaftAuswahlDlg.setTabOrder(self.comboBox, self.comboBox_6)
        liegenschaftAuswahlDlg.setTabOrder(self.comboBox_6, self.comboBox_2)
        liegenschaftAuswahlDlg.setTabOrder(self.comboBox_2, self.comboBox_5)
        liegenschaftAuswahlDlg.setTabOrder(self.comboBox_5, self.comboBox_3)
        liegenschaftAuswahlDlg.setTabOrder(self.comboBox_3, self.comboBox_4)
        liegenschaftAuswahlDlg.setTabOrder(self.comboBox_4, self.buttonBox)

    def retranslateUi(self, liegenschaftAuswahlDlg):
        liegenschaftAuswahlDlg.setWindowTitle(QtGui.QApplication.translate("liegenschaftAuswahlDlg", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.neuPushButton.setText(QtGui.QApplication.translate("liegenschaftAuswahlDlg", "Neu anlegen", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("liegenschaftAuswahlDlg", "Label1", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("liegenschaftAuswahlDlg", "Label2", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("liegenschaftAuswahlDlg", "Label3", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setText(QtGui.QApplication.translate("liegenschaftAuswahlDlg", "Label4", None, QtGui.QApplication.UnicodeUTF8))
        self.label_5.setText(QtGui.QApplication.translate("liegenschaftAuswahlDlg", "Label5", None, QtGui.QApplication.UnicodeUTF8))
        self.label_6.setText(QtGui.QApplication.translate("liegenschaftAuswahlDlg", "Label6", None, QtGui.QApplication.UnicodeUTF8))





More information about the PyQt mailing list