share variable between classes [SOLVED]
-
-
If I subclass Edge in this way:
class Edge : public QGraphicsItem , public QObject { Q_OBJECT
I obtain the following errors:
C2039 'StaticMetaObject' is not a member of 'QGraphicItem'
C2039 'qt_metacast' is not a member of 'QGraphicItem'
C2039 'qt_metacall' is not a member of 'QGraphicItem'while if I do in this way:
class Edge : public QObject, public QGraphicsItem { Q_OBJECT
I have no error when I compile but when I run the program I obtain:
QObject::connect: No such signal MainWindow::length_COMPUTED( qreal ) in ..\MainWindow\MainWindow.cpp:31
QObject::connect: (sender name: 'MainWindow')
QObject::connect: (receiver name: 'MainWindow')The program works but it seems that the SIGNAL/SLOT functions do not happen
-
Your connect statement is wrong. Your signal should be emitted from an Edge object but you're trying to connect a signal from MainWindow ('this' below) but there is no such signal in your MainWindow.
this->connect(this, SIGNAL(length_COMPUTED( qreal ), this,SLOT(official_length_computation(qreal )));
-
Sorry if I bother you, but I undestood that but if I use:
this->connect(this, SIGNAL(length_COMPUTED( qreal ), this,SLOT(official_length_computation(qreal )));
in the my edge object I'm not able to see the SLOT function of MainWindow!
-
Your connect statement should be called in from your MainWindow as you have it, but the first parameter is the sender object which is not your MainWindow object but an Edge object. You should create an Edge object and use a pointer to that object as the first parameter of your connect statement.
-
@tmoranduzzo
I assume that you include Edge declaration in MainWindow.cpp.You can write something similar to:
Edge *myEdge = new Edge; connect ( Edge, SIGNAL(length_COMPUTED( qreal ), this, SLOT(official_length_computation(qreal ) ) );
The signal is in Edge class and the connect statement above allows connecting from myEdge (using the pointer) to the slot function pf MainWindow (using this pointer).
-
Thank you guys! I'm close to the final solution! Now the program compile and runs without error. The only problem is that it seem that there is no connection between the emission of length_COMPUTED and the slot official_length_computation because the program never enters in that function.
I declared myEdge and the connect function in this way:
test1 = new MyItem(); test2 = new MyItem(); Edge *myEdge = new Edge(test1,test2); this->connect(myEdge, SIGNAL(length_COMPUTED( qreal )), this, SLOT(official_length_computation( qreal)));
the returning value of connect is true so I guess that the connection is ok
After several attempts and after several Debug I saw that in the function adjust() the SIGNAL is never emitted even though the connection is ok.
-
Well, the question how adjust is called in your original design?
Since you have adjusted to inherit from QGraphicsObject it is emitting signals for different changes. Probably you have to connect an appropriate signal to trigger your adjust method (which has to be declared under slots then). Alternatively, you may need an intermediate slot method to be triggered which calls adjust.
-
The program calls correctly the function adjust() and in it it does all the required operation except for the emission of the signal. the function adjust is called two times in the code:
1: in edge.cppEdge::Edge(MyItem *sourceNode, MyItem *destNode) : arrowSize(10) { setAcceptedMouseButtons(0); source = sourceNode; dest = destNode; source->addEdge(this); dest->addEdge(this); adjust(); }
and in Myitem.ccp (the is the function to create a rect used as extreme of the edge.):
QVariant MyItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value){ qDebug()<<"myIteme change"; switch (change) { case ItemPositionHasChanged: foreach (Edge *edge, edgeList){ edge->adjust();} break; default: break; }; return QGraphicsItem::itemChange(change, value); }
I do not get exactly why even if the connection is ok the signal is not emitted. Anyway, I'll try as you suggested by invoking the adjust() with a SLOT
-
@tmoranduzzo
Depending on complexity of your application you may set a breakpoint in the debugger on the emit as well on the first statement in the slot method for tracking purposes. Not sure if this is possible in your application.Also you can use dumpObjectInfo() and dumpObjectTree() in a debug compilation for tracking if there should be still a connection when the emit is happening.
-
Please correct me if I'm wrong. The SIGNAL/SLOT concept is associated to a specific object right? In my case where during the execution of the process multiple object Edge are created I should create and connection for each object right? and consequently the declaration:
test1 = new MyItem(); test2 = new MyItem(); Edge *myEdge = new Edge(test1,test2); this->connect(myEdge, SIGNAL(length_COMPUTED( qreal )), this, SLOT(official_length_computation( qreal)));
that I did in the MainWindow.cpp is not enough because it connect the object myEdge that in the process is not use.
Am I completely wrong?
-
My reasoning was correct!!! I solved my problem! I added the connection in the code where the edge that I effectively use is created!
By Thinking right now it was a really stupid and obvious error.
Thank you very much for your help!
-
@tmoranduzzo
You are right. Connections are always between specific objects. Therefore, you need to repeat the connections each time you are defining a new instance.It looks like you have mastered the signal slot concept. After you got used to there are lots of possibilities.
Please mark your thread with [solved] in title line. This indicates to others that there might be a solution for them. Also upvoting of responses are appreciated.