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 dziko147
    21 Jul 2021, 14:19

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

    UploadCSV *up;
    

    in backend.cpp

    up = new UploadCSV();
    
    C Offline
    C Offline
    CP71
    wrote on 21 Jul 2021, 14:20 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
      21 Jul 2021, 14:19

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

      UploadCSV *up;
      

      in backend.cpp

      up = new UploadCSV();
      
      C Offline
      C Offline
      CP71
      wrote on 21 Jul 2021, 14:22 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
        21 Jul 2021, 14:11

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

        P Offline
        P Offline
        Pl45m4
        wrote on 21 Jul 2021, 14:22 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
        • D dziko147
          21 Jul 2021, 14:19

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

          UploadCSV *up;
          

          in backend.cpp

          up = new UploadCSV();
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 21 Jul 2021, 14:26 last edited by
          #44

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

          currently I pass a pointer like this

          No, you do not pass any pointers here.
          What I mean is:

          up = new UploadCSV();
          Backend backend(up); // Here you pass pointer to up to Backend instance
          

          Of course you need to change the Backend constructor and store the pointer in Backend as member variable to be able to use it later.

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

          1 Reply Last reply
          4
          • D Offline
            D Offline
            dziko147
            wrote on 21 Jul 2021, 14:49 last edited by
            #45

            thank you all for your help :) .

            C 1 Reply Last reply 21 Jul 2021, 14:59
            0
            • D dziko147
              21 Jul 2021, 14:49

              thank you all for your help :) .

              C Offline
              C Offline
              CP71
              wrote on 21 Jul 2021, 14:59 last edited by
              #46

              @dziko147
              You are welcome! :)

              1 Reply Last reply
              0

              41/46

              21 Jul 2021, 14:20

              • Login

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