简体   繁体   中英

Python: Connect QlineEdit with QlcdNumber via button

Why doesn't the content of the edit box get transferred into the lcdNumber field?

class MainDialog (QDialog, MultiTool_widget_ui.Ui_Form):
    def __init__(self):
        #super(MainDialog, self).__init__() OR <next line>
        QDialog.__init__(self)
        self.setupUi(self)
        self.connect(self.pushButton, SIGNAL("clicked()"),self.lcdNumber.display(self.lineEdit.text()))    

The way you've connected the slot and the signal is the way you would do it in C++, which is not the same way it's done in pyside.

In an articel over at Zetcode , there's sample code of this exact program:

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

def __init__(self):
    super(Example, self).__init__()

    self.initUI()

def initUI(self):

    lcd = QtGui.QLCDNumber(self)
    sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)

    vbox = QtGui.QVBoxLayout()
    vbox.addWidget(lcd)
    vbox.addWidget(sld)

    self.setLayout(vbox)
    sld.valueChanged.connect(lcd.display)

    self.setGeometry(300, 300, 250, 150)
    self.setWindowTitle('Signal & slot')
    self.show()

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


if __name__ == '__main__':
    main()

This shows not only how the entire program should be written (I assume this is what you're aiming for), but also the way you connect signals and slots in PySide.

So instead of the C++ way:

self.connect(self.pushButton, SIGNAL("clicked()"),self.lcdNumber.display(self.lineEdit.text()))

You should have:

sld.valueChanged.connect(lcd.display)

Or in your case:

sld.valueChanged.connect(self.lineEdit.setText()) 

Also notice that I wrote "setText()" instead of just "text()" as "text()" returns the current text, where "setText()" changes it.

After re-reading the question, here's the snippet that'll make it work:

class MainDialog (QDialog, MultiTool_widget_ui.Ui_Form):
    def __init__(self):
        #super(MainDialog, self).__init__() OR <next line>
        QDialog.__init__(self)
        self.setupUi(self)

        self.btn = QPushButton("Click ME!")
        self.le = QLineEdit(self)
        self.lcd = QLCDDisplay(self)

        btn.clicked.connect(self.onBtnClicked)

        vbox = QVBoxLayout(self)
        vbox.addWidget(self.btn)
        vbox.addWidget(self.le)
        vbox.addWidget(self.lcd)
        self.setLayout(vbox)

    def onBtnClicked():
        self.lcd.display(self.le.text(())

I hope this last edit should do the trick, I am however unable to test this right now as I am typing on my phone ;)

Signals must be connected to a callable object . But in your example:

    self.connect(self.pushButton, SIGNAL("clicked()"),
        self.lcdNumber.display(self.lineEdit.text()))

you are actually passing in the return value of the display() method, which in this case, is None .

To fix your example, you can use a lambda function, like this:

    self.pushButton.clicked.connect(
        lambda: self.lcdNumber.display(self.lineEdit.text()))

Now you are passing in a function object , which will be called when the signal is fired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM