Results 1 to 20 of 38

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

    Wysota ... the Object doesn't even execute the reportThread method in your code, nor in mine. I'm trying to point that out to you. Like I said, you have a hard time reading.

    Here ... I will quote my post:
    "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."

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

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

    Quote Originally Posted by prof.ebral View Post
    Wysota ... the Object doesn't even execute the reportThread method in your code, nor in mine. I'm trying to point that out to you. Like I said, you have a hard time reading.
    I understand that it doesn't work for you, I don't know why. It works for me just fine and I'm sure it works for many other people because it is intended to work that way.

    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."
    You get the main thread ID's because you connect the timer's signal to the slot of an object that lives in main thread.

    I just tried running the code you posted but it bails out with a syntax error (I'm using Python 3):

    text Code:
    1. File "./code.py", line 11
    2. print self, (QtCore.QThread.currentThreadId())
    3. ^
    To copy to clipboard, switch view to plain text mode 

    After fixing those syntax errors (by putting print arguments in parenthesis) and running the code, I get this:

    <__main__.Object object at 0x7fa7c3b50b00> <__main__.Thread object at 0x7fa7c3b50a70> 140358524008192
    MainThread 140358524008192
    <__main__.Thread object at 0x7fa7c3b50a70> 140358524008192
    <__main__.Object object at 0x7fa7c3b50b00> 140358466680576
    <__main__.Thread object at 0x7fa7c3b50a70> 140358524008192
    <__main__.Object object at 0x7fa7c3b50b00> 140358466680576

    You can see two slots are called -- one in Object (with thread id 576) and the other one in Thread (with thread id 192). That's the correct behaviour as described in Qt docs (PyQt can't possibly work differently because it's just a wrapper over Qt C++ classes). I don't know why you're getting no output for Object, the most obvious reason is that the worker thread is not started (or its event loop is not running).

    A side note: I checked with Python2.7 and it works the same way as with Python3:
    (<__main__.Object object at 0x7fa08aaf7d40>, <__main__.Thread object at 0x7fa08aaf7cb0>, 140327479097088L)
    MainThread 140327479097088
    (<__main__.Thread object at 0x7fa08aaf7cb0>, 140327479097088L(<__main__.Object object at 0x7fa08aaf7d40>, 140327422002944L)
    )
    (<__main__.Thread object at 0x7fa08aaf7cb0>, 140327479097088L)
    (<__main__.Object object at 0x7fa08aaf7d40>, 140327422002944L)
    (<__main__.Thread object at 0x7fa08aaf7cb0>, 140327479097088L)
    (<__main__.Object object at 0x7fa08aaf7d40>, 140327422002944L)
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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

    Quote Originally Posted by wysota View Post
    You can see two slots are called -- one in Object (with thread id 576) and the other one in Thread (with thread id 192). That's the correct behaviour as described in Qt docs (PyQt can't possibly work differently because it's just a wrapper over Qt C++ classes). I don't know why you're getting no output for Object, the most obvious reason is that the worker thread is not started (or its event loop is not running).
    That is why I subclassed it. The thread starts. You can modify the run or start methods and determine that if you want. I don't know why either.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

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

    Quote Originally Posted by prof.ebral View Post
    The thread starts.
    How do you know that? There is no code in your program that verifies if the thread is running.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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.