9

I'd like to create a library built on top of QTcpServer and QTcpSocket for use in programs that don't have event loops in their main functions (because the Qt event loop is blocking and doesn't provide enough timing resolution for the real-time operations required).

I was hoping to get around this by creating local event loops within the class, but they don't seem to work unless I've called app->exec() in the main function first. Is there some way to create local event loops and allow for signal/slot communication within a thread without having an application level event loop?

I've already looked at Is there a way to use Qt without QApplication::exec()? but the answer doesn't help because it seems like the solution adds a local event loop but doesn't remove the application loop.

2
  • spawn a new thread that has the QApplication::exec() and keep it alive as the "main thread"? Jan 6, 2015 at 15:17
  • I'll give it a try. The only downside is that even if works it would probably preclude me from using the library in the future with actual Qt applications as you certainly can't have two calls to QApplication::exec Jan 6, 2015 at 15:19

1 Answer 1

11

You can create the instance of the QCoreApplication in a new thread in the library. You should check to create only one instance of it, That's because each Qt application should contain only one QCoreApplication :

class Q_DECL_EXPORT SharedLibrary :public QObject    
{
Q_OBJECT
public:
    SharedLibrary();

private slots:

    void onStarted();

private:
    static int argc = 1;
    static char * argv[] = {"SharedLibrary", NULL};
    static QCoreApplication * app = NULL;
    static QThread * thread = NULL;
};


SharedLibrary::SharedLibrary()
{
    if (thread == NULL)
    {
        thread = new QThread();
        connect(thread, SIGNAL(started()), this, SLOT(onStarted()), Qt::DirectConnection);
        thread->start();
    }
}
SharedLibrary::onStarted()
{
   if (QCoreApplication::instance() == NULL)
   {
       app = new QCoreApplication(argc, argv);
       app->exec();
   }
}  

This way you can use your Qt shared library even in non Qt applications.

6
  • 1
    and maybe synchronize on checking and starting the loop Jan 6, 2015 at 15:21
  • 3
    This deserves an extra +1 for the check on QCoreApplication::instance() Jan 6, 2015 at 15:24
  • The QCoreApplication should be created on the main thread. See stackoverflow.com/questions/27963697/…
    – Alex Wade
    Feb 5, 2022 at 0:56
  • 2
    @AlexWade The post you mentioned does not tell QCoreApplication should be created on main thread. All that is needed to fix the warning is SharedLibrary not inheriting from QObject and creating QCoreApplication instance in constructor.
    – Nejat
    Feb 5, 2022 at 5:52
  • @Nejat right. And no other QObjects should be created prior to instantiating QCoreApplication. Instead of using QThread, std::thread can be used to create the main thread.
    – Alex Wade
    Feb 5, 2022 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.