Question

I have a program that creates a number of qlineedits and buttons depending on the users input:

screenshot of GUI

On the image above 4 lines have been added with a button after the grayed out "Next" button was clicked. Now I want to get the input from the user into a function when the corresponding button is clicked (Click "Create Shot 1! --> goto a function with "exShot1" passed as an argument).

The thing is I have no idea how to get the names of each qline and button when they are created in a loop. I guess I could create unique variables in the loop but that doesn't feel right. I have tried using setObjectName but I can't figure out how I can use that to call the text. I also made an unsuccessful attempt with Lamdba (which I have a feeling might be the right way to go somehow) I believe it's a combination of having to fetch the name and tracking when the user input is changed.

I have experimented with textChanged and I got it to work on the last entry of the loop but not for the other qlines and buttons)

Relevant code:

while i <= int(seqNum):
    #create each widget
    self.createShotBtn = QtGui.QPushButton("Create Shot %s!" %str(self.shotNumberLst[i-1]))
    self.labelName = QtGui.QLabel(self)
    self.labelName.setText("Enter Name Of Shot %s!" %str(self.shotNumberLst[i-1]))
    self.shotName = QtGui.QLineEdit(self)
    self.shotName.setObjectName("shot"+str(i))

    #add widget to layout
    self.grid.addWidget(self.labelName, 11+shotjump,0)
    self.grid.addWidget(self.shotName,11+shotjump,1)
    self.grid.addWidget(self.createShotBtn, 11+shotjump,2)

    #Press button that makes magic happen
    self.createShotBtn.clicked.connect(???)

    i += 1

edit: It would also be fine if the user entered input on all the lines and just pressed one button that passed all those inputs as a list or dict (there will be more lines added per "shot")

Was it helpful?

Solution

The problem is that on each run through the values of self.createShotBtn, self.labelName and self.shotName are being overridden.

So on the last run through, they are fixed, but only for the last iteration.

Instead, you want to use a locally scoped variable in the loop, and potentially store it in an array for later use.

This code should come close to what you need, but I can see where self.shotNumberLst (which returns a number?) and shotjump (which is an offest, or equal to to i) are declared.

self.shots = []
for i in range(seqNum): # Changed while to for, so you don't need to increment
    #create each widget
    createShotBtn = QtGui.QPushButton("Create Shot %s!" %str(self.shotNumberLst[i-1]))
    labelName = QtGui.QLabel(self)
    labelName.setText("Enter Name Of Shot %s!" %str(self.shotNumberLst[i-1]))
    shotName = QtGui.QLineEdit(self)

    self.shots.append({"button":createShotBtn,
                       "name":shotName)) # Store for later if needed.

    #add widget to layout
    self.grid.addWidget(labelName, 11+shotjump,0)
    self.grid.addWidget(shotName,11+shotjump,1)
    self.grid.addWidget(createShotBtn, 11+shotjump,2)

    #Press button that makes magic happen
    createShotBtn.clicked.connect(self.createShot(i))

#elsewhere
def createShot(self,index):
    print self.shots[index]["name"].text

OTHER TIPS

Try this,

while i <= int(seqNum):

    #create each widget
    createShotBtn = "ShotBtn"+str(i)
    self.createShotBtn = QtGui.QPushButton("Create Shot %s!" %str(self.shotNumberLst[i-1]))

    labelName = "labName"+str(i)
    self.labelName = QtGui.QLabel(self)
    self.labelName.setText("Enter Name Of Shot %s!" %str(self.shotNumberLst[i-1]))

    shotName = "shtName"+str(i)
    self.shotName = QtGui.QLineEdit(self)


    #add widget to layout
    self.grid.addWidget(self.labelName, 11+shotjump,0)
    self.grid.addWidget(self.shotName,11+shotjump,1)
    self.grid.addWidget(self.createShotBtn, 11+shotjump,2)

    #Press button that makes magic happen
    self.createShotBtn.clicked.connect(self.printText)

    i += 1

def printText(self):
    print(self.shotName.text())

This will print the text when you push the button on the same line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top