PDA

View Full Version : QApplication instance



nile.one
5th October 2007, 05:01
I'm developing some Qt project.
I have the common Qt application (exe). and it attaches plugin - Qt library (dll).
Both are GUI.
So i have to create QApplication instances both in exe and dll.

Because there must be only one QApplication object, I've tried to create second instance in QThread.
But when I create second instance of QApplication in dll, this causes error

ASSERT failure in QCoreApplication: "there should be only one application object"

So i tried to check if qApp exists and create instance of QApplication only if it's null.
So then i have error

ASSERT failure in QWidget: "Widgets must be created in the GUI thread."

Does anybody know how to solve this problem?

jpn
5th October 2007, 09:45
You might have to slightly revise the architecture. You should not instantiate a QApplication in a library/plugin at all. What is the reason for doing so?

nile.one
5th October 2007, 10:25
I need to implement Qt widgets in plugin. So using widgets requires Qt GUI thread. So the problem is I can't put my widget to GUI thread without creating QApplication

In addition, I get it in debug mode: in qthread's run() I create local instance of QApplication and destroy it after thread working end. But in release i have messages in output:


QObject::startTimer: timers cannot be started from another thread
QObject::killTimer: timers cannot be stopped from another thread

and after closing the thread application exits itself :(


The thread 'Win32 Thread' (0x9d4) has exited with code 0 (0x0).
QWidget: Must construct a QApplication before a QPaintDevice
The thread 'Win32 Thread' (0xc8c) has exited with code 1 (0x1).
The thread 'Win32 Thread' (0x778) has exited with code 1 (0x1).
The thread 'Win32 Thread' (0xc78) has exited with code 1 (0x1).
The program '[2740] deposits_repayment.exe: Native' has exited with code 1 (0x1).

marcel
5th October 2007, 11:00
Use the QApplication from the application that loads the plugins.

wysota
5th October 2007, 11:00
Why not attach to the application QApplication object? It's available as a global pointer qApp and furthermore you can change the plugin interface so that either the application object pointer is passed to the plugin or a pointer to the widget created in the plugin is passed back to the application object and then it takes control over the newly created widget (that's what Qt Designer does by the way).

nile.one
5th October 2007, 11:12
Yes, of course, I can get qApp pointer. But I don't know how to pass it to the QWidget.
Could you give me an example, please?
Simple instantiating QWidget with having qApp causing error


ASSERT failure in QWidget: "Widgets must be created in the GUI thread."

nile.one
5th October 2007, 11:19
There is code block of main QThread thread in plugin where i'm trying to instantiate QWidget (QDialog)

I understand, that code is incorrect and even more maybe stupid, so I need help :o


void MainThread::run()
{
if (!qApp)
{
int argc=0;
char** argv=0;
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(true);

Gui gui;
gui.show();
gui.job();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
a.exec();
}
else
{
Gui gui;
gui.show();
gui.job();
}
}

marcel
5th October 2007, 11:41
But it is not clear what are you're trying to do...
Why are you starting an event loop in a plugin?
The plugin should use the host applciation's event loop, not create its own.

Ant what's with the thread?

nile.one
5th October 2007, 11:50
in the example i get error
ASSERT failure in QWidget: "Widgets must be created in the GUI thread."

So, what's the solution? not to implement GUI in thread or what?
But if i want to implement plugin's GUI in independent thread in case of not blocking the application while long-time work of plugin.

nile.one
5th October 2007, 12:06
ok, finally i got it working. i've chosen the solution to run the Qt GUI not in independent thread, i'll use the threads only in core classes.