Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. share variable between classes [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

share variable between classes [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
23 Posts 3 Posters 10.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mchinand
    wrote on last edited by
    #13

    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 )));
    1 Reply Last reply
    1
    • T Offline
      T Offline
      tmoranduzzo
      wrote on last edited by
      #14

      @mchinand

      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!

      K 1 Reply Last reply
      0
      • M Offline
        M Offline
        mchinand
        wrote on last edited by
        #15

        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.

        1 Reply Last reply
        0
        • T tmoranduzzo

          @mchinand

          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!

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #16

          @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).

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          1
          • T Offline
            T Offline
            tmoranduzzo
            wrote on last edited by tmoranduzzo
            #17

            @koahnig @mchinand

            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.

            K 1 Reply Last reply
            0
            • T tmoranduzzo

              @koahnig @mchinand

              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.

              K Offline
              K Offline
              koahnig
              wrote on last edited by
              #18

              @tmoranduzzo

              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.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tmoranduzzo
                wrote on last edited by
                #19

                @koahnig

                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.cpp

                Edge::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

                K 1 Reply Last reply
                0
                • T tmoranduzzo

                  @koahnig

                  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.cpp

                  Edge::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

                  K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #20

                  @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.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tmoranduzzo
                    wrote on last edited by
                    #21

                    @koahnig

                    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?

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tmoranduzzo
                      wrote on last edited by tmoranduzzo
                      #22

                      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!

                      K 1 Reply Last reply
                      0
                      • T tmoranduzzo

                        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!

                        K Offline
                        K Offline
                        koahnig
                        wrote on last edited by
                        #23

                        @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.

                        Vote the answer(s) that helped you to solve your issue(s)

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved