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. Fill a QTableWidget

Fill a QTableWidget

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 12.9k 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.
  • M Offline
    M Offline
    Massinissa
    wrote on last edited by
    #1

    Hello everyone,
    I'm trying to fill a QTablewidget with matrix of vectors, I have the following code. I'm sure that my Qt code is good but I don't know my it doesn't work. Is there something I missed out?

    I would really appreciate your help!
    @
    #include "qtablewidget.h"
    #include <vector>@

    @class Test
    {
    public:
    Test(){};
    MainClass obj;
    void setMatrix(std::vector<std::vector<double>> a){
    obj.GetMatrix(a);
    }
    void createMatrix(){
    /*
    / some code to fill the private variable matrix;
    */
    this->setMatrix(this->matrix);
    }
    private:
    std::vector<std::vector<double>> matrix;

    }@

    @class MainClass : public QMainWindow
    {
    Q_OBJECT
    public:
    MainClass(QWidget *parent = 0, Qt::WFlags flags = 0);

     void GetMatrix(std::vector<std::vector<double>> p){
        this->widget.tableWidget->setRowCount(p.size());
        this->widget.tableWidget->setColumnCount(p[0].size());
    
        for(unsigned int row=0;row<p.size();row++){
             for(unsigned int column=0;column<p[0].size();column++){
                 QTableWidgetItem* newItem = new QTableWidgetItem();
                 newItem->setText(QString::number(p[row][column]));
                 this->widget.tableWidget->setItem(row,column,newItem);
                 }
              }
        }
    

    private:
    Ui::MainClass widget;
    }@

    Massinissa Bandou Ing.jr

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Is your code compiled ok?
      Instead >> write > >:
      @std::vector<std::vector<double>>@

      @std::vector<std::vector<double> >@

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on last edited by
        #3

        And about your solution. In my opinion better not mix different libraries where it's possible. Qt has own QVector class which is also good as std::vector.
        it's just my opinion :)

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

          Hi,

          Where do you fill your matrix ? Are you sure it contains something ?

          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
          • M Offline
            M Offline
            Massinissa
            wrote on last edited by
            #5

            Hello both! *Thank you for reply! *

            Yes, my code compile and I'm sure that my matrix contains values in void createMatrix() I can display them in the output box. My Qt code seems correct because I have tried to create a random matrix in the constructor* MainCalss()* using the same Qt code as GetMatrix and it works. But when I try to call the void GetMatrix(std::vector<std::vector<double>> p) function from *calss Test * it just doesn't display. I mean I can display the values using cout<< but my Qt code doesn't respond.
            I'm wondering if declaring an object MainClass in the test.h file is correct or not??

            Again thank you for your HELP!!!

            Massinissa Bandou Ing.jr

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Massinissa
              wrote on last edited by
              #6

              I have tried to inherit the Test class from the MainClass to avoid the use of object but it still doesn't work!

              Massinissa Bandou Ing.jr

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

                You don't call
                @obj.show()@

                anywhere in you Test class. Without that no widget will be shown.

                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
                • M Offline
                  M Offline
                  Massinissa
                  wrote on last edited by
                  #8

                  Thank you for your quick reply!!!
                  I'm sorry for taking your time. Yes, in my main I have

                  @int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  MainClass w;
                  w.show();
                  return a.exec();
                  }@

                  Any code from another class than MainCalss doesn't execute????

                  Massinissa Bandou Ing.jr

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

                    In that code you don't have your Test class anywhere

                    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
                    • M Offline
                      M Offline
                      Massinissa
                      wrote on last edited by
                      #10

                      Here is the whole code and I have connected a signal to void CallMatrix() in the main class.
                      @
                      #include "MainCalss.h"
                      class Test
                      {
                      public:
                      Test(){};
                      static Test *New(){return new Test;}

                        MainClass obj;
                      
                        void setMatrix(std::vector<std::vector<double>> a){
                              obj.GetMatrix(a);
                         }
                      
                        void createMatrix(){
                             for(int i=0;i<4;i++){
                                   for(int j=0;j<4;j++){
                                      this->row.push_back(j);
                                   }
                                   this->matrix.push_back(row);
                                }
                             this->setMatrix(this->matrix);
                        }
                      

                      private:
                      std::vector<std::vector<double>> matrix;
                      std::vector<double> row;
                      }

                      #include "Test.h"

                      class MainClass : public QMainWindow
                      {
                      Q_OBJECT
                      public:
                      MainClass(QWidget *parent = 0, Qt::WFlags flags = 0);

                       void GetMatrix(std::vector<std::vector<double>> p){
                          this->widget.tableWidget->setRowCount(p.size());
                          this->widget.tableWidget->setColumnCount(p[0].size());
                      
                          for(unsigned int row=0;row<p.size();row++){
                               for(unsigned int column=0;column<p[0].size();column++){
                                   QTableWidgetItem* newItem = new QTableWidgetItem();
                                   newItem->setText(QString::number(p[row][column]));
                                   this->widget.tableWidget->setItem(row,column,newItem);
                                   }
                                }
                          }
                      

                      public slots:
                      void CallMatrix(){
                      Test *call = Test::New();
                      call->createMatrix();
                      }

                      private:
                      Ui::MainClass widget;
                      }@

                      @
                      #include "MainClass.h"
                      #include "Test.h"

                      int main(int argc, char *argv[])
                      {
                      QApplication a(argc, argv);
                      MainClass w;
                      w.show();
                      return a.exec();
                      }@

                      Massinissa Bandou Ing.jr

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Massinissa
                        wrote on last edited by
                        #11

                        In this case, I don't need to call Test class. m'I right??

                        Massinissa Bandou Ing.jr

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

                          You are updating the MainClass obj in your Test class not the one you create in main

                          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
                          • M Offline
                            M Offline
                            Massinissa
                            wrote on last edited by
                            #13

                            I'm not familiar with OOP, is there another way how can I figure out with this.
                            Thank you for your help!

                            Massinissa Bandou Ing.jr

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              Massinissa
                              wrote on last edited by
                              #14

                              Hi,
                              I tried to inherit Test class from MainClass to avoid the use of obj. But the problem still persist! My QTablewidget display nothing.

                              Any hint or advice would be appreciated. Thank you for your help!!!!

                              @class Test : public MainClass
                              {
                              public:
                              Test(){};
                              static Test *New(){return new Test;}

                                void setMatrix(std::vector<std::vector<double>> a){
                                      this->GetMatrix(a);
                                 }
                              
                                void createMatrix(){
                                     for(int i=0;i<4;i++){
                                           for(int j=0;j<4;j++){
                                              this->row.push_back(j);
                                           }
                                           this->matrix.push_back(row);
                                        }
                                     this->setMatrix(this->matrix);
                                }
                              

                              private:
                              std::vector<std::vector<double>> matrix;
                              std::vector<double> row;
                              }@

                              Massinissa Bandou Ing.jr

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

                                Your logic is flowed,

                                If you really want to set this matrix from another object:

                                • First rename GetMatrix to setMatrix -> GetXXX is for a getter which your function is clearly not

                                • Since your using Qt I would encourage your to use Qt's container classes (it's not mandatory)

                                • Refactor your test class
                                  @
                                  #include "MainClass.h"
                                  namespace Test
                                  {
                                  void updateMainClassMatrix(MainClass *mainClass)
                                  {
                                  if (!mainClass) {
                                  qCritical("Null pointer given");
                                  }
                                  QVector<double> row;
                                  for(int i=0;j<4;i++){
                                  row.push_back(i);
                                  }
                                  QVector<QVector<double>> matrix(4, row);
                                  mainClass->setMatrix(matrix);
                                  }
                                  }@

                                • Update your main.cpp
                                  @
                                  #include "test.h"
                                  int main(int argc, char *argv[])
                                  {
                                  QApplication a(argc, argv);
                                  MainClass w;
                                  w.show();
                                  Test::updateMainClassMatrix(&w);
                                  return a.exec();
                                  }@
                                  WARNING: code not compiled or tested but should work.

                                This is one way of doing what you want, maybe not the best but this should give you a starting point.

                                If you're not familiar with OOP, get some good books about it and read through the examples and demos from Qt, they are a good source of ideas. And since you seem to want to do testing, I would also encourage you to use TDD, it helps to learn how a framework works.

                                Hope it helps

                                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

                                • Login

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