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. Insert fill circle into cell of QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Insert fill circle into cell of QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
88 Posts 5 Posters 47.5k Views 2 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
    #34

    You should also provide APIs that will allow you ton modify that matrix.

    As for the roles, they are there to retrieve specific information from the model's elements.

    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
    2
    • J Offline
      J Offline
      juaniyoalm
      wrote on last edited by juaniyoalm
      #35

      @SGaist said in Insert fill circle into cell of QTableWidget:

      You should also provide APIs that will allow you ton modify that matrix.
      As for the roles, they are there to retrieve specific information from the model's elements.

      I'm sorry but I still do not understand well the models and roles in QT.
      Can anyone give me an example of how the model class should be?

      I have a matrix (vector <vector <Cell >>) and when modifying some attributes of the Cell class, have to change what is painted in the graphic representation (Only the one in the cell corresponding to the coordinates of the matrix).

      mrjjM 1 Reply Last reply
      0
      • J juaniyoalm

        @SGaist said in Insert fill circle into cell of QTableWidget:

        You should also provide APIs that will allow you ton modify that matrix.
        As for the roles, they are there to retrieve specific information from the model's elements.

        I'm sorry but I still do not understand well the models and roles in QT.
        Can anyone give me an example of how the model class should be?

        I have a matrix (vector <vector <Cell >>) and when modifying some attributes of the Cell class, have to change what is painted in the graphic representation (Only the one in the cell corresponding to the coordinates of the matrix).

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #36

        @juaniyoalm
        Hi
        Its something like this
        https://meetingcpp.com/blog/items/an-introduction-into-qt-part-3.html
        However he uses a
        std::vector<PersonalData> mydata;
        where you have
        vector <vector <Cell >>
        so you need to use both index.row and index.col
        to address your matrix. ( the data)

        J 1 Reply Last reply
        2
        • mrjjM mrjj

          @juaniyoalm
          Hi
          Its something like this
          https://meetingcpp.com/blog/items/an-introduction-into-qt-part-3.html
          However he uses a
          std::vector<PersonalData> mydata;
          where you have
          vector <vector <Cell >>
          so you need to use both index.row and index.col
          to address your matrix. ( the data)

          J Offline
          J Offline
          juaniyoalm
          wrote on last edited by
          #37

          @mrjj Thanks so much.

          I was doing it that way, but how did I assign the matrix to the model variable? At this moment, I am creating the matrix in mainwindow class ...

          mrjjM 1 Reply Last reply
          0
          • J juaniyoalm

            @mrjj Thanks so much.

            I was doing it that way, but how did I assign the matrix to the model variable? At this moment, I am creating the matrix in mainwindow class ...

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #38

            @juaniyoalm
            Hi
            well the vector <vector <Cell >> matrix could be inside the model
            and you would use method like the Addperson in sample to fill it.
            Much like you do now. when maxtrix is outside of the model.
            You could also fill it in mainwindow and simply give whole matrix to model
            to copy it to internal variable.(matrix) but thats a bit waste full.

            Also i been wanting to ask you. You do use a TableView because you need scrolling correct?
            I mean there are far more rows with cells than can fit on on screen ?

            J 1 Reply Last reply
            3
            • mrjjM mrjj

              @juaniyoalm
              Hi
              well the vector <vector <Cell >> matrix could be inside the model
              and you would use method like the Addperson in sample to fill it.
              Much like you do now. when maxtrix is outside of the model.
              You could also fill it in mainwindow and simply give whole matrix to model
              to copy it to internal variable.(matrix) but thats a bit waste full.

              Also i been wanting to ask you. You do use a TableView because you need scrolling correct?
              I mean there are far more rows with cells than can fit on on screen ?

              J Offline
              J Offline
              juaniyoalm
              wrote on last edited by
              #39

              @mrjj

              Yes, there is no row limit, it can be 20 rows, 100 or 1000 xD. I always have to think about what I do to behave well when I do the parallel implementation, using pthread and openmp.

              mrjjM 1 Reply Last reply
              0
              • J juaniyoalm

                @mrjj

                Yes, there is no row limit, it can be 20 rows, 100 or 1000 xD. I always have to think about what I do to behave well when I do the parallel implementation, using pthread and openmp.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #40

                @juaniyoalm
                Ok so scrolling is needed:)
                Also make sure to check out QtConcurrent and QThread before jumping to pthread.

                J 1 Reply Last reply
                0
                • mrjjM mrjj

                  @juaniyoalm
                  Ok so scrolling is needed:)
                  Also make sure to check out QtConcurrent and QThread before jumping to pthread.

                  J Offline
                  J Offline
                  juaniyoalm
                  wrote on last edited by juaniyoalm
                  #41

                  @mrjj

                  ok, I will look it.

                  I have a doubt, when I modify a value of the Cell class (this is done in a method of the Cell class ), it will be modified automatically in the model or I have to do model-> setData (model-> index (i, j) , matrix [i] [j] -> getModifiedValue ())? The Cell of matrix are pointer...

                  mrjjM 1 Reply Last reply
                  0
                  • J juaniyoalm

                    @mrjj

                    ok, I will look it.

                    I have a doubt, when I modify a value of the Cell class (this is done in a method of the Cell class ), it will be modified automatically in the model or I have to do model-> setData (model-> index (i, j) , matrix [i] [j] -> getModifiedValue ())? The Cell of matrix are pointer...

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #42

                    @juaniyoalm
                    Well if you go with QAbstractTableModel
                    your matrix IS the data so if you modify it
                    and call dataChanged (model call ) to let view know data has been altered.

                    J 1 Reply Last reply
                    3
                    • mrjjM mrjj

                      @juaniyoalm
                      Well if you go with QAbstractTableModel
                      your matrix IS the data so if you modify it
                      and call dataChanged (model call ) to let view know data has been altered.

                      J Offline
                      J Offline
                      juaniyoalm
                      wrote on last edited by
                      #43

                      @mrjj

                      ok, this is my model implementation:

                      /#include "cellmodel.h"
                      
                      CellModel::CellModel(QObject *parent) : QAbstractTableModel(parent){}
                      CellModel::CellModel(int rows, int columns, int fungivores)
                      {
                          this->mundo = new World(rows, columns, fungivores);
                      }
                      
                      int CellModel::rowCount(const QModelIndex &parent) const
                      {
                          return this->mundo->world.size();
                      }
                      int CellModel::columnCount(const QModelIndex &parent) const
                      {
                          return this->mundo->world[0].size();
                      }
                      
                      QVariant CellModel::data(const QModelIndex &index, int role) const
                      {
                          if(!index.isValid())
                              return QVariant();
                      
                          if(index.row() >= mundo->world.size() || index.row() < 0 ||
                                 index.column() >= mundo->world[0].size() || index.column() < 0)
                              return QVariant();
                      
                          if(role == Qt::DisplayRole || role == Qt::EditRole)
                          {
                              return this->mundo->world[index.row()][index.column()]->getFood();
                          }
                          return QVariant();
                      }
                      
                      bool CellModel::setData(const QModelIndex &index, const QVariant &value, int role)
                      {
                          if (index.isValid() && role == Qt::EditRole && !(index.row() >= this->mundo->world.size() || index.row() < 0 ||
                                                                           index.column() >= this->mundo->world[0].size() || index.column() < 0))
                          {
                              int row = index.row();
                              int col = index.column();
                      
                              this->mundo->world[row][col]->setFood(value.toInt());
                      
                              emit dataChanged(index, index);
                              return true;
                          }
                          return false;
                      }
                      
                      Qt::ItemFlags CellModel::flags(const QModelIndex &index) const
                      {
                          if (!index.isValid())
                              return Qt::ItemIsEnabled;
                          return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
                      }
                      
                      
                      

                      if I modify the value in this way: model-> world-> world [0] [0] -> setFood (200);

                      1- All row is changed instead of just the selected cell.

                      2 - Repaint all matrix, although the data of the others have not changed.

                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #44

                        hi
                        If you place break point and check
                        CellModel::data
                        return this->mundo->world[index.row()][index.column()]->getFood();
                        is row and index what you expect and does getFood() return different values?

                        it looks ok.
                        very much like
                        http://doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-tablemodel-cpp.html

                        J 1 Reply Last reply
                        2
                        • mrjjM mrjj

                          hi
                          If you place break point and check
                          CellModel::data
                          return this->mundo->world[index.row()][index.column()]->getFood();
                          is row and index what you expect and does getFood() return different values?

                          it looks ok.
                          very much like
                          http://doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-tablemodel-cpp.html

                          J Offline
                          J Offline
                          juaniyoalm
                          wrote on last edited by juaniyoalm
                          #45

                          @mrjj Hi!!

                          In the variable index I get the whole world object (contains the matrix). Attached image:

                          0_1543964832588_Selección_001.png

                          I think the problem is in the setData method because if I delete the content of this method (I only leave the return false) the behavior does not change, that is, it continues to paint all row instead of just the one that has been modified.

                          EDIT:

                          When I modify the value with the setValue method of the Cell class, it does not pass through the setData method.

                          mrjjM 1 Reply Last reply
                          0
                          • J juaniyoalm

                            @mrjj Hi!!

                            In the variable index I get the whole world object (contains the matrix). Attached image:

                            0_1543964832588_Selección_001.png

                            I think the problem is in the setData method because if I delete the content of this method (I only leave the return false) the behavior does not change, that is, it continues to paint all row instead of just the one that has been modified.

                            EDIT:

                            When I modify the value with the setValue method of the Cell class, it does not pass through the setData method.

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #46

                            @juaniyoalm
                            Hi
                            is index.row / col always 0,0 ?
                            you can use QDebug to easy see it
                            while running.

                            J 1 Reply Last reply
                            1
                            • mrjjM mrjj

                              @juaniyoalm
                              Hi
                              is index.row / col always 0,0 ?
                              you can use QDebug to easy see it
                              while running.

                              J Offline
                              J Offline
                              juaniyoalm
                              wrote on last edited by juaniyoalm
                              #47

                              @mrjj

                              No, is 0,0 because I have done it this way to test.

                              mrjjM 1 Reply Last reply
                              0
                              • J juaniyoalm

                                @mrjj

                                No, is 0,0 because I have done it this way to test.

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #48

                                @juaniyoalm
                                ok, so it returns same data from any index() / cell ?

                                J 1 Reply Last reply
                                1
                                • mrjjM mrjj

                                  @juaniyoalm
                                  ok, so it returns same data from any index() / cell ?

                                  J Offline
                                  J Offline
                                  juaniyoalm
                                  wrote on last edited by juaniyoalm
                                  #49

                                  @mrjj

                                  No, each cell returns a different value, but in the modification of the UI it modifies all row with the value sent.

                                  EDIT:

                                  Is it normal that every time I touch the main window, the debug enters the data method of the model?

                                  mrjjM 1 Reply Last reply
                                  0
                                  • J juaniyoalm

                                    @mrjj

                                    No, each cell returns a different value, but in the modification of the UI it modifies all row with the value sent.

                                    EDIT:

                                    Is it normal that every time I touch the main window, the debug enters the data method of the model?

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #50

                                    @juaniyoalm
                                    Ok. Im not sure whats up.
                                    Maybe something with indexes().
                                    Cannot guess it.

                                    • the debug enters the data method of the model?
                                      Im not sure what it means ?
                                    J 1 Reply Last reply
                                    1
                                    • mrjjM mrjj

                                      @juaniyoalm
                                      Ok. Im not sure whats up.
                                      Maybe something with indexes().
                                      Cannot guess it.

                                      • the debug enters the data method of the model?
                                        Im not sure what it means ?
                                      J Offline
                                      J Offline
                                      juaniyoalm
                                      wrote on last edited by juaniyoalm
                                      #51

                                      @mrjj

                                      I'm sorry, I wanted to say that, in the execution, when I touch the ui, the execution goes through the Data method and reviews all the indexes.

                                      Was there any way to send you to the app? It's a small app and I need to be able to move forward but I can not find the problem ......

                                      mrjjM 1 Reply Last reply
                                      0
                                      • J juaniyoalm

                                        @mrjj

                                        I'm sorry, I wanted to say that, in the execution, when I touch the ui, the execution goes through the Data method and reviews all the indexes.

                                        Was there any way to send you to the app? It's a small app and I need to be able to move forward but I can not find the problem ......

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #52

                                        @juaniyoalm
                                        Hi
                                        You can link to dropbox or google drive. just zip project folder.

                                        J 1 Reply Last reply
                                        1
                                        • mrjjM mrjj

                                          @juaniyoalm
                                          Hi
                                          You can link to dropbox or google drive. just zip project folder.

                                          J Offline
                                          J Offline
                                          juaniyoalm
                                          wrote on last edited by juaniyoalm
                                          #53

                                          @mrjj

                                          This is the project:
                                          Project

                                          A short summary:

                                          At this time only the most basic is implemented.

                                          Cell Class: Contains an array of fungivores and an integer that represents the fungus. When the number of fungivores increases, the circle of the UI should become larger. When the amount of fungus increases or decreases, the color of the cell of the UI should change.

                                          Fungivore Class: At this moment it is only created so that it can be instantiated.

                                          World Class: Contains an array of fungivores (all fungivores of the app that will then be shared between the cells). In addition, it contains a Vector <Vector <Cell >> (Matrix).

                                          mrjjM 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