Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. [How to use a result of a function in another class]
Forum Updated to NodeBB v4.3 + New Features

[How to use a result of a function in another class]

Scheduled Pinned Locked Moved Solved Qt 6
18 Posts 4 Posters 2.2k 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.
  • A appdev

    Well I don't think it's related directly to the UI.

    In fact, here is the thing.

    In my mainwindow.ui, I added a push button which when clicked, it opens a file, reads it and extracts the values then stores them in QLists. All these functions were developed in my MainWindow.cpp since when I right click the button and go to slot, it takes me directly to the mainwindow.cpp file.

    So I'm going to create another class, develop these functions again ( read the file, extract the values, store them in QList) and in my MainWindow class I'm gonna connect the button to the new class.

    Then, I'm going to add the getter to the new class and see if it works that way.

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #8

    @appdev That is indeed a good way to separate UI from non-UI code, especially as your code size grows.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      appdev
      wrote on last edited by appdev
      #9

      Hello again.

      So you said that children shouldn't know anything about their parents but the other way is possible. So I tried another thing:

      I developped another function that copies the QList values into another list which is an attribute of the mainwidget class.

      void MainWindow::copyvaluestomainwidget()
      {
          for (int i=0; i<w_list.size();i++)
          {
             this->ui->mainwidget->w.append(w_list[i]);
          }
          qDebug()<<"w"<<this->ui->mainwidget->w;
      }
      

      I used qDebug() inside this function to see if the list is empty or not but didn't get anything. It looks like qDebug() isn't working and I can't figure out why. However
      It works inside other functions.

      JonBJ SGaistS 3 Replies Last reply
      0
      • A appdev

        Hello again.

        So you said that children shouldn't know anything about their parents but the other way is possible. So I tried another thing:

        I developped another function that copies the QList values into another list which is an attribute of the mainwidget class.

        void MainWindow::copyvaluestomainwidget()
        {
            for (int i=0; i<w_list.size();i++)
            {
               this->ui->mainwidget->w.append(w_list[i]);
            }
            qDebug()<<"w"<<this->ui->mainwidget->w;
        }
        

        I used qDebug() inside this function to see if the list is empty or not but didn't get anything. It looks like qDebug() isn't working and I can't figure out why. However
        It works inside other functions.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #10

        @appdev
        If qDebug() "works in other functions" and you do not get any output from the one here, you are not calling copyvaluestomainwidget(). Put a qDebug() as the first statement in this function.

        1 Reply Last reply
        0
        • A appdev

          Hello again.

          So you said that children shouldn't know anything about their parents but the other way is possible. So I tried another thing:

          I developped another function that copies the QList values into another list which is an attribute of the mainwidget class.

          void MainWindow::copyvaluestomainwidget()
          {
              for (int i=0; i<w_list.size();i++)
              {
                 this->ui->mainwidget->w.append(w_list[i]);
              }
              qDebug()<<"w"<<this->ui->mainwidget->w;
          }
          

          I used qDebug() inside this function to see if the list is empty or not but didn't get anything. It looks like qDebug() isn't working and I can't figure out why. However
          It works inside other functions.

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

          @appdev Hi,

          If the qDebug statement is not triggered in you loop it just means that w_list is empty.

          How are you filling it ?

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

          JonBJ 1 Reply Last reply
          0
          • SGaistS SGaist

            @appdev Hi,

            If the qDebug statement is not triggered in you loop it just means that w_list is empty.

            How are you filling it ?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #12

            @SGaist said in [How to use a result of a function in another class]:

            If the qDebug statement is not triggered in you loop it just means that w_list is empty.

            But the qDebug() statement is shown outside the loop? :)

            SGaistS 1 Reply Last reply
            0
            • JonBJ JonB

              @SGaist said in [How to use a result of a function in another class]:

              If the qDebug statement is not triggered in you loop it just means that w_list is empty.

              But the qDebug() statement is shown outside the loop? :)

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

              @JonB The OP talked about the one he used to have inside the loop. Now re-reading his post, it seems that the one outside the loop is not call either, which indicates that the function is not called.

              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
              • A Offline
                A Offline
                appdev
                wrote on last edited by appdev
                #14

                Hello again,

                void MainWindow::copyvaluestomainwidget()
                {
                    for (int i=0; i<w_list.size();i++)
                    {
                       this->ui->mainwidget->w.append(w_list[i]);
                    }
                    qDebug()<<"w"<<this->ui->mainwidget->w;
                }
                

                This function copies the values of the QList "w_list" in another QList which is "w".
                w_list is an attribute of mainwindow class.
                w is an attribute of mainwidget class.

                @SGaist @JonB The copied list isn't empty because when I call that function in the other function that opens the file, extracts data and stores them in QLists and use qDebug() as follows:

                copyw_valuestomainwidget();
                qDebug()<<"w"<<this->ui->mainwidget->w;
                

                The output of the application shows "w" filled with the copied values.

                However, there is something wrong : I declared "w" as QList of floats in the mainwidget class attributes.

                And tried to fill it with values of a QList from the mainwindow class which I called w_list.

                When I go to mainwidget class and try to see the values of "w" [the copied list], I still get an empty list. I didn't know what's missing.

                1 Reply Last reply
                0
                • A appdev

                  Hello again.

                  So you said that children shouldn't know anything about their parents but the other way is possible. So I tried another thing:

                  I developped another function that copies the QList values into another list which is an attribute of the mainwidget class.

                  void MainWindow::copyvaluestomainwidget()
                  {
                      for (int i=0; i<w_list.size();i++)
                      {
                         this->ui->mainwidget->w.append(w_list[i]);
                      }
                      qDebug()<<"w"<<this->ui->mainwidget->w;
                  }
                  

                  I used qDebug() inside this function to see if the list is empty or not but didn't get anything. It looks like qDebug() isn't working and I can't figure out why. However
                  It works inside other functions.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #15

                  @appdev said in [How to use a result of a function in another class]:

                  I used qDebug() inside this function to see if the list is empty or not but didn't get anything. It looks like qDebug() isn't working and I can't figure out why.

                  So you did not "didn't get anything", you got at least "w" printed out from the qDebug() to show it has been hit? Can you please be accurate on what you say, we can only go by what you write.

                  It looks like qDebug() isn't working

                  qDebug() "works".

                  I don't know what you are doing wrong. For all I know there is a "shadow" local variable for w or w_list hiding the member variable maybe....

                  void MainWindow::copyvaluestomainwidget()
                  {
                      qDebug() << w_list.size() << this->ui->mainwidget->w.size();
                      for (int i=0; i<w_list.size();i++)
                      {
                         qDebug() << w_list[i];
                         this->ui->mainwidget->w.append(w_list[i]);
                      }
                      qDebug() << w_list.size() << this->ui->mainwidget->w.size();
                      qDebug()<<"w"<<this->ui->mainwidget->w;
                  }
                  

                  Then there is your:

                  copyw_valuestomainwidget();

                  Well, the method you show is named MainWindow::copyvaluestomainwidget(). So that call does not call it, or you are not showing us the relevant code.

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    appdev
                    wrote on last edited by
                    #16

                    Okay, I will try to be more accurate.

                    In my MainWindow class, I have two functions:
                    -One that opens a file, extracts some data, stores these data in QLists ( 4 QLists to be more precise)
                    -the second one copies the values of the first QList in a new list.

                    This is the second function:

                    void MainWindow::copyw_valuestomainwidget()
                    {
                        for (int i=0; i<w_list.size();i++)
                        {
                           this->ui->mainwidget->w.append(w_list[i]);
                        }
                        qDebug()<<"w"<<this->ui->mainwidget->w;
                    }
                    

                    When I run my program, the output of the application doesn't show anything even if i add qDebug inside that function as I did in the code above.

                    When I call the copy function in the function that opens the file, extracts data and stores them in QLists and then add qDebug() to see if the new list is full, the output of the application shows the new list which is successfully filled.

                    copyw_valuestomainwidget();
                    qDebug()<<"w"<<this->ui->mainwidget->w;
                    

                    That's how I call the copy function and I do see the new QList which is "w" and which filled with the same values as the old QList "w_list".

                    Till now, I'm only talking about the mainwindow class.
                    Now here is the thing:

                    w_list is an attribute of mainwindow.
                    w is an attribute of mainwidget.
                    My goal is to copy the values from the QList "w_list" to the new QList "w" so I can use the values of "w" in the class mainwidget.

                    After developping the copy function in the class mainwindow, I went to mainwidget class and tried this:

                    qDebug()<<"w"<<w;
                    
                    

                    The output of the application shows an empty QList in this case, as if the application didn't take into consideration the copy function I used to fill the QList w.

                    I didn't know what's missing

                    jsulmJ 1 Reply Last reply
                    0
                    • A appdev

                      Okay, I will try to be more accurate.

                      In my MainWindow class, I have two functions:
                      -One that opens a file, extracts some data, stores these data in QLists ( 4 QLists to be more precise)
                      -the second one copies the values of the first QList in a new list.

                      This is the second function:

                      void MainWindow::copyw_valuestomainwidget()
                      {
                          for (int i=0; i<w_list.size();i++)
                          {
                             this->ui->mainwidget->w.append(w_list[i]);
                          }
                          qDebug()<<"w"<<this->ui->mainwidget->w;
                      }
                      

                      When I run my program, the output of the application doesn't show anything even if i add qDebug inside that function as I did in the code above.

                      When I call the copy function in the function that opens the file, extracts data and stores them in QLists and then add qDebug() to see if the new list is full, the output of the application shows the new list which is successfully filled.

                      copyw_valuestomainwidget();
                      qDebug()<<"w"<<this->ui->mainwidget->w;
                      

                      That's how I call the copy function and I do see the new QList which is "w" and which filled with the same values as the old QList "w_list".

                      Till now, I'm only talking about the mainwindow class.
                      Now here is the thing:

                      w_list is an attribute of mainwindow.
                      w is an attribute of mainwidget.
                      My goal is to copy the values from the QList "w_list" to the new QList "w" so I can use the values of "w" in the class mainwidget.

                      After developping the copy function in the class mainwindow, I went to mainwidget class and tried this:

                      qDebug()<<"w"<<w;
                      
                      

                      The output of the application shows an empty QList in this case, as if the application didn't take into consideration the copy function I used to fill the QList w.

                      I didn't know what's missing

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

                      @appdev said in [How to use a result of a function in another class]:

                      After developping the copy function in the class mainwindow, I went to mainwidget class and tried this:
                      qDebug()<<"w"<<w;

                      1. Please show the whole code where you put this qDebug
                      2. Do you call this qDebug AFTER doing the copy?

                      One note to the design: You should NOT implement the copy inside MainWindow. Instead your mainwidget class should have a setter method like:

                      void mainwidget::setList(const QList<WHATEVERTYPEITIS> list)
                      {
                          w += list;
                      }
                      

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

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        appdev
                        wrote on last edited by appdev
                        #18

                        Guys I'm really sorry, I should have called the copy function so that it can be executed.
                        Now I get a QList full of values.

                        Sorry again, a simple lack of attention made things go wrong.
                        @jsulm @JonB @JonB Thank you for your precious time and patience

                        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