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
    #60

    As long as your model stores all the data you need, your delegate can access them. Use custom roles for these additional informations. This has the added advantage to make your code easier to read and understand.

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

      @mrjj @SGaist Hi!!

      If I put QCoreApplication::processEvents(); the UI is not updated until, for example, I do not select the cells in the UI. I need them to be updated automatically when I change the values from a method of the world class. I can not use timer and the values change each time range because this application will later have a parallel version and what is searched in that app is the simulation of a real biological environment as quickly as possible. I need a solution, because I'm delaying too much at the point of the UI (it's the least important).

      I want to thank you for all the help you have given me, but I need to solve this point.

      The method that will modify the values is not this, but it will look like this:

      void World::initializeWorld() {
      
          int count = 0;
      
          while (count < 1500) {
              for(int i = 0; i < rowSize(); i++) {
                  for(int j = 0; j < rowSize(); j++) {
                      getCell(i,j)->setFood(rand()&255);
                  }
              }
              count++;
               QCoreApplication::processEvents();
          }
      }
      

      I put the updated project link in case you want to look at it:

      Update Project

      mrjjM 1 Reply Last reply
      0
      • J juaniyoalm

        @mrjj @SGaist Hi!!

        If I put QCoreApplication::processEvents(); the UI is not updated until, for example, I do not select the cells in the UI. I need them to be updated automatically when I change the values from a method of the world class. I can not use timer and the values change each time range because this application will later have a parallel version and what is searched in that app is the simulation of a real biological environment as quickly as possible. I need a solution, because I'm delaying too much at the point of the UI (it's the least important).

        I want to thank you for all the help you have given me, but I need to solve this point.

        The method that will modify the values is not this, but it will look like this:

        void World::initializeWorld() {
        
            int count = 0;
        
            while (count < 1500) {
                for(int i = 0; i < rowSize(); i++) {
                    for(int j = 0; j < rowSize(); j++) {
                        getCell(i,j)->setFood(rand()&255);
                    }
                }
                count++;
                 QCoreApplication::processEvents();
            }
        }
        

        I put the updated project link in case you want to look at it:

        Update Project

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

        @juaniyoalm
        Hi
        The timer is meant as a way of not strangulating the event loop.
        its not as fast a tight loop as it goes over the event loop but it
        allows redraw of the view. ( which also uses the event loop to actually redraw)
        Anyway, you are looping row x colSize ( actually rox x row ??)
        and then allow event loop.
        maybe you mean like this ?

         while (count < 1500) {
                for(int i = 0; i < rowSize(); i++) {
                    for(int j = 0; j < rowSize(); j++) { // should this not be colSize ?
                        getCell(i,j)->setFood(rand()&255);
                       QCoreApplication::processEvents();
                    }
                }
                count++;
              
            }
        
        J 1 Reply Last reply
        2
        • mrjjM mrjj

          @juaniyoalm
          Hi
          The timer is meant as a way of not strangulating the event loop.
          its not as fast a tight loop as it goes over the event loop but it
          allows redraw of the view. ( which also uses the event loop to actually redraw)
          Anyway, you are looping row x colSize ( actually rox x row ??)
          and then allow event loop.
          maybe you mean like this ?

           while (count < 1500) {
                  for(int i = 0; i < rowSize(); i++) {
                      for(int j = 0; j < rowSize(); j++) { // should this not be colSize ?
                          getCell(i,j)->setFood(rand()&255);
                         QCoreApplication::processEvents();
                      }
                  }
                  count++;
                
              }
          
          J Offline
          J Offline
          juaniyoalm
          wrote on last edited by juaniyoalm
          #63

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

          QCoreApplication::processEvents();

          Row x Row is because the rows will really be the same as the columns.

          I have also tried to locate QCoreApplication :: processEvents (); in that position, with the same effect, that is, it does not update well in the UI.

          If the timer does not penalize the execution time of the program and only acts in the reprssentación of the UI, it could be good. How should I do it?

          mrjjM 1 Reply Last reply
          0
          • J juaniyoalm

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

            QCoreApplication::processEvents();

            Row x Row is because the rows will really be the same as the columns.

            I have also tried to locate QCoreApplication :: processEvents (); in that position, with the same effect, that is, it does not update well in the UI.

            If the timer does not penalize the execution time of the program and only acts in the reprssentación of the UI, it could be good. How should I do it?

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

            Hi

            • it does not update well in the UI.

            meaning it does update sometimes or what result do you get ?

            here is sample of changing random cell

            int random(int min, int max) //range : [min, max)
            {
                static bool first = true;
                if (first) {
                    srand( time(nullptr) ); //seeding for the first time only!
                    first = false;
                }
                return min + rand() % (( max + 1 ) - min);
            }
            
            void MainWindow::changeVal()
            {
                //model->mundo->world[4][4]->setFood(200);
                auto timer = new QTimer(this);
                QObject::connect(timer, &QTimer::timeout, [this]() {
                    int rowCount = this->model->rowCount(QModelIndex());
                    int colCount = this->model->columnCount(QModelIndex());
                    this->model->setData(model->index(random(0, rowCount), random(0, colCount)), random(0, 20), Qt::EditRole);
                });
                timer->start(10);
            
            }
            Note that sample will create a timer each time you select Change val menu so
            its better to move timer as member. 
            Also it uses a lambda as slot. you can use a normal slot for it and let it be part of World class.
            
            • If the timer does not penalize the execution time of the program
              it does to some degree. But even using Threads, you will have to ask it to update and redraw
              over the event loop as the VIEW cannot update if you strangulate it using tight loops.

            But using timer is not super slow.
            alt text
            (video is actually slower than real deal)

            J 1 Reply Last reply
            2
            • mrjjM mrjj

              Hi

              • it does not update well in the UI.

              meaning it does update sometimes or what result do you get ?

              here is sample of changing random cell

              int random(int min, int max) //range : [min, max)
              {
                  static bool first = true;
                  if (first) {
                      srand( time(nullptr) ); //seeding for the first time only!
                      first = false;
                  }
                  return min + rand() % (( max + 1 ) - min);
              }
              
              void MainWindow::changeVal()
              {
                  //model->mundo->world[4][4]->setFood(200);
                  auto timer = new QTimer(this);
                  QObject::connect(timer, &QTimer::timeout, [this]() {
                      int rowCount = this->model->rowCount(QModelIndex());
                      int colCount = this->model->columnCount(QModelIndex());
                      this->model->setData(model->index(random(0, rowCount), random(0, colCount)), random(0, 20), Qt::EditRole);
                  });
                  timer->start(10);
              
              }
              Note that sample will create a timer each time you select Change val menu so
              its better to move timer as member. 
              Also it uses a lambda as slot. you can use a normal slot for it and let it be part of World class.
              
              • If the timer does not penalize the execution time of the program
                it does to some degree. But even using Threads, you will have to ask it to update and redraw
                over the event loop as the VIEW cannot update if you strangulate it using tight loops.

              But using timer is not super slow.
              alt text
              (video is actually slower than real deal)

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

              @mrjj

              meaning it does update sometimes or what result do you get ?

              Means that the UI is only updated if I interact with it, that is, if for example I select the cells, then it shows the changes made in the code.

              But using timer is not super slow

              Yes but in the parallel version it should go faster and if I use a timer, it will always take the same time, because the time depends on the timer variable.

              Video with an example of what happens to me:

              Video

              Change button calls the InitializeWorld() method.

              mrjjM 1 Reply Last reply
              0
              • J juaniyoalm

                @mrjj

                meaning it does update sometimes or what result do you get ?

                Means that the UI is only updated if I interact with it, that is, if for example I select the cells, then it shows the changes made in the code.

                But using timer is not super slow

                Yes but in the parallel version it should go faster and if I use a timer, it will always take the same time, because the time depends on the timer variable.

                Video with an example of what happens to me:

                Video

                Change button calls the InitializeWorld() method.

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

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

                • it will always take the same time, because the time depends on the timer variable.

                Not really. the timer will call your function at a fix time. Your function will take the time it takes but using a timer, its all driven like a loop but allows the event to run to the view can update.

                Anyway, what you are seeing is expected.
                You are modifying the vector list BEHIND models back. It dont know you changed some of its data so first when you click something, view re-reads the cells and draws the values.

                Recall the setData.

                bool CellModel::setData(const QModelIndex &index, const QVariant &value, int role)
                {
                    auto RowCount = this->mundo->world.size();
                    auto ColCount = this->mundo->world[0].size();
                    qDebug () << "role=" << role;
                    if (index.isValid()            
                            && role == Qt::EditRole
                            && index.row() < RowCount
                            && index.row() >= 0
                            && index.column() <  ColCount
                            && 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;
                }
                

                here we emit dataChanged so view knows something changed.
                when you do
                getCell(i,j)->setFood(rand()&255);
                you are not telling model/view you changed the list and that is why
                QCoreApplication::processEvents(); do not help.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  juaniyoalm
                  wrote on last edited by
                  #67

                  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 1 Reply Last reply
                  0
                  • 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

                                          • Login

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