[PySide] Passing parameter from one class to an external function

Sebastian Elsner sebastian at risefx.com
Thu Apr 5 08:37:24 CEST 2012


Having a slot (your greetings function) outside a Class is never going
to work. This is from the Qt docs:

All classes that contain signals or slots must mention Q_OBJECT
<http://qt-project.org/doc/qt-4.8/qobject.html#Q_OBJECT> at the top of
their declaration. They must also derive (directly or indirectly) from
QObject <http://qt-project.org/doc/qt-4.8/qobject.html>.

The last sentence is the most interesting for you. I suggest reading the
whole signal slots page, its very important. What you can do is this:

import sys
from PySide.QtCore import *
from PySide.QtGui import *


class Form(QDialog):
  
    def __init__(self, parent=None):

        QDialog.__init__(self, parent=None)       

        self.edit = QLineEdit("Write my name here")
        self.button = QPushButton("Show Greetings")       
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.edit)
        self.layout.addWidget(self.button)      
        self.setLayout(self.layout)
        self.button.clicked.connect(self._greetings)
       
 
    def _greetings(self):
        greetings(self)

def greetings(form_instance):
    print ("Hello %s" %form_instance.edit.text())
   
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    sys.exit( app.exec_() )

Am 05.04.2012 06:54, schrieb Frank Rueter | OHUfx:
> Not sure if I follow (I'm new to PySide myself).
> Your "greetings" function is outside the "Form" class, yet it takes the 
> argument "self" and tries to call "self.edit.text()" which is unknown 
> outside of the "Form" class.
> Hence my suggestion to simply increase the indentation of the function 
> to make it a method to the class and things will fall into place:
> http://pastebin.com/rNu2NQQa
>
>
>
> On 4/5/12 1:27 PM, craf wrote:
>>> just indent your "greetings" function so it becomes a method to the
>>> "Form" class, then it will work.
>> Hi Frank.
>>
>> Thanks for your response, but the code is fine.
>> The function is outside the class, and I need to call it from within. In
>> WxWidget and Pygtk can be done, but I've failed PySide.
>>
>> Any suggestions?
>>
>> Regards,
>>
>> Cris
>>
>> _______________________________________________
>> PySide mailing list
>> PySide at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/pyside
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20120405/f193a85f/attachment.html>


More information about the PySide mailing list