c++ - share variable between classes QT -


i created application exploits 3 different classes of objects. have first class "all interface , main code" second 1 simple rect object , last 1 edge between different rect (more or less in elasticnode example ). every time user moves rect edge modified , need seed "length" main class.

here code of movement:

void edge::adjust() {  qlinef line(mapfromitem(source, 0, 0), mapfromitem(dest, 0, 0));  preparegeometrychange();  qpointf edgeoffset(5, 5); sourcepoint = line.p1() + edgeoffset; destpoint = line.p2() + edgeoffset;  length_reff = sqrt((source->x()-dest->x())*(source->x()-dest->x())+(source->y()-dest->y())*(source->y()-dest->y()));  emit length_computed(length_reff); //here have send lenght_ref variable mainwindow class } 

i tried implement signal/slot in way:

edge.h:

public: edge(myitem *sourcenode, myitem *destnode);  void adjust();  signals: void length_computed(qreal &length_reff); 

mainwindow.h:

class mainwindow: public qmainwindow { q_object  public: explicit mainwindow(qwidget *parent = 0); ~mainwindow(); ... public slots: void official_length_computation(); ... 

in mainwindow.cpp:

this->connect(this, signal(length_computed(&length_reff)), this, slot(official_length_computation())); 

i guess i'm wrong connect function.

any help?

thanks

edge.cpp

edge::edge(myitem *sourcenode, myitem *destnode) : arrowsize(10) {     setacceptedmousebuttons(0);     source = sourcenode;     dest = destnode;     source->addedge(this);     dest->addedge(this);       adjust(); }  void edge::adjust() {     qlinef line(mapfromitem(source, 0, 0), mapfromitem(dest, 0, 0));      preparegeometrychange();      qpointf edgeoffset(5, 5);     sourcepoint = line.p1() + edgeoffset;     destpoint = line.p2() + edgeoffset;       length_reff = sqrt((source->x()-dest->x())*(source->x()-dest->x())+(source->y()-dest->y())*(source->y()-dest->y()));      emit length_computed(length_reff);    } 

connection statement:

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))); 

this->connect(this, signal(length_computed(&length_reff)), this, slot(official_length_computation()));

bold wrong part. must be: ...signal(length_computed(qreal &)...

and want variable in slot... so:

this->connect(this, signal(length_computed(qreal &)), this, slot(official_length_computation(qreal &)));

but have add variable official_length_computation, too.

ok, not sure if shortened code. when edge supposed emit signals, must qobject , use q_object macro.

change edge.h way:

class edge: public qobject {  q_object 

..and adjust constructor accordingly


Comments