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. How Can i get array in another class
Forum Updated to NodeBB v4.3 + New Features

How Can i get array in another class

Scheduled Pinned Locked Moved Unsolved General and Desktop
46 Posts 8 Posters 7.0k Views 3 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.
  • D Offline
    D Offline
    dziko147
    wrote on 19 Jul 2021, 22:28 last edited by
    #17

    @SGaist exact. I set data in my array from GUI element .
    if i understand you. I don't need to create an instance of my class .
    So how can i call the array from the other class

    1 Reply Last reply
    0
    • A Offline
      A Offline
      anh_ph
      wrote on 20 Jul 2021, 03:41 last edited by
      #18

      If class B is child of class A, then you can do sth like this in class B

      classA *clsA= dynamic_cast <classA*> (parent());
      clsA->funcToGetData();
      
      1 Reply Last reply
      0
      • D Offline
        D Offline
        dziko147
        wrote on 20 Jul 2021, 11:41 last edited by
        #19

        @anh_ph the funcToGetData(); can be a slot wich get data from GUI elements ?

        And if classA isn't child od classB . How can i call it

        J A 2 Replies Last reply 20 Jul 2021, 12:56
        0
        • D dziko147
          20 Jul 2021, 11:41

          @anh_ph the funcToGetData(); can be a slot wich get data from GUI elements ?

          And if classA isn't child od classB . How can i call it

          J Offline
          J Offline
          JonB
          wrote on 20 Jul 2021, 12:56 last edited by JonB
          #20

          @dziko147 said in How Can i get array in another class:

          And if classA isn't child od classB . How can i call it

          The code you were shown attempts to cast the parent() to classA. If your classA instance is not related to you classB instance through its parent, then use whatever you have for the desired classA instance....

          I'm not sure but you seem not to understand the difference between a class and an instance (or object)? If that is the case I suggest you read up to understand, as this is vital in OO programming.

          1 Reply Last reply
          1
          • D Offline
            D Offline
            dziko147
            wrote on 21 Jul 2021, 00:35 last edited by
            #21

            @JonB no i know the difference between instance and class .
            But honestly i have no idea what parent() mean .
            And if we say a classA is a child of classB we mean classA is inherited drom classB .

            Thank you

            J 1 Reply Last reply 21 Jul 2021, 06:19
            0
            • D dziko147
              21 Jul 2021, 00:35

              @JonB no i know the difference between instance and class .
              But honestly i have no idea what parent() mean .
              And if we say a classA is a child of classB we mean classA is inherited drom classB .

              Thank you

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 21 Jul 2021, 06:19 last edited by
              #22

              @dziko147 said in How Can i get array in another class:

              we mean classA is inherited drom classB

              No, we don't.
              See https://doc.qt.io/qt-5/objecttrees.html
              Parent/child is not about inheritance it is about relationships between QObject based classes.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • D dziko147
                20 Jul 2021, 11:41

                @anh_ph the funcToGetData(); can be a slot wich get data from GUI elements ?

                And if classA isn't child od classB . How can i call it

                A Offline
                A Offline
                anh_ph
                wrote on 21 Jul 2021, 07:30 last edited by
                #23

                @dziko147 said in How Can i get array in another class:

                @anh_ph the funcToGetData(); can be a slot wich get data from GUI elements ?

                And if classA isn't child od classB . How can i call it

                If you want to get data from classA, you must get an exact object of it, which works with the data. If you can't do it, then try to apply singleton to classA.
                Btw, you can't get an array in another class, but you can get an array in an object of another class.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dziko147
                  wrote on 21 Jul 2021, 12:30 last edited by
                  #24

                  to resume what should i do to set data from GUI element in an array then get this array in another class .

                  Btw I tried many solution but usually I get an empty array in the other class :)

                  J 1 Reply Last reply 21 Jul 2021, 13:22
                  0
                  • D dziko147
                    21 Jul 2021, 12:30

                    to resume what should i do to set data from GUI element in an array then get this array in another class .

                    Btw I tried many solution but usually I get an empty array in the other class :)

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 21 Jul 2021, 13:22 last edited by
                    #25

                    @dziko147 said in How Can i get array in another class:

                    but usually I get an empty array in the other class

                    I guess that's because you have more than one instance of the class from which you get your array.
                    Please post your code. Passing data from one object to another is actually a trivial exercise.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • D Offline
                      D Offline
                      dziko147
                      wrote on 21 Jul 2021, 13:53 last edited by dziko147
                      #26

                      @jsulm

                      // classeA.h
                      std::array<QString,25> Inputs;
                      
                      // ClassA.cpp "UploadCSV"
                      void UploadCSV::on_parambtn_2_clicked()
                      {
                      
                          int compteur=0;
                          QString cell0;
                          QString cell3 ;
                          for(int i=0;i<ui->tablewidget->rowCount();i++){
                      
                              QTableWidgetItem *cell7 = ui->tablewidget->item(i,7);
                      
                              QVariant myData7 = cell7->data(Qt::DisplayRole);
                              if (myData7=="Core_meas"){
                                  cell0  = ui->tablewidget->item(i,0)->text();
                                  cell3 = ui->tablewidget->item(i,3)->text();
                                  Inputs[compteur]=cell3;
                                  qDebug() << "Inputs baby"<<Inputs[compteur] ;
                                  compteur++;
                              }
                      
                          }
                      
                      
                      }
                      

                      //in classB.

                      UploadCSV *up;

                      //in classB.cpp

                      up=new UploadCSV();
                      for(size_t i=0;i<up.Inputs->size();i++){
                      qqDebug() << "this is data",up.Inputs[i]
                      
                      
                      J P C 3 Replies Last reply 21 Jul 2021, 14:00
                      0
                      • D dziko147
                        21 Jul 2021, 13:53

                        @jsulm

                        // classeA.h
                        std::array<QString,25> Inputs;
                        
                        // ClassA.cpp "UploadCSV"
                        void UploadCSV::on_parambtn_2_clicked()
                        {
                        
                            int compteur=0;
                            QString cell0;
                            QString cell3 ;
                            for(int i=0;i<ui->tablewidget->rowCount();i++){
                        
                                QTableWidgetItem *cell7 = ui->tablewidget->item(i,7);
                        
                                QVariant myData7 = cell7->data(Qt::DisplayRole);
                                if (myData7=="Core_meas"){
                                    cell0  = ui->tablewidget->item(i,0)->text();
                                    cell3 = ui->tablewidget->item(i,3)->text();
                                    Inputs[compteur]=cell3;
                                    qDebug() << "Inputs baby"<<Inputs[compteur] ;
                                    compteur++;
                                }
                        
                            }
                        
                        
                        }
                        

                        //in classB.

                        UploadCSV *up;

                        //in classB.cpp

                        up=new UploadCSV();
                        for(size_t i=0;i<up.Inputs->size();i++){
                        qqDebug() << "this is data",up.Inputs[i]
                        
                        
                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 21 Jul 2021, 14:00 last edited by
                        #27

                        @dziko147 Sorry, I fail to understand the code you posted.
                        What type is myclasseA? And why do you access Inputs from two different objects (up and myclasseA)?
                        It really looks like you have two different instances of UploadCSV...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • D dziko147
                          21 Jul 2021, 13:53

                          @jsulm

                          // classeA.h
                          std::array<QString,25> Inputs;
                          
                          // ClassA.cpp "UploadCSV"
                          void UploadCSV::on_parambtn_2_clicked()
                          {
                          
                              int compteur=0;
                              QString cell0;
                              QString cell3 ;
                              for(int i=0;i<ui->tablewidget->rowCount();i++){
                          
                                  QTableWidgetItem *cell7 = ui->tablewidget->item(i,7);
                          
                                  QVariant myData7 = cell7->data(Qt::DisplayRole);
                                  if (myData7=="Core_meas"){
                                      cell0  = ui->tablewidget->item(i,0)->text();
                                      cell3 = ui->tablewidget->item(i,3)->text();
                                      Inputs[compteur]=cell3;
                                      qDebug() << "Inputs baby"<<Inputs[compteur] ;
                                      compteur++;
                                  }
                          
                              }
                          
                          
                          }
                          

                          //in classB.

                          UploadCSV *up;

                          //in classB.cpp

                          up=new UploadCSV();
                          for(size_t i=0;i<up.Inputs->size();i++){
                          qqDebug() << "this is data",up.Inputs[i]
                          
                          
                          P Offline
                          P Offline
                          Pl45m4
                          wrote on 21 Jul 2021, 14:00 last edited by Pl45m4
                          #28

                          @dziko147 said in How Can i get array in another class:

                          ,myclasseA.Inputs[i]

                          Why do you have myClasseA there, when the object is up
                          Try up.Inputs[i]

                          (Corrected, thx @jsulm )


                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                          ~E. W. Dijkstra

                          J 1 Reply Last reply 21 Jul 2021, 14:00
                          0
                          • P Pl45m4
                            21 Jul 2021, 14:00

                            @dziko147 said in How Can i get array in another class:

                            ,myclasseA.Inputs[i]

                            Why do you have myClasseA there, when the object is up
                            Try up.Inputs[i]

                            (Corrected, thx @jsulm )

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 21 Jul 2021, 14:00 last edited by
                            #29

                            @Pl45m4 said in How Can i get array in another class:

                            when the object is UploadCSV

                            UploadCSV seems to be the class, not object

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              dziko147
                              wrote on 21 Jul 2021, 14:04 last edited by
                              #30

                              @jsulm sorry I edited the code .

                              I gave the expamle of classA and ClassB to be clearer .

                              ClassA (UploadCSV ) is a Qwidget : Qdialog

                              ClassB (Backend) is QObject .

                              J 1 Reply Last reply 21 Jul 2021, 14:07
                              0
                              • D dziko147
                                21 Jul 2021, 13:53

                                @jsulm

                                // classeA.h
                                std::array<QString,25> Inputs;
                                
                                // ClassA.cpp "UploadCSV"
                                void UploadCSV::on_parambtn_2_clicked()
                                {
                                
                                    int compteur=0;
                                    QString cell0;
                                    QString cell3 ;
                                    for(int i=0;i<ui->tablewidget->rowCount();i++){
                                
                                        QTableWidgetItem *cell7 = ui->tablewidget->item(i,7);
                                
                                        QVariant myData7 = cell7->data(Qt::DisplayRole);
                                        if (myData7=="Core_meas"){
                                            cell0  = ui->tablewidget->item(i,0)->text();
                                            cell3 = ui->tablewidget->item(i,3)->text();
                                            Inputs[compteur]=cell3;
                                            qDebug() << "Inputs baby"<<Inputs[compteur] ;
                                            compteur++;
                                        }
                                
                                    }
                                
                                
                                }
                                

                                //in classB.

                                UploadCSV *up;

                                //in classB.cpp

                                up=new UploadCSV();
                                for(size_t i=0;i<up.Inputs->size();i++){
                                qqDebug() << "this is data",up.Inputs[i]
                                
                                
                                C Offline
                                C Offline
                                CP71
                                wrote on 21 Jul 2021, 14:05 last edited by
                                #31

                                @dziko147 said in How Can i get array in another class:

                                up=new UploadCSV();
                                for(size_t i=0;i<up.Inputs->size();i++){
                                qqDebug() << "this is data",up.Inputs[i]

                                Sorry, who calls the on_parambtn_2_clicked() function?

                                1 Reply Last reply
                                1
                                • D dziko147
                                  21 Jul 2021, 14:04

                                  @jsulm sorry I edited the code .

                                  I gave the expamle of classA and ClassB to be clearer .

                                  ClassA (UploadCSV ) is a Qwidget : Qdialog

                                  ClassB (Backend) is QObject .

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 21 Jul 2021, 14:07 last edited by
                                  #32

                                  @dziko147 Is this your real code:

                                  up=new UploadCSV();
                                  for(size_t i=0;i<up.Inputs->size();i++){
                                  qqDebug() << "this is data",up.Inputs[i]
                                  

                                  ?
                                  Because Items is filled when UploadCSV::on_parambtn_2_clicked() is called (at least according to code you posted). But you're printing Inputs just after creating UploadCSV instance (up)! So why should there be anything in Inputs?

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  D 1 Reply Last reply 21 Jul 2021, 14:09
                                  3
                                  • D Offline
                                    D Offline
                                    dziko147
                                    wrote on 21 Jul 2021, 14:07 last edited by
                                    #33

                                    @CP71 it's called in UploadCSV class (classA) .

                                    and inside this function I set data in Inputs[] .

                                    J 1 Reply Last reply 21 Jul 2021, 14:08
                                    0
                                    • D dziko147
                                      21 Jul 2021, 14:07

                                      @CP71 it's called in UploadCSV class (classA) .

                                      and inside this function I set data in Inputs[] .

                                      J Offline
                                      J Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 21 Jul 2021, 14:08 last edited by jsulm
                                      #34

                                      @dziko147 said in How Can i get array in another class:

                                      it's called in UploadCSV class (classA) .

                                      Where exactly?! And what is classA?! This all is really confusing!
                                      up != classA

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      D 1 Reply Last reply 21 Jul 2021, 14:11
                                      1
                                      • J jsulm
                                        21 Jul 2021, 14:07

                                        @dziko147 Is this your real code:

                                        up=new UploadCSV();
                                        for(size_t i=0;i<up.Inputs->size();i++){
                                        qqDebug() << "this is data",up.Inputs[i]
                                        

                                        ?
                                        Because Items is filled when UploadCSV::on_parambtn_2_clicked() is called (at least according to code you posted). But you're printing Inputs just after creating UploadCSV instance (up)! So why should there be anything in Inputs?

                                        D Offline
                                        D Offline
                                        dziko147
                                        wrote on 21 Jul 2021, 14:09 last edited by
                                        #35

                                        @jsulm yes i understand you . So can you suggest a way to do this correctly ?

                                        J SGaistS 2 Replies Last reply 21 Jul 2021, 14:09
                                        0
                                        • D dziko147
                                          21 Jul 2021, 14:09

                                          @jsulm yes i understand you . So can you suggest a way to do this correctly ?

                                          J Offline
                                          J Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on 21 Jul 2021, 14:09 last edited by
                                          #36

                                          @dziko147 Yes, I can: use one instance of the class instead of two...

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          2

                                          26/46

                                          21 Jul 2021, 13:53

                                          • Login

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