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

How Can i get array in another class

Scheduled Pinned Locked Moved Unsolved General and Desktop
46 Posts 8 Posters 7.0k Views
  • 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 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 :)

    jsulmJ 1 Reply Last reply
    0
    • D dziko147

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

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on 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 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]
        
        
        jsulmJ Pl45m4P CP71C 3 Replies Last reply
        0
        • D dziko147

          @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]
          
          
          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on 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

            @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]
            
            
            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on 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

            jsulmJ 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

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

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on 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 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 .

                jsulmJ 1 Reply Last reply
                0
                • D dziko147

                  @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]
                  
                  
                  CP71C Offline
                  CP71C Offline
                  CP71
                  wrote on 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

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

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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
                    3
                    • D Offline
                      D Offline
                      dziko147
                      wrote on last edited by
                      #33

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

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

                      jsulmJ 1 Reply Last reply
                      0
                      • D dziko147

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

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

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on 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
                        1
                        • jsulmJ jsulm

                          @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 last edited by
                          #35

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

                          jsulmJ SGaistS 2 Replies Last reply
                          0
                          • D dziko147

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

                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on 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
                            • D dziko147

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

                              SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #37

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

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

                              Something like it was explained in your other thread ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1
                              • jsulmJ jsulm

                                @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

                                D Offline
                                D Offline
                                dziko147
                                wrote on last edited by
                                #38

                                @jsulm forget classA and classB example .

                                So curretly we have UploadCSV class and BackendClass .
                                I would like to get array defined from UploadCSV in Backend class .

                                jsulmJ Pl45m4P 2 Replies Last reply
                                0
                                • D dziko147

                                  @jsulm forget classA and classB example .

                                  So curretly we have UploadCSV class and BackendClass .
                                  I would like to get array defined from UploadCSV in Backend class .

                                  jsulmJ Online
                                  jsulmJ Online
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #39

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

                                  I would like to get array defined from UploadCSV in Backend class

                                  Then do so.
                                  To get it you need the UploadCSV instance. How you get it in Backend I don't know as I don't have your code. There are different ways to do this. You can pass pointer to UploadCSV instance to Backend constructor for example.
                                  But to be able to give a good advice we would need to know how you manage your UploadCSV and Backend instances.

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

                                  1 Reply Last reply
                                  2
                                  • D Offline
                                    D Offline
                                    dziko147
                                    wrote on last edited by
                                    #40

                                    @jsulm
                                    currently I pass a pointer like this .
                                    in backend.h

                                    UploadCSV *up;
                                    

                                    in backend.cpp

                                    up = new UploadCSV();
                                    
                                    CP71C jsulmJ 3 Replies Last reply
                                    0
                                    • D dziko147

                                      @jsulm
                                      currently I pass a pointer like this .
                                      in backend.h

                                      UploadCSV *up;
                                      

                                      in backend.cpp

                                      up = new UploadCSV();
                                      
                                      CP71C Offline
                                      CP71C Offline
                                      CP71
                                      wrote on last edited by CP71
                                      #41

                                      @dziko147
                                      Here you are not passing a pointer but you are creating a new instance of class

                                      1 Reply Last reply
                                      1
                                      • D dziko147

                                        @jsulm
                                        currently I pass a pointer like this .
                                        in backend.h

                                        UploadCSV *up;
                                        

                                        in backend.cpp

                                        up = new UploadCSV();
                                        
                                        CP71C Offline
                                        CP71C Offline
                                        CP71
                                        wrote on last edited by CP71
                                        #42

                                        @dziko147
                                        Sorry, but my English is no good.
                                        So maybe I can help you with an example.

                                        63a538e9-5f5f-47a8-98f4-1fe03c5462a4-image.png

                                        To link my example.
                                        I think you are filling your array in obj1, but when you try to read it you use obj2.
                                        You that know your project, have a sense what I write?

                                        In short, these are possible ways to have your result:

                                        • two or more instance of you class
                                        • your object never fills the array
                                        • your object fills the array but with an empty value
                                        • in somewhere of your class clear your array before you use it

                                        But in your case seems you have two instances of class

                                        1 Reply Last reply
                                        2
                                        • D dziko147

                                          @jsulm forget classA and classB example .

                                          So curretly we have UploadCSV class and BackendClass .
                                          I would like to get array defined from UploadCSV in Backend class .

                                          Pl45m4P Offline
                                          Pl45m4P Offline
                                          Pl45m4
                                          wrote on last edited by Pl45m4
                                          #43

                                          @dziko147

                                          Now that I understand a bit more, I think the only issue here is that you dont click that button that will fill your array before you try to access the elements.

                                          You fill your array here:

                                          void UploadCSV::on_parambtn_2_clicked()
                                          {
                                          ....
                                          if (myData7=="Core_meas"){
                                                      cell0  = ui->tablewidget->item(i,0)->text();
                                                      cell3 = ui->tablewidget->item(i,3)->text();
                                                      Inputs[compteur]=cell3;  //  <---- HERE
                                                      qDebug() << "Inputs baby"<<Inputs[compteur] ;
                                                      compteur++;
                                                  }
                                          ....
                                          
                                          }
                                          

                                          That means the array is still empty right after creating the class instance.

                                          up=new UploadCSV(); // <--- New, empty public array
                                          for(size_t i=0;i<up.Inputs->size();i++){
                                          qDebug() << "this is data",up.Inputs[i] // data access, but not clicked that button before
                                          

                                          Also, you need to have access to the "same" instance where your array is filled with data. (See @CP71 and @jsulm answers). This could be done by passing a pointer to that object with contains your data.


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

                                          ~E. W. Dijkstra

                                          1 Reply Last reply
                                          1

                                          • Login

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