Results 1 to 2 of 2

Thread: The threaded signal/slot mechanism

  1. #1
    Join Date
    Mar 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default The threaded signal/slot mechanism

    Hello everyone

    I've encountered a problem while trying to implement a polling function to fetch data from a serial port.
    Qt Version is 4.3.4 and I'm using QDevelop as IDE. When I hit compile, I get a debug and a release version.
    At first, I tried to set up a timer and connect its signal to the polling-method. This did work, but only in the release version. It doesn't work in the debug version nor in a static version. This seems to indicate that I created some kind of half-working solution which I just hate.
    So I started reading and searching for this problem on the internet and found that setting up a timer only works when it is inside a eventloop. Since my polling class is connected to the mainWindow-class via pointer, I figured this could be the problem (Is it?).
    Therefore I created a QThread-inheritance(TimerThread) to contain the polling timer. This worked eventually, but still only in the release-version of my project.
    Hmpf. There still seems to be something about these signals/slots I just don't get.

    My goal is having a class that handles one serial port via qextserialport and fetches data from it on a fixed timetick completely on its own. Occasionally, it gets called and returns a chunk of data. And it should be able to be included everywhere by setting up a pointer with the type of this class and construct an object on it.

    This is my implementation of the thread-class whick inherits QThread:
    Qt Code:
    1. #include "TimerThread.h"
    2. //******************************************************************************
    3. TimerThread::TimerThread(SerialPort *port)
    4. {
    5. this->port=port;
    6. }
    7. //******************************************************************************
    8. void TimerThread::run()
    9. {
    10. timer=new QTimer(this);
    11. timer->start(30);
    12. connect(timer, SIGNAL(timeout()), port, SLOT(fetchData()));
    13. exec();
    14. }
    15. //******************************************************************************
    To copy to clipboard, switch view to plain text mode 

    So, the object of TimerThread knows the object it has been created by and uses this knowledge to connect the timer-signal to the method of its creator that collects data from the serial port.

    This class-object gets created like that:

    Qt Code:
    1. timerPort = new TimerThread(this);
    To copy to clipboard, switch view to plain text mode 

    Would really appreciate any help because I totally ran out of ideas.

  2. #2
    Join Date
    Mar 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: The threaded signal/slot mechanism

    Solved it after reading
    http://www.qtcentre.org/forum/f-qt-p...ots-12721.html


    I changed my TimerThread to have its own pseudo-polling method. This method can get connected just fine and does nothing except calling the actual polling-function.

    Qt Code:
    1. #include "TimerThread.h"
    2. //******************************************************************************
    3. TimerThread::TimerThread(SerialPort *port)
    4. {
    5. this->port=port;
    6. }
    7. //******************************************************************************
    8. void TimerThread::run()
    9. {
    10. timer=new QTimer(this);
    11. timer->start(300);
    12. connect(timer, SIGNAL(timeout()), this, SLOT(platform()));
    13. exec();
    14. }
    15. //******************************************************************************
    16. void TimerThread::platform()
    17. {
    18. port->fetchData();
    19. }
    20. //******************************************************************************
    To copy to clipboard, switch view to plain text mode 

    Ok then... Now my question is: Why did it work with the release-version?

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.