You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Friday, June 17th 2011, 5:04pm

who executes a Method called from a slot?

Hi everybody!
I'm having some trouble with my QT signal - slot implementation. Maybe i did something wrong, because i'm new to this topic.
I have the following problem:
I implemented a class, derived from QThread. This class emit's a Signal when a DialogBoxClass shall be instantiated, which is of the QDialog type.
The workflow is the following:
I start my thread an do some stuff - then i emit a signal which is connected to a slot of the class who started the thread.
In this method i call a method which instantiates a dialogbox class which is derived from QDialog.

I get the following error: "Widgets must be created in the GUI thread."

This dialog creating thing worked before i called this from a "slot-method" which reacts on a signal from another thread.

I'm asking my self, which thread actually calls the method which tries to instantiate the QDialog derived class.

Does anybody has an idea what's going wrong?

2

Friday, June 17th 2011, 10:18pm

Is the slot in the main thread or your new thread? I think that's the problem.

3

Monday, June 20th 2011, 12:32pm

The slot is created in the main thread - i got this idea too, but i'm quite sure that the class and it's slot (which is declared private) are instantiated by the main thread.

4

Monday, June 20th 2011, 3:08pm

ok, i figured out somerthing more:
i got the following workflow:

workerthread: emit SIGNAL
main-Thread: active slot
main thread initialises an instance of a class which is derived from QDialog.
following the constructor of my DialogBox Class:

Source code

1
CDialogBox::CDialogBox( QWidget* pParent) : QDialog(pParent, Qt::FramelessWindowHint )



it's initialised like the following:

Source code

1
CDialogBox dialogBox(this);dialogBox.exec();


So i guess the "this" pointer parameter is the cause of my problem, because it seems to be some kind of undefined but i got actually no idea how to solve this.

The crash occures within here:

Source code

1
QDialog(pParent...

Junior

Professional

  • "Junior" is male

Posts: 1,622

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

5

Tuesday, June 21st 2011, 1:11pm

If I'm reading this thread correctly and you are trying to launch a widget from within a class that is a sub-class of QThread, than please note:

Quoted

Each QThread can have its own event loop. You can start the event loop by calling exec(); you can stop it by calling exit() or quit(). Having an event loop in a thread makes it possible to connect signals from other threads to slots in this thread, using a mechanism called queued connections. It also makes it possible to use classes that require the event loop, such as QTimer and QTcpSocket, in the thread. Note, however, that it is not possible to use any widget classes in the thread.
ref: QThread

Most likely anything not referenced back to QtGui module will probably work because it doesn't have dependency to the main gui event loop.

If the QDialog is being created in a non-sub qthread class and I've got that wrong from the posts. Then maybe something like this:

{ CDialogBox *dialogBox = new CDialogBox dialogBox(this);dialogBox->exec(); } // parented
{ CDialogBox dialogBox(0);dialogBox.exec(); } // non-parent
{ CDialogBox dialogBox();dialogBox.exec(); } // non-parent and defined default is 0 in declaration

6

Tuesday, June 21st 2011, 1:25pm

Thanks for the advice. I found my mistake. I connected the signals from the QThread instance to the slots of the man(gui) thread using a direct connection, what acutally caused this kind of error.

Thanks a lot!