PDA

View Full Version : Problems with signals and slots [probably c++ issue]



Nayrb
26th April 2012, 05:18
Hi guys, I am making and app to allow users to make some simple network diagrams. The app will allow users to draw simple lines and drag and drop devices.
I have reimplemented the following classes: myScene, myView, myCable, myLine and myRouter. I notice that I mostly instantiate my other objects in myScene and the GUI interacts mostly with myScene. I was wondering if I am doing it wrong without having any inheritance as I frequently encounter situation where I need to connect signal and slot from my other classes to the mainwindow. It got me thinking if my other classes should inherits from myScene class.

E.g. I reimplemented the mouseDoubleClicked() in myCable which is a subclass of QGraphicsLineItem, I wanted it to emit a signal to update a QTreeWidget but I it has not been instantiated and I could not do it. I only instantiate it in myScene class.

Thank you all in advance for any advices.

P.S. I apologise in advance if the question is too simple, I am new to C++ and also QT Programming.

zgulser
26th April 2012, 12:43
What is the thing that's not instantiated? Is it your "myCable" class?

If so, please post the code where you think you instantiate the myCable object..

You also need to use setScene() for your myCable item in order to receive mouse events.

T_h_e_o
26th April 2012, 13:42
I would not dare to say that I fully understand your problem, but from what I do understand I would say that this more a design issue than a Qt or C++ issue. Before one starts to implement it is always advisable to thoroughly think about the possible designs, the pros and cons of each alternative, and then find the best solution. I understand that this is no answer to your problem, but maybe a tip for future projects.

Now, in order for us to help you I think you need to elaborate a bit more. Which object is not yet instantiated? The QTreeWidget? And at what time *will* it be instantiated?

Nayrb
1st May 2012, 17:34
First of all, I did most of the UIs using the Qt Designer. Secondly, two of my classes myScene and myView were instantiated in MainWindow classes and the signals and slots are mostly connected here in MainWindow constructor. The other classes like myCable and myLine etc are only instantiated within myScene class and I notice I could not connect them to the widgets created using the Qt Designer or calling any functions from myScene. I think its a design problem and I hope someone can give me some advise on it.

ChrisW67
2nd May 2012, 00:25
E.g. I reimplemented the mouseDoubleClicked() in myCable which is a subclass of QGraphicsLineItem,
There is no function mouseDoubleClicked() to reimplement. Perhaps you mean QGraphicsItem::mouseDoubleClickEvent(). Having that function emit a signal is trivial if your item also inherits from QObject somehow. Emitting this signal does not depend on knowing anything about the receiver, which is the entire point of signal/slot loose coupling.


I wanted it to emit a signal to update a QTreeWidget but I it has not been instantiated and I could not do it. I only instantiate it in myScene class.
What is "it"?

First "it": The graphics item?
Second "it": Some other object? Perhaps a desired receiver of the signal from the first "it"?
Third "it": The sending of a signal or the connection of a signal to a slot?
Fourth "it": Some other object possibly related to the second "it"?

Clearly you cannot emit a signal without having instantiated an object, so the first"it" and the second and fourth "it" are not the same object. You need instance of both the sender and receiver in order to form a signal/slot connection. Any place in your code that both objects exist is a candidate for placing a connect() call.


P.S. I apologise in advance if the question is too simple, I am new to C++ and also QT Programming.
The question is not too simple just too vague.

Nayrb
2nd May 2012, 04:48
I apologise for being too vague.

The function is the QGraphicsScene::mouseDoubleClickEvent().

1) The first "it" is the object, I wanted to update the QTreeWidget whenever the object is being double-clicked.
2) The second "it" refers to the same object which has not been instantiated.
3) The third "it" refers to the connection that I wanted to connect between the myCable object and myScene object, however I only instantiate the myCable object in one of myScene methods.
4) The fourth "it" refers to the myCable object which I have instantiate in one of the myScene methods.

E.g. myScene.cpp

void myScene::myCableToolsDrawSlot(int x, int y, int x2, int y2)
{
myCable cable = new myCable();
...
...
}

ChrisW67
2nd May 2012, 09:13
:confused: If "the object" (presumably intended to be a QGraphicsItem) has not been instantiated then it cannot be double-clicked. Your description is not doing you any favours.

If you have implemented a double-click handler on the scene then you can determine the scene objects that are at the double-click position with QGraphicsScene::itemAt() or QGraphicsScene::items(). Your scene can emit any signal it likes to update a QTableWidget. There is no need for a connection between an item in the scene and the scene (graphics items are not QObjects and cannot emit signals anyway). There's no need for a connection between the graphics item and and external table widget.

When your scene creates a new item the scene can tell the table widget about it if need be.