0

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()))    
Creatronik
  • 189
  • 3
  • 18

2 Answers2

0

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 ;)

Pkarls
  • 35
  • 3
  • 18
  • I understand. I coded a little in C++ before and I just started reading tutorials. But I want to know how i connect these 2 objects' contents by pushing that button. How is the syntax for that action? – Creatronik Mar 15 '16 at 14:33
  • So if I understand this correctly, you want to have a button, a display and a lineedit. And when the button is clicked you want the lineedits value to be on the display? – Pkarls Mar 15 '16 at 14:35
  • Yes! I thought that was clear from my initial code snippet :) – Creatronik Mar 15 '16 at 14:39
  • No, it wasn't really. I'm at work currently, I'll type something up on my way home in ~1h :) – Pkarls Mar 15 '16 at 14:41
  • @Creatronik Updated my question. – Pkarls Mar 15 '16 at 14:54
  • Ok, that works of course because it's the same as in the tutorial. I thought it would be more intuitive such way that i could handle the whole function in that "connect" statement. Or is there a way? – Creatronik Mar 15 '16 at 17:02
0

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thank you. Interesting! Also that works with the syntax from the tutorial: self.pushButton.clicked.connect(lambda: self.lcdNumber.display(self.lineEdit.text())) – Creatronik Mar 16 '16 at 12:35