Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThread, execute slot without signal
QtWS25 Last Chance

QThread, execute slot without signal

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 5.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    ByteWight
    wrote on last edited by
    #1

    I have been researching about QThreads, and have found out that the best way to use thread is to inherit a QObject and then move that to another thread. Signals are important, because with a queued connection, I don't have to worry about locking anything.

    Because I am using Qt5, I would like to take full advantage of speed. I'm aware of using function pointers to connect, and I would rather that than using invokeMethod. Therefore, I created a signal in my QObject that calls the slot.

    @class Output : public QObject
    {
    Q_OBJECT

    public:

    Output()
    {
        connect(this, &Output::postMessage, this, &Output::message, Qt::QueuedConnection);
    }
    ~Output() {}
    

    public slots:

    void message()
    {
        qDebug() << "Happened in thread " << this->thread();
    }
    

    signals:

    void postMessage();
    

    };@

    And then add it to the queue by:
    @Output *output = new Output();
    output->moveToThread(tread);
    emit ouput->postMessage();@

    Is my approach good? Will it be faster than invokeMethod since it uses function pointers instead of using strings to get the function? Is there a better way?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      emitting a signal from another class is wrong. The signals are emitted from the class that declares them.

      The way of using QThreads by either inheritance or using a worker object is always case specific. You have to carefully choose which solution is the best based on what you will do in your thread.

      Proper locking of resources has to be always analyzed. Qt won't protect automagically for you the accesses made to a shared piece of data.

      I would strongly encourage you to read again the documentation about signals and slots and also threading, look extensively in the examples of Qt to see what you can do.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        For examples you can have a look at

        • "QThread problem":http://qt-project.org/forums/viewthread/26067/
        • "My tutorial on how to properly use QThreads":http://qt-project.org/forums/viewthread/14806/
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved