Search code examples
pythonsignals-slotspysideqlineedit

Pyside: Use button to copy text from qlineEdit to a label and to a string var


I'm trying to get a pyside button to copy text from a qlineEdit field to a label and to a string variable. I have been through almost all of the Zetcode tutorials, but obviously I am missing something critical here. Be gentle, I'm a python newbie and a pyside newbie. I'm looking for more pyside tutorials.

I tried using my Qbutton to copy the text from the lineEdit, and then in a fit of desperation, I tried xxxxxxx

Here's my code (any suggestions would be much appreciated, particularly with a link to where I can learn about what I needed to know):

Thank you for your help, Marc

import sys
from PySide import QtGui, QtCore   

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()        
        self.initUI()

    def initUI(self):
        nu_prg_name_label = QtGui.QLabel('Program Name:')
        author_label = QtGui.QLabel('Author')

        qle = QtGui.QLineEdit(self)
        qle.textChanged[str].connect(self.onChanged)

        # I added the buttons 
        okButton = QtGui.QPushButton("OK")
        cancelButton = QtGui.QPushButton("Cancel")

        grid = QtGui.QGridLayout()
        grid.setSpacing(4)

        # (arg__1, row, column, rowSpan, columnSpan[, alignment=0])
        grid.addWidget(nu_prg_name_label, 1, 0)
        grid.addWidget(author_label, 2, 0)
        grid.addWidget(qle, 1, 1, 1, 4)

        # I added the following 2 lines
        grid.addWidget(okButton, 3, 3)
        grid.addWidget(cancelButton, 3, 4)
        #grid.addWidget(review_label, 3, 0)

        # The QPushButton has a predefined 'signal' called 'clicked' 
        #   which is triggered every time that the button is pressed. 
        #   We will just 'connect' this signal to the sayHello() function:
        # Connect the button to the function
        okButton.clicked.connect(self.sendtxt2_qle)

        #grid.addWidget(author_label, 2, 0)
        #grid.addWidget(author_LineEdit02, 2, 1)

        #grid.addWidget(review_label, 3, 0)
        #grid.addWidget(review_TextEdit, 3, 1, 5, 1)

        self.setLayout(grid) 

        # Horizontal, vertical, width, length
        self.setGeometry(900, 300, 400, 100)
        self.setWindowTitle('Create Dirs [Info, TestArea, ItWorks] for a Program')    
        self.show()

    def onChanged(self, text):          
        nu_prg_name = self.qle.getText()
        self.author_label.setText(nu_prg_name) 
        print "Line 67: nu_prg_name = " + nu_prg_name   

    def sendtxt2_qle(self):
        nu_prg_name = self.grid.qle.getText()
        self.grid.author_label.setText(nu_prg_name) 
        print "Line 72: nu_prg_name = " + nu_prg_name

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())   

if __name__ == '__main__':
    main()

Solution

  • There are several issues with the script you posted:

    1. author_label and qle are referenced outside the initUI method, so they need to be replaced with self.author_label and self.qle wherever they are used.
    2. The onChanged method tries to retrieve the line-edit text using the non-existent method getText. Use self.qle.text(), or, better still, use the text argument that is passed to the onChanged method by the textChanged signal.
    3. The sendtxt2_qle method has similar errors to (2), and also wrongly tries to reference qle and author_label as attributes of self.grid (which does not exist). See (1) for how to fix this.

    Note: When I ran the amended version of you script, I got some corrupted output from the print statements. This appears to be a bug in PySide, because the same script runs perfectly fine using PyQt4. (I'm using PySide-1.09.)