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 8.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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    What exactly do you want to do ?

    Class A seems to be a widget that you likely instanciate somewhere. You likely instanciate an object of class B somewhere else and thus these are two different objects of class A.

    What exactly are you trying to do ?

    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
    0
    • D Offline
      D Offline
      dziko147
      wrote on last edited by
      #3

      @SGaist
      yes , exact class A is a widget .I uploaded a csv file then extract some data from it and put it in Inputs[] using this code :

      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 "<<Inputs[compteur] ;
      compteur++;
      }
      

      So I would like to get the Inputs[] in my class B .

      I create a classA instance in class B .

      ClasseA myclasseA; // in classB.h
      //then i tried to get Inputs[] using this code
      for(size_t i=0;i<myclasseA.Inputs.size();i++){
      myclasseA.Inputs[i];
      qDebug() << "this is data",myclasseA.Inputs[i]
      

      So my question is what i have missed .? Or how can i do this correctly .

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        What is class B supposed to be doing ?

        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
        0
        • D Offline
          D Offline
          dziko147
          wrote on last edited by
          #5

          @SGaist I will manipulate a CAN bus (send and receive frame) using data in inputs[].

          A 1 Reply Last reply
          0
          • D dziko147

            @SGaist I will manipulate a CAN bus (send and receive frame) using data in inputs[].

            A Offline
            A Offline
            Anonymous_Banned275
            wrote on last edited by
            #6

            @dziko147 I am doing similar task - passing data between classes and trying to use "connect". In my case the "parent " class" is a "form" and the "child" class is a widget - dialog.
            I do not have it fully solved.
            I can use QDesigner "go to slot" , but so far only within the class.
            I found a bug ( submitted and acknowledged as bug ) in "go to slot" and trying to hack around it for now.
            Is that something - connect - which would work for you?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dziko147
              wrote on last edited by
              #7

              @AnneRanch what type variable you passed please !

              A 1 Reply Last reply
              0
              • D Offline
                D Offline
                dziko147
                wrote on last edited by
                #8
                This post is deleted!
                1 Reply Last reply
                0
                • D dziko147

                  Hello ,
                  I created a public array in class A .
                  I want to get this array in class B .

                  so this is my code .

                  In class A.h

                  std::array<QString,25> Inputs;
                  
                  

                  In class A.cpp

                  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 class B :

                  I create instance of Class A then I called the array but I get it null .

                  this what I did :
                  In class B.h

                  #include "classA.h"
                  
                  ClassA myclassA ;
                  

                  In class B.cpp

                  for(size_t i=0;i<myclasseA.Inputs.size();i++){
                  myclasseA.Inputs[i];
                  qDebug() << "this is data",myclasseA.Inputs[i]
                  
                  

                  I get outputs :

                  this is data : ""
                  

                  What I did wrong .
                  Or How can I do this correctly
                  thankyou

                  CP71C Offline
                  CP71C Offline
                  CP71
                  wrote on last edited by CP71
                  #9

                  @dziko147
                  Hi,
                  I tried doing make the same thing and I didn't see anything wrong.
                  Are you sure the fill function is called?
                  Are you sure that you don't have two instances of the class?

                  Class1.h
                  public:
                  std::array<QString,25> Inputs;

                  Class1.cpp
                  Class1::Class1(QObject *parent) : QObject(parent)
                  {
                  for( int i=0; i<25; i++)
                  {
                  Inputs[i] = 'a'+i;
                  }
                  }
                  Class2.h
                  protected:
                  Class1 *myClass;

                  Class2.cpp
                  Class2::Class2(QObject *parent) : QObject(parent)
                  {
                  myClass = new Class1();
                  for ( int i=0; i<25; i++)
                  qDebug() << myClass->Inputs[i];
                  }
                  My output
                  "a"
                  "b"
                  "c"
                  "d"
                  "e"
                  "f"
                  "g"
                  "h"
                  "i"
                  "j"
                  "k"
                  "l"
                  "m"
                  "n"
                  "o"
                  "p"
                  "q"
                  "r"
                  "s"
                  "t"
                  "u"
                  "v"
                  "w"
                  "x"
                  "y"

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

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

                    protected:
                    ClassChield *chield;

                    can you explain me what did this ?

                    CP71C 1 Reply Last reply
                    0
                    • D dziko147

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

                      protected:
                      ClassChield *chield;

                      can you explain me what did this ?

                      CP71C Offline
                      CP71C Offline
                      CP71
                      wrote on last edited by CP71
                      #11

                      @dziko147
                      My mistake, I have corrected my previous post.
                      In my example project the classes have different names
                      Sorry

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        dziko147
                        wrote on last edited by
                        #12

                        @CP71 thanks for your reply .
                        I used your solution but still get array empty.

                        I set data in Inputs in a Slot called after clicking in button .
                        It cause problem ?

                        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++;
                                }
                        
                            }
                        
                        
                        }
                        
                        CP71C SGaistS 2 Replies Last reply
                        0
                        • D dziko147

                          @CP71 thanks for your reply .
                          I used your solution but still get array empty.

                          I set data in Inputs in a Slot called after clicking in button .
                          It cause problem ?

                          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++;
                                  }
                          
                              }
                          
                          
                          }
                          
                          CP71C Offline
                          CP71C Offline
                          CP71
                          wrote on last edited by
                          #13

                          @dziko147
                          I suppose you see a valid values in array at line:
                          qDebug () << "Inputs baby" << Inputs [compteur];

                          If yes, the only thing I think is a double instance of the class, but surely you have already checked it.

                          I don't have other ideas :(

                          1 Reply Last reply
                          0
                          • D dziko147

                            @AnneRanch what type variable you passed please !

                            A Offline
                            A Offline
                            Anonymous_Banned275
                            wrote on last edited by
                            #14

                            @dziko147 At current state of the project I need only a text (QTString ) - basically address and name of local , connected to PC, Bluetooth device.
                            That data is a result of my dialog "scan" for local Bluetooth adapters. .

                            Since it is such small amount of data I am currently NOT using "connect".
                            Eventually I am going to use connect to pass real data from the dialog to the form.

                            I started using "go to slot" feature , however I have not figured out how to use it "between objects ".
                            It is interesting feature but so far only good to "pass data" in same object.

                            It has an optional "parameter", but I not found how to use it.

                            So in summary - using "go to slot" should be able to pass data between objects - as you desire . But I need to study it more.

                            PS
                            Passing data between objects seems to be subject of many discussions.
                            The way I see it - it is two step process - you have to collect the data (obviously) using "connect" triggered by the data event. . Then use second "connect " to pass the captured data between objects.

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              dziko147
                              wrote on last edited by
                              #15

                              @AnneRanch if you need just a Qstring to pass it . I recommend using signal/Slot .

                              1 Reply Last reply
                              0
                              • D dziko147

                                @CP71 thanks for your reply .
                                I used your solution but still get array empty.

                                I set data in Inputs in a Slot called after clicking in button .
                                It cause problem ?

                                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++;
                                        }
                                
                                    }
                                
                                
                                }
                                
                                SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @dziko147 you should go back to the basics first: séparation of responsibilities.

                                There's no reason for you child class to have an instance of your GUI class in it.

                                From the looks of it, your GUI is responsible to generate that array and it looks as well as the class processing that array should receive it from the GUI object. Hence there's no need for it to know anything or contain an object of your GUI class.

                                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
                                3
                                • D Offline
                                  D Offline
                                  dziko147
                                  wrote on 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 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 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

                                      JonBJ A 2 Replies Last reply
                                      0
                                      • D dziko147

                                        @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

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on 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 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

                                          jsulmJ 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