Results 1 to 20 of 38

Thread: Need Qt SerialPort class example using signals and slots WITHIN A THREAD

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #28
    Join Date
    Feb 2010
    Posts
    96
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 5 Times in 5 Posts

    Default Re: Need Qt SerialPort class example using signals and slots WITHIN A THREAD

    That doesn't work for me. I only receive the MainThread print statement in my terminal.

    edit: wysota, here is some modified code and the output. Even after the Object class is moved to the Thread class I still get the main thread's ID ... but for me the signal is not being caught by the Object's reportThread method.

    Qt Code:
    1. from PyQt4 import QtCore
    2. import sys, time
    3.  
    4. class Object(QtCore.QObject):
    5. def __init__(self):
    6. QtCore.QObject.__init__(self)
    7.  
    8. def reportThread(self):
    9. print self, (QtCore.QThread.currentThreadId())
    10.  
    11. class Thread(QtCore.QThread):
    12. def __init__(self):
    13. QtCore.QThread.__init__(self)
    14.  
    15. def reportThread(self):
    16. print self, (QtCore.QThread.currentThreadId())
    17.  
    18. app = QtCore.QCoreApplication(sys.argv)
    19. thr = Thread()
    20. obj = Object()
    21. obj.moveToThread(thr)
    22. print obj, obj.thread(), obj.thread().currentThreadId()
    23. thr.start()
    24.  
    25. timer = QtCore.QTimer()
    26. print ( "MainThread "+str(QtCore.QThread.currentThreadId()) )
    27. timer.timeout.connect(obj.reportThread)
    28. timer.timeout.connect(thr.reportThread)
    29. timer.start(2000)
    30.  
    31. t2 = QtCore.QTimer()
    32. t2.timeout.connect(app.quit)
    33. t2.start(10000)
    34. app.exec_()
    To copy to clipboard, switch view to plain text mode 

    output:
    Qt Code:
    1. <__main__.Object object at 0xabd2f8> <__main__.Thread object at 0xabd270> 140273089054464 MainThread 140273089054464
    2. <__main__.Thread object at 0xabd270> 140273089054464
    3. <__main__.Thread object at 0xabd270> 140273089054464
    4. <__main__.Thread object at 0xabd270> 140273089054464
    5. <__main__.Thread object at 0xabd270> 140273089054464
    6. <__main__.Thread object at 0xabd270> 140273089054464
    To copy to clipboard, switch view to plain text mode 
    Last edited by prof.ebral; 11th December 2012 at 22:54.

Similar Threads

  1. Signals Slots and Class Pointers
    By Atomic_Sheep in forum Newbie
    Replies: 18
    Last Post: 7th September 2012, 09:08
  2. [Signals & Slots] Custom class' signal not detected
    By Mr_Cloud in forum Qt Programming
    Replies: 5
    Last Post: 26th July 2012, 10:35
  3. Thread safety with signals and slots
    By blooglet in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2012, 15:03
  4. Are signals and slots thread safe?
    By Cruz in forum Qt Programming
    Replies: 12
    Last Post: 21st April 2011, 14:57
  5. Access a class without using Signals/Slots
    By impeteperry in forum Qt Programming
    Replies: 5
    Last Post: 10th January 2010, 11:14

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.