Results 1 to 6 of 6

Thread: Sending signals from one module to another

  1. #1
    Join Date
    Jul 2010
    Posts
    4
    Qt products
    Platforms
    MacOS X Windows

    Default Sending signals from one module to another

    Hi,

    I have an application I am writing that has a fairly complicated set of functions that take a while to run.

    I have put them inside a separate module file (and in their own class) to keep things simple.

    the end of my main GUI file looks like this:

    Qt Code:
    1. if __name__ == "__main__":
    2. import sys
    3.  
    4. app = QtGui.QApplication(sys.argv)
    5.  
    6. if len(sys.argv) == 1:
    7. filePath = serverRoot + ''
    8. else:
    9. filePath = sys.argv[1]
    10.  
    11. chunks = os.path.split(filePath)
    12. fpath = chunks[0]
    13. fname = chunks[1]
    14.  
    15. panel = StartQT4(fpath, fname)
    16. panel.show()
    17.  
    18. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    I'm guessing this is a very newb question, but I can't seem to figure out how to send a signal (so I can update the UI) from a class in a separate file.

    Imagine the other file looks something like this:

    Qt Code:
    1. #myLib.py
    2.  
    3. class Stuff:
    4. def doComplicatedStuff():
    5. # Somehow send a signal here back to the main app that called this function
    To copy to clipboard, switch view to plain text mode 

    I am planning on running the "complicated function" in a separate thread so that the GUI updating will work.

    Am I way off here?
    This is only my 3rd day of Python, so please be gentle

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Sending signals from one module to another

    Quote Originally Posted by Dubbie View Post
    This is only my 3rd day of Python, so please be gentle
    I have exactly zero seconds of experience with Python, so I'm not able to help you with the language syntax.
    However, I can tell you how signals and slots work.

    You have two classes

    class MyClass1
    ...

    class MyClass2
    ...

    You then create objects based on these classes (I don't know how this is done in Python, so the rest is pseudo code)
    MyClass1 myClass1Object
    MyClass2 myClass2Object

    You can then link them together:
    connect(myClass1Object, SIGNAL(finishedTask1()), myClass2Object, SLOT(printResults()))
    connect(myClass2Object, SIGNAL(somethingElse()), myClass1Object, SLOT(doAnotherThing()))

    When using connect, you link one or two objects.

    Be careful when using signals and slots in threads, the documentation is not clear on how it works.

  3. #3
    Join Date
    Jul 2010
    Posts
    4
    Qt products
    Platforms
    MacOS X Windows

    Default Re: Sending signals from one module to another

    Thankyou for the hint!

    I *think* I get what you mean. I'll give it a try in the morning.

    Thanks very much for your help!

    R

  4. #4
    Join Date
    Jul 2010
    Posts
    4
    Qt products
    Platforms
    MacOS X Windows

    Default Re: Sending signals from one module to another

    Ok, I'm still not getting this working.

    I have written a bare-bones app to illustrate what I want to do:

    This is my main file: (test2_ui.py)

    Qt Code:
    1. from test2 import *
    2. from PyQt4 import QtCore, QtGui
    3.  
    4. class StartQT4(QtGui.QMainWindow,):
    5.  
    6. def __init__(self, parent=None):
    7.  
    8. QtGui.QWidget.__init__(self, parent)
    9. self.ui = Ui_Dialog()
    10. self.ui.setupUi(self)
    11.  
    12. QtCore.QObject.connect(self.ui.pushButton,QtCore.SIGNAL("clicked()"), self.ok)
    13.  
    14. def closeApp(self):
    15. self.close()
    16.  
    17. def ok(self):
    18. import testlib
    19. tl = testlib.lazyworker()
    20. QtCore.QObject.connect(tl,QtCore.SIGNAL("update(int)"), self.updateUI)
    21. tl.doWork()
    22.  
    23. def updateUI(self, prog):
    24. self.ui.label.setText(str(prog))
    25.  
    26. if __name__ == "__main__":
    27. import sys
    28.  
    29. app = QtGui.QApplication(sys.argv)
    30.  
    31. panel = StartQT4()
    32. panel.show()
    33.  
    34. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    This is my UI include: (test2.py)

    Qt Code:
    1. from PyQt4 import QtCore, QtGui
    2.  
    3. class Ui_Dialog(object):
    4. def setupUi(self, Dialog):
    5. Dialog.setObjectName("Dialog")
    6. Dialog.resize(400, 300)
    7. self.label = QtGui.QLabel(Dialog)
    8. self.label.setGeometry(QtCore.QRect(70, 120, 221, 16))
    9. self.label.setObjectName("label")
    10. self.pushButton = QtGui.QPushButton(Dialog)
    11. self.pushButton.setGeometry(QtCore.QRect(70, 180, 231, 91))
    12. self.pushButton.setObjectName("pushButton")
    13.  
    14. self.retranslateUi(Dialog)
    15. QtCore.QMetaObject.connectSlotsByName(Dialog)
    16.  
    17. def retranslateUi(self, Dialog):
    18. Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
    19. self.label.setText(QtGui.QApplication.translate("Dialog", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))
    20. self.pushButton.setText(QtGui.QApplication.translate("Dialog", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
    To copy to clipboard, switch view to plain text mode 

    This is my module with class inside: (testlib.py)

    Qt Code:
    1. import time
    2. from PyQt4 import QtCore, QtGui
    3.  
    4. class lazyworker:
    5. def doWork(self):
    6. for self.i in range(0,100):
    7. time.sleep(0.1)
    8. self.emit(SIGNAL("update(int)"), self.i)
    To copy to clipboard, switch view to plain text mode 

    When I try to run this I get the following error:

    Qt Code:
    1. TypeError: arguments did not match any overloaded call:
    2. QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'lazyworker'
    3. QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'lazyworker'
    4. QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): first argument of unbound method must have type 'QObject'
    To copy to clipboard, switch view to plain text mode 

    I gather this is because it wants a class of type Qobject, not my "lazyworker" type.

    I'm not really sure what do to from here.

    Thanks in advance!

    R

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sending signals from one module to another

    Lazyworker needs to inherit from QObject to gain the needed functionality. Not a snake handler either but something like;
    Qt Code:
    1. class lazyworker(QtCore.QObject):
    2. ...
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jul 2010
    Posts
    4
    Qt products
    Platforms
    MacOS X Windows

    Default Re: Sending signals from one module to another

    Ah, that was the missing link!

    Thanks so much.

    It's all working great now, of course the UI doesn't update, but I figure that is because the thread is locking it.

    Now to get the multithreading working.

    Thanks very much for the fast and accurate help.

Similar Threads

  1. Qt Svg Module
    By onurozcelik in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2010, 06:47
  2. Zip module for Qt
    By arpspatel in forum Qt Programming
    Replies: 1
    Last Post: 28th October 2009, 06:34
  3. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18
  4. sending signals to ancestors
    By magland in forum Qt Programming
    Replies: 3
    Last Post: 6th October 2007, 11:51
  5. SQL-Module
    By phil_ in forum Newbie
    Replies: 7
    Last Post: 22nd January 2006, 16:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.