0

I wrote a library (DLL). Java code uses my DLL with JNA. In library I creates Qtimer by "new" in QThread derived class.

But, I have the following error:

ERROR: QTimer can only be used with threads started with QThread

How should I properly use QTimer to invoke from non-Qt thread without QApplication/QCoreApplication?

legotron
  • 61
  • 4
  • i think the obvious answer is that you cannot... but if you don`t trust me read here http://stackoverflow.com/questions/7450044/qtimer-can-only-be-used-with-threads-started-with-qthread –  May 25 '12 at 12:22
  • But, I don't have QCoreApplication. Should I need to create QCoreApplication instance? – legotron May 25 '12 at 12:26
  • 1
    quotation from the link i gave you: "To use a QTimer you need to have an event loop. You need to start the application event loop with QCoreApplication::exec()." –  May 25 '12 at 12:35
  • Where is I need to start app event loop? What place in code? – legotron May 25 '12 at 14:39
  • Given that ::exec() does not return, at a minimum you'd need to call it from a dedicated (Java) thread. – technomage May 25 '12 at 15:17

2 Answers2

1

Unfortunately QTimer requires QCoreApplication as far as I know. And also QCoreApplication must be instantiated in the thread that creates the first QObject I see. What I did was to instantiate QCoreApplication a new thread spawned using QtConcurrent::run, and instantiated my QObjects in there. I reported a trivial example here. QTimer now seems to be working correctly.

Luca Carlon
  • 9,546
  • 13
  • 59
  • 91
0

Take a look at QBasicTimer. I'm not 100% sure if this will work but at least the documentation doesn't mention QThreads and event loops at all (in contrast to the documentation of the QTimer class).

The drawback is that it's a bit more complicated (it calls timerEvent() reimplementation in your QObject, no signal-slot-connection), but if it's true, you can use it without an event loop.

leemes
  • 44,967
  • 21
  • 135
  • 183