简体   繁体   中英

share variable between classes QT

I created an application that exploits three different classes of objects. Basically I have the first class that "all the interface and the main code" the second one that is a simple rect object and the last one that is the edge between different rect (more or less as in the elasticnode example ). Basically every time the user moves the rect the edge is modified and I need to seed the "length" to the main class.

here is the code of the 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 I have to send the lenght_ref variable to the MainWindow class
}

I tried to implement SIGNAL/SLOT in this 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 completely wrong with the 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 is wrong part. Must be: ...SIGNAL(length_COMPUTED( qreal & )...

And most likely you want the variable in your slot... so:

this->connect(this, SIGNAL(length_COMPUTED( qreal & )), this, SLOT(official_length_computation( qreal & )));

But then you have to add this variable to official_length_computation, too.

Ok, not sure if you just shortened your code. But when Edge is supposed to emit signals, it must be a QObject and use the Q_OBJECT macro.

Change Edge.h this way:

class Edge: public QObject {
 Q_OBJECT

..and adjust its constructor accordingly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM