Oren Sarid
Oren Sarid

Reputation: 153

Using Qt's signals in a non Qt application (C++)

I have a lot of existing code using Qt, and more specifically Qt signals and slots to time specific actions.

Now I need to use this code within a new application which is not a Qt application (and cannot be - I am writing a plugin to visual studio). Anyway - how can I get the existing code to actually intercept the signals and activate the relevant slots?

Do I need to somehow create a dummy Qt application? If so - how do I cause it to process the signals without becoming a blocking loop to the rest of my code?

Upvotes: 3

Views: 760

Answers (2)

dgsomerton
dgsomerton

Reputation: 115

This is a common problem when using ASIO and Qt together. The solution I have found is to make a Broker object, and run the Qt and ASIO loops on their own threads. The Broker is the target for emit calls to the ASIO message queue, and the emitter to the Qt message queue. Take care of the syncronisation/data copying in the Broker.

Upvotes: 0

László Papp
László Papp

Reputation: 53165

It seems that if you write something like this, the "Test" message is still printed even though there is no event loop, so this could be a clue:

#include <QObject>
#include <QCoreApplication>
#include <QDebug>

class MyClass : public QObject
{
    Q_OBJECT
    public:
        explicit MyClass(QObject *parent) : QObject(parent) {}
        void testMethod() { emit testSignal(); }

    signals:
        void testSignal();

    public slots:
        void testSlot() { qDebug() << "Test"; }
};

#include "main.moc"

int main(int argc, char **argv)
{
    // QCoreApplication coreApplication(argc, argv);
    MyClass myObject(0);
    QObject::connect(&myObject, SIGNAL(testSignal()), &myObject, SLOT(testSlot()));
    myObject.testMethod();
    // return coreApplication.exec();
    return 0;
}

This way, you would still need Qt, but you could avoid having a "blocking" event loop. However, it might be simpler to just rearrange the code from the signal-slot layering to direct calls, depending on how many direct calls you would need to do for a signal emitted.

Upvotes: 1

Related Questions