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.
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.
Signal chaining (a signal connected to another signal) could also be one possibility.
Qt Code:
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(); }To copy to clipboard, switch view to plain text mode
J-P Nurmi
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,
Òscar Llarch i Galán
Circular dependency
You must pass a valid sender and a valid receiver to the connect-statement. Did you read the signal chaining example?
J-P Nurmi
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:irectConnection" and "Qt::QueuedConnection". If I use the "Qt::QueuedConnection", the connection is not made.
Òscar Llarch i Galán
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,
Òscar Llarch i Galán
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).
Òscar Llarch i Galán
Last edited by jacek; 19th November 2006 at 20:04. Reason: spelling error
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?
Òscar Llarch i Galán
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.
Qt Code:
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 }To copy to clipboard, switch view to plain text mode
I'm not sure if I explained it clearly
Thanks,
Last edited by ^NyAw^; 19th November 2006 at 20:22. Reason: updated contents
Òscar Llarch i Galán
Bookmarks