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. Qobject::connect without QApplication
Forum Updated to NodeBB v4.3 + New Features

Qobject::connect without QApplication

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 6.1k Views 1 Watching
  • 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
    breakthru
    wrote on 25 Jun 2012, 12:45 last edited by
    #1

    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_OBJECT

    public slots:
    void produce() { emit message_ready("QT"); }
    signals:
    void message_ready(QString);
    };

    class MyConsumer : public QObject
    {
    Q_OBJECT

    signals:
    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!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raaghuu
      wrote on 25 Jun 2012, 15:01 last edited by
      #2

      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

      1 Reply Last reply
      0

      1/2

      25 Jun 2012, 12:45

      • Login

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