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?
↧