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.
  • J juaniyoalm

    But if you modified only the value of a cell if work fine.

    How could I then modify the value? It has to be modified from the world class. Could I put a signal when modified with the setdata method?

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

    @juaniyoalm
    Hi
    You can add add signal to World class and connect it to the model. and then emit it
    to have model call DataChanged. OR give World a pointer to the model and use models
    API to change data and inform view.

    • But if you modified only the value of a cell if work fine.
      im not sure what u say here.
    J 1 Reply Last reply
    0
    • mrjjM mrjj

      @juaniyoalm
      Hi
      You can add add signal to World class and connect it to the model. and then emit it
      to have model call DataChanged. OR give World a pointer to the model and use models
      API to change data and inform view.

      • But if you modified only the value of a cell if work fine.
        im not sure what u say here.
      J Offline
      J Offline
      juaniyoalm
      wrote on last edited by
      #69

      @mrjj
      I want to say that if I do this

      getCell(0,0)->setFood(rand()&255);
           
      

      The UI update fine and model unknown data either

      mrjjM 1 Reply Last reply
      0
      • J juaniyoalm

        @mrjj
        I want to say that if I do this

        getCell(0,0)->setFood(rand()&255);
             
        

        The UI update fine and model unknown data either

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

        @juaniyoalm
        Ok. not really sure how,
        but didnt check the latest version.
        Also if the VIEW get an update/paintEvent it will re-read the indexes so i guess
        its via the normal paint/update/redraw.

        J 1 Reply Last reply
        1
        • mrjjM mrjj

          @juaniyoalm
          Ok. not really sure how,
          but didnt check the latest version.
          Also if the VIEW get an update/paintEvent it will re-read the indexes so i guess
          its via the normal paint/update/redraw.

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

          @mrjj

          I have already solved the problem. I have created a signal in the Cell class that is emited when the value of the cell is modified. This signal sends the cell to the slots. Then I created a slot in the MainView class and connected each cell of the model's matrix with that slot. This slot creates a QModelIndex and issues a dataChanged for the model to update the view. Besides, I had to save the position of the cell in the cell class.

          Emit Signal:

          void Cell::setFood(int food)
          {
              if(this->fungus != food) {
                  this->fungus = food;
                  emit this->updateModel(this);
              }
          }
          

          Slot:

          void MainWindow::foodChanged(Cell *cell)
          {
              QModelIndex aux = model->index(cell->place.first, cell->place.second);
              model->dataChanged(aux, aux);
          }
          

          Connections:

              for (int i = 0; i < this->rows; i++) {
                  for (int j = 0; j < this->columns; j++) {
                      connect(this->model->mundo->getCell(i, j), &Cell::updateModel, this, &MainWindow::foodChanged);
                  }
              }
          

          Now I need pass the value of the circle also in the data method so that the delegate's paint method can receive it ...
          Any example of how to do it?

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

            As I already suggested many times: use custom roles. So in your paint function, request the data using the roles specific to the parameters you want to get.

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

            J 1 Reply Last reply
            0
            • SGaistS SGaist

              As I already suggested many times: use custom roles. So in your paint function, request the data using the roles specific to the parameters you want to get.

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

              @SGaist

              Yes, but I unknown how to do it...

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #74
                QVariant data = model->data(MyModel::MyCustomRole);
                

                In your model:

                QVariant MyModelModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const
                {
                // usual stuff
                if (role == MyModel::MyCustomRole) {
                    return mound->getCell(index.row(), index.column())->myCoolProperty;
                }
                }
                

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

                J 1 Reply Last reply
                2
                • SGaistS SGaist
                  QVariant data = model->data(MyModel::MyCustomRole);
                  

                  In your model:

                  QVariant MyModelModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const
                  {
                  // usual stuff
                  if (role == MyModel::MyCustomRole) {
                      return mound->getCell(index.row(), index.column())->myCoolProperty;
                  }
                  }
                  
                  J Offline
                  J Offline
                  juaniyoalm
                  wrote on last edited by juaniyoalm
                  #75

                  @SGaist

                  But I need both values to be at the same time in the delegate's paint method.

                  I modify the value of the items of the cells in internal methods of the World class, not from model, then emit signal that is received by the slot located in the MainWindows class and this emit dataChange signal of the model, as I wrote before.

                  mrjjM 1 Reply Last reply
                  0
                  • J juaniyoalm

                    @SGaist

                    But I need both values to be at the same time in the delegate's paint method.

                    I modify the value of the items of the cells in internal methods of the World class, not from model, then emit signal that is received by the slot located in the MainWindows class and this emit dataChange signal of the model, as I wrote before.

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

                    @juaniyoalm
                    Hi
                    Can we talk about the actual data ?
                    What is the data for the circle ?
                    you say "value of the circle"
                    can u specify what that means ?
                    The delegate has access to the cell so it can have any values at the same time.
                    So to avoid confusing, could you tell what data you need to paint the circle ?

                    J 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @juaniyoalm
                      Hi
                      Can we talk about the actual data ?
                      What is the data for the circle ?
                      you say "value of the circle"
                      can u specify what that means ?
                      The delegate has access to the cell so it can have any values at the same time.
                      So to avoid confusing, could you tell what data you need to paint the circle ?

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

                      @mrjj
                      Yeah, I need size array of fungivores in each cell to paint circle. This value can get it whit a method of Cell class.

                      The data method of model can you see above.

                      mrjjM 1 Reply Last reply
                      0
                      • J juaniyoalm

                        @mrjj
                        Yeah, I need size array of fungivores in each cell to paint circle. This value can get it whit a method of Cell class.

                        The data method of model can you see above.

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

                        @juaniyoalm
                        well just do as @SGaist suggests and the delegate can use your custom role(s)
                        to get the data it needs.
                        user roles are nothing complicated at all.
                        its just an id that let you assign a vlue to that id for an index.
                        like
                        model->setData(model->index(0,0), 90 , Qt::UserRole + 1 );

                        To avoid to ugly code and stupid mistakes, we define it as a const.
                        constexpr int CircleData = Qt::UserRole+1;
                        constexpr int SomeOtherData = Qt::UserRole+2;

                        and use instead of the raw value. This is a must to do. :)

                        J 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @juaniyoalm
                          well just do as @SGaist suggests and the delegate can use your custom role(s)
                          to get the data it needs.
                          user roles are nothing complicated at all.
                          its just an id that let you assign a vlue to that id for an index.
                          like
                          model->setData(model->index(0,0), 90 , Qt::UserRole + 1 );

                          To avoid to ugly code and stupid mistakes, we define it as a const.
                          constexpr int CircleData = Qt::UserRole+1;
                          constexpr int SomeOtherData = Qt::UserRole+2;

                          and use instead of the raw value. This is a must to do. :)

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

                          @mrjj
                          But the problem is that I don't change the value with model->setData...., as I told you, I change it with methods place in Cell class and after emit signals to notified to model and call dataChanged method...

                          mrjjM 1 Reply Last reply
                          0
                          • J juaniyoalm

                            @mrjj
                            But the problem is that I don't change the value with model->setData...., as I told you, I change it with methods place in Cell class and after emit signals to notified to model and call dataChanged method...

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

                            @juaniyoalm

                            i dont see that matters with roles.
                            the delegate ask model and model get from the vector/cell so
                            should just be fine.

                            J 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @juaniyoalm

                              i dont see that matters with roles.
                              the delegate ask model and model get from the vector/cell so
                              should just be fine.

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

                              @mrjj

                              I can not return a cell in the data method of the model because it gives me an error:

                              0_1544553267398_Selección_002.png

                              mrjjM 1 Reply Last reply
                              0
                              • J juaniyoalm

                                @mrjj

                                I can not return a cell in the data method of the model because it gives me an error:

                                0_1544553267398_Selección_002.png

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

                                @juaniyoalm
                                you try to return a Cell pointer.
                                Should be like
                                mundo->getCell(index.row(), index.column())->somePublicVariable
                                or
                                mundo->getCell(index.row(), index.column())->Func(); // return some data, int , float , etc

                                Why you want to return the cell pointer ?
                                Just return some of its data

                                J 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @juaniyoalm
                                  you try to return a Cell pointer.
                                  Should be like
                                  mundo->getCell(index.row(), index.column())->somePublicVariable
                                  or
                                  mundo->getCell(index.row(), index.column())->Func(); // return some data, int , float , etc

                                  Why you want to return the cell pointer ?
                                  Just return some of its data

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

                                  @mrjj

                                  I think I'm not understanding ... hah.

                                  If I put the following:

                                  0_1544553931277_Selección_003.png

                                  When I modify the value in this method:

                                  0_1544554094879_Selección_004.png

                                  Or these:

                                  0_1544554184318_Selección_005.png

                                  That signals activate this method (slot):

                                  0_1544554310170_Selección_006.png

                                  But in no time I pass the role and then the model does not know what data to return...

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

                                    Hi
                                    The view itself ask for
                                    Qt::DisplayRole and Qt::EditRole ( if u edit cell )
                                    the new roles was for the delegate to request data it needs.

                                    You did study for more than a few minutes the
                                    http://doc.qt.io/qt-5/model-view-programming.html
                                    right ?

                                    J 1 Reply Last reply
                                    1
                                    • mrjjM mrjj

                                      Hi
                                      The view itself ask for
                                      Qt::DisplayRole and Qt::EditRole ( if u edit cell )
                                      the new roles was for the delegate to request data it needs.

                                      You did study for more than a few minutes the
                                      http://doc.qt.io/qt-5/model-view-programming.html
                                      right ?

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

                                      @mrjj

                                      Ok, I understand now. The problems are solved:

                                      QVariant CellModel::data(const QModelIndex &index, int role) const
                                      {
                                          if(!index.isValid())
                                              return QVariant();
                                      
                                          if(index.row() >= mundo->getWorld().size() || index.row() < 0 ||
                                                 index.column() >= mundo->getWorld()[0].size() || index.column() < 0)
                                              return QVariant();
                                      
                                          if(role == CircleSize)
                                          {
                                              return this->mundo->getCell(index.row(), index.column())->getFungivoresSize();
                                          }
                                      
                                          if(role == FoodCount)
                                          {
                                              return this->mundo->getCell(index.row(), index.column())->getFood();
                                          }
                                          return QVariant();
                                      }
                                      

                                      And paint method:

                                      void CellDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
                                      {
                                          int cellValue = index.model()->data(index, Qt::UserRole+2).toInt();
                                          qreal circleValue = index.model()->data(index, Qt::UserRole+1).toInt();
                                      
                                          qreal circleRealValue = (circleValue*20)/50;
                                      
                                          painter->save();
                                          painter->fillRect(option.rect, QBrush(QColor((255 - cellValue), 255, 51)));
                                      
                                          painter->setBrush(Qt::blue);
                                          painter->drawEllipse(static_cast<QPointF>(option.rect.center()), circleRealValue, circleRealValue);
                                          painter->setRenderHint(QPainter::Antialiasing, true);
                                          painter->setPen(Qt::NoPen);
                                      
                                          painter->restore();
                                      }
                                      
                                      1 Reply Last reply
                                      2
                                      • mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #86

                                        Hi
                                        Super.
                                        Note you should try to reuse the CircleSize, FoodCount constexpr if possible
                                        instead of Qt::UserRole+1.
                                        you can just make a .h file and put them there and let both
                                        model and delegate include it.

                                        J 1 Reply Last reply
                                        1
                                        • mrjjM mrjj

                                          Hi
                                          Super.
                                          Note you should try to reuse the CircleSize, FoodCount constexpr if possible
                                          instead of Qt::UserRole+1.
                                          you can just make a .h file and put them there and let both
                                          model and delegate include it.

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

                                          @mrjj

                                          Ok I have already done it.

                                          I want to thank all of you who have tried to help me, especially @mrjj and @SGaist . You have had a lot of patience with me. Thank you.

                                          Surely I will have more questions about the project later. I'll stay here.

                                          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