PDA

View Full Version : QMainWindow pointer



^NyAw^
18th November 2006, 16:54
Hi,
Is there any way to get the MainWindow pointer on any part of the code?

I need to get the MainWindow pointer to make connections into other objects. These objects are created dinamically by user operation and I want to connect a SIGNAL from the object to the SLOT of the MainWindow.

I need this because I have to make that the GUI Thread paint Images that the objects create.

Thanks,

jacek
18th November 2006, 17:04
There is no "main" window in Qt4. If your application has one main window, you can always add a static method that returns a pointer to it (read about singleton pattern (http://en.wikipedia.org/wiki/Singleton_pattern)).

Where do you create those objects?

^NyAw^
18th November 2006, 17:19
Hi,
There are objects that are created in the main window. Then, this objects have more objects inside them, ...
for example:


classA
{
classB* pB;
}

classB
{
classC* pC;
}

classC
{
classD* pD;
}

classD
{
/*Here I want the pointer to the main window to connect a SIGNAL from classD and
a SLOT from main window because I need to paint Images and only the GUI Thread is
able to do it.
Do you remember the other thread that we are talking about Producer and Consumer
that my application hangs because another Thread tryies to paint Images.
*/
}


So, have I to pass the pointer to the widget along all the classes or there is a way to get the main widget pointer anywhere in the code?

Thanks,

jacek
18th November 2006, 17:54
There are objects that are created in the main window. Then, this objects have more objects inside them, ...
for example:
It's a bit to abstract example to propose any other solutions, so either you will have to pass the pointer or, if you have only one main window instance, you can use the singleton pattern.

^NyAw^
18th November 2006, 18:00
Ok,
Will try it.

Thanks for your time, :)

wysota
18th November 2006, 18:08
If your objects are QObjects and are somehow attached to the application object, you can use QObject methods for finding children, but it is a bit complicated and slower than simply storing the pointer somewhere as Jacek already suggested.

jpn
18th November 2006, 18:36
Signal chaining (a signal connected to another signal) could also be one possibility.



class MainWindow
{
public:
MainWindow(...)
{
SomeClass* some = new SomeClass(...);
connect(some, SIGNAL(somethingHappened()), this, SLOT(doSomething());
}

private slots:
void doSomething()
{
...
}
}

class SomeClass
{
public:
SomeClass(...)
{
AnotherClass* another = new AnotherClass(...);
connect(another, SIGNAL(somethingHappened()), this, SIGNAL(somethingHappened()));
}

signals:
void somethingHappened();
}

class AnotherClass
{
signals:
void somethingHappened();
}

^NyAw^
19th November 2006, 15:28
Hi,
I'm having trouble with passing the pointer to othre classes. When I include the main window header in a class that is included in the main window header there are errors compiling. I think this is because one includes the other and the other includes the one, so ... there is a infinite bucle including that the compiler detects.

mmm, is there anyway to force a class (main window) to response a SIGNAL without making the connections? I want to kwon if there is a way to force the main window to response the SIGNAL "X" with the SLOT "slotX" without having to tell who is the sender of the signal.

Thanks,

jpn
19th November 2006, 15:55
I'm having trouble with passing the pointer to othre classes. When I include the main window header in a class that is included in the main window header there are errors compiling. I think this is because one includes the other and the other includes the one, so ... there is a infinite bucle including that the compiler detects.

Circular dependency (http://en.wikipedia.org/wiki/Circular_dependency)



mmm, is there anyway to force a class (main window) to response a SIGNAL without making the connections? I want to kwon if there is a way to force the main window to response the SIGNAL "X" with the SLOT "slotX" without having to tell who is the sender of the signal.
You must pass a valid sender and a valid receiver to the connect-statement. Did you read the signal chaining example?

^NyAw^
19th November 2006, 16:20
Hi,
Yes, I have readed it. I'm trying to put down it now. It's a complicated task because I have to emit 5 different signal types, and I have a lot of classes that have to emit this signals and response with the properly slots. Also I have some derived classes that are the first that emit these signals, so ... working on it.

I have tryied to make the connections between 2 classes like your example and I have a question. I have readed about the 2 connection types "Qt::DirectConnection" and "Qt::QueuedConnection". If I use the "Qt::QueuedConnection", the connection is not made.

^NyAw^
19th November 2006, 16:24
Hi,
Now I'm reading about Circular Dependency and I think that it wolud be a little simple way to resolve it. I'm gonna try to use forward declarations.

I will tell you if I solve this.

Thanks,

^NyAw^
19th November 2006, 18:52
Hi,
I'm having trouble.

Now I'm able to make the connection of the main window and the class. "connect" returns true, but there is no response to the SIGNAL with the SLOT. In the connect call I had to convert the main window pointer to QObject pointer. I checked if the pointer points to the main window object, it's ok.

The SIGNAL is emmited, but nothing happens in the SLOT, it is not called. The object that emmits the SIGNAL is in other Thread. I've tryied to use QueuedConnection but it returns me false. Using DirectConnection retruns me true but I think that I have to use QueuedConnection emmiting SIGNALS from another Thread to the Main Thread.

I'm passing a non Qt class type with the SIGNAL and reciving it with the SLOT so I have called
"qRegisterMetaType<HImage>("HImage");" from the main window constructor as the Mandelbrot example does. It returned me an integer (256).

jacek
19th November 2006, 18:58
Now I'm able to make the connection of the main window and the class. "connect" returns true, but there is no response to the SIGNAL with the SLOT. In the connect call I had to convert the main window pointer to QObject pointer. I checked if the pointer points to the main window object, it's ok.
That conversion shouldn't be necessary, unless you are missing #include directive.

Are there any messages on the console? Remember that under windows you have to add CONFIG += console to your .pro file.

^NyAw^
19th November 2006, 19:02
Hi,
I'm thinking that I'm doing something wrong.
The objects that connect with the main window are created from the main thread, then I created the Thread that would have a pointer to these objects. The code of the objects are called from this Thread, but the objects are created in the main thread.

Could be this the error? So, if yes, I have to create the objects into the Thread?

jacek
19th November 2006, 19:06
Could be this the error? So, if yes, I have to create the objects into the Thread?
No, if you use queued connections.

^NyAw^
19th November 2006, 19:20
Hi,
Ok, so I can pass a pointer of the Object to the Thread.
The connect still returns me false with QueuedConnection.

I'm calliing the connect function out of the Thread. When I create the object that would emmit the signal, the connection is done by this object, not by the Thread.



classA
{
classB* pB;
}

classB
{
classD* pD;
pD = new classD(.....);
classC* pCThread = new classC(pD); //The constructor of the classC has a pointer to the classD

}

classC : QThread
{
//Here it have a pointer to the classD object. This Thread will call the classD function to make jobs
}

classD
{
//Here I have the pointer to the main window, so I try to connect a SLOT from it and a SIGNAL from this classD
}



I'm not sure if I explained it clearly

Thanks,

jacek
19th November 2006, 19:29
The connect still returns me false with QueuedConnection.
For the second time: Are there any messages on the console?