Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt: Creating QT Widgets Programmatically

There are times when there is a need to create as many widgets (such as QtGui.QLineEdit() ) as there are values in a list (we don't know how many values stored in a list variable). I could create a loop function to be run as many times as there are values stored in a list... such as:

for each in myList:
    myLineEdit = QtGui.QLineEdit("myLineEdit")

The problem with an approach like this is that with every loop the same variable name is declared. There will be no way to access the myLineEdit variable later. I've heard some have success using eval()? or exec()? function. It would be interesting to see an example. If there are other way to do it please post it up. Here is the example code to start with (if you would like to):

from PyQt4 import QtCore, QtGui

app = QtGui.QApplication(sys.argv)

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        mainQWidget = QtGui.QWidget()
        mainLayout=QtGui.QVBoxLayout()

        for i in range(5):
            exec( 'myGroupBox'+str(i)+'= QtGui.QGroupBox() ' )
            exec( 'myLayout'+str(i)+' = QtGui.QHBoxLayout()' )       

            exec( 'label'+str(i)+'=QtGui.QLabel("Name '+str(i)+': ")' )
            exec( 'self.myLineEdit'+str(i)+'=QtGui.QLineEdit()' )

            exec( 'myLayout'+str(i)+'.addWidget(label'+str(i)+')' )
            exec( 'myLayout'+str(i)+'.addWidget(self.myLineEdit'+str(i)+', QtCore.Qt.AlignRight)' )

            exec( 'myGroupBox'+str(i)+'.setLayout(myLayout'+str(i)+')' )
            exec( 'mainLayout.addWidget(myGroupBox'+str(i)+')' )

        mainQWidget.setLayout(mainLayout)        
        self.setCentralWidget(mainQWidget) 

window = MainWindow()
window.show()
window.resize(480,320)
sys.exit(app.exec_())

ANOTHER EXAMPLE:

from PyQt4 import QtCore, QtGui
import random
app = QtGui.QApplication(sys.argv)

class MainWindow(QtGui.QMainWindow):
    def __init__(self, argList):
        super(MainWindow, self).__init__()
        self.argList=argList

        mainQWidget = QtGui.QWidget()
        mainLayout=QtGui.QVBoxLayout()

        for i in range(len(self.argList)):
            exec( 'myGroupBox'+str(i)+'= QtGui.QGroupBox() ' )
            exec( 'myLayout'+str(i)+' = QtGui.QHBoxLayout()' )                   

            exec( 'label'+str(i)+'=QtGui.QLabel("Name '+str(self.argList[i])+': ")' )
            exec( 'label'+str(i)+'.setFixedWidth(100)' )
            exec( 'self.myLineEdit'+str(i)+'=QtGui.QLineEdit()' )
            exec( 'self.myLineEdit'+str(i)+'.setText("'+str(random.random())+'")' )


            exec( 'myLayout'+str(i)+'.addWidget(label'+str(i)+')' )
            exec( 'myLayout'+str(i)+'.addWidget(self.myLineEdit'+str(i)+', QtCore.Qt.AlignRight)' )

            exec( 'myGroupBox'+str(i)+'.setLayout(myLayout'+str(i)+')' )
            exec( 'mainLayout.addWidget(myGroupBox'+str(i)+')' )

        ButtonBox = QtGui.QGroupBox()
        ButtonsLayout = QtGui.QHBoxLayout()

        Button_01 = QtGui.QPushButton("Close")
        Button_01.clicked.connect(self.close)

        Button_02 = QtGui.QPushButton("Print")
        Button_02.clicked.connect(self.printOut)

        ButtonsLayout.addWidget(Button_01)
        ButtonsLayout.addWidget(Button_02)

        ButtonBox.setLayout(ButtonsLayout)
        mainLayout.addWidget(ButtonBox)

        mainQWidget.setLayout(mainLayout)
        self.setCentralWidget(mainQWidget)


    def printOut(self):
        for i in range(len(self.argList)):
            exec( 'print self.myLineEdit'+str(i)+'.text()' )
    def close(self):
        sys.exit()


myList=['One','Two','Tree','Four','Five','Six','Seven']
window = MainWindow(myList)
window.show()
window.resize(480,320)
sys.exit(app.exec_())
like image 282
alphanumeric Avatar asked Jan 11 '23 01:01

alphanumeric


1 Answers

A easy way to handle this situation is to use a list:

lineEdits = []
for _ in range(5):
    myLineEdit = QtGui.QLineEdit("myLineEdit")
    lineEdits.append(myLineEdit)

myLayout = QtGui.QHBoxLayout()
for lineEdit in lineEdits:
    myLayout.addWidget(lineEdit)

Alternatively you could use a dictionary, if you'd later like to reference a widget by a key:

lineEdits = {}
for i in range(5):
    myLineEdit = QtGui.QLineEdit("myLineEdit")
    lineEdits[i] = myLineEdit

lineEdits[3].setText("My new text")
like image 157
Soytoise Avatar answered Jan 22 '23 10:01

Soytoise