Qobject::connect without QApplication
-
Hi All,
As far as I understand, each QThread has an internal event loop.
So in theory, I might be able to launch some QThreads and use "connect" for thread events.
However, I get the message:
QEventLoop: Cannot be used without QApplication
But in my case, I'd like my object to be useful even to non-qt applications.I wrote a very simple example to illustrate what I want to do
@#include <iostream>
#include <QThread>
#include <QObject>
#include <QString>class MyProducer : public QObject
{
Q_OBJECTpublic slots:
void produce() { emit message_ready("QT"); }
signals:
void message_ready(QString);
};class MyConsumer : public QObject
{
Q_OBJECTsignals:
void finished();
public slots:
void consume(QString input_string)
{
std::cout << "got string: " << input_string.toStdString() << std::endl;
emit finished();
}
};class MyLibrary
{
QThread producer_thread_;
QThread consumer_thread_;
MyConsumer consumer_;
MyProducer producer_;public:
void init()
{
// set up my qobjects
consumer_.moveToThread(&consumer_thread_);
producer_.moveToThread(&producer_thread_);
// start producer when producer thread starts
producer_.connect(&producer_thread_,SIGNAL(started()),SLOT(produce()));
// using connect for thread communications
consumer_.connect(&producer_,SIGNAL(message_ready(QString)),SLOT(consume(QString)));
// stop the threads on finish
consumer_thread_.connect(&consumer_,SIGNAL(finished()),SLOT(quit()));
producer_thread_.connect(&consumer_,SIGNAL(finished()),SLOT(quit()));
}void process_all()
{
// launch threads
consumer_thread_.start();
producer_thread_.start();
// wait for threads to terminate
producer_thread_.wait();
consumer_thread_.wait();
}
};using namespace std;
int main()
{
MyLibrary library;
library.init();
library.process_all();
cout << "Hello World!" << endl;
return 0;
}#include "moc_main.cpp"
@calling the above "main.cpp" I can create the moc code and compile with
@moc main.cpp > moc_main.cpp@
@g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -o main.o main.cpp
g++ -Wl,-O1 -o qttest main.o -L/usr/lib/x86_64-linux-gnu -lQtGui -lQtCore -lpthread@If I run this program, I get the error.
@./qttest
QEventLoop: Cannot be used without QApplication
QEventLoop: Cannot be used without QApplication
@I have done some online search where the replies to this kind of question were always of the kind: "you don't want to use QThreads if you're not writing a QApplication". But I'd like to understand why that error message comes about, why is a QApp really required to run a QThread event loop, and what would be the correct QT way if I want to use QThreads in a library (or if this is really a bad idea).
Thanks!
-
i think this may be because of the QObject relies on QApplication's initialisation of various elements used by various features(such as signals and slots) although i can't say for sure