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

                      @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 Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #54

                      @juaniyoalm
                      Hi
                      some function was missing in NewDialog but i just made them return a valid value.
                      You model actually seems fine (\o/)
                      but i found out you would fill the whole
                      row with same Cell , it seems.
                      Doing this instead
                      alt text

                      and little change to Cell ( to to be able to see difference)

                      Cell::Cell(QObject *parent) : QObject(parent)
                      {
                          this->fungus = 10 + (rand() % 5);
                      }
                      
                      

                      It now seems to edit fine :)

                      J 1 Reply Last reply
                      2
                      • mrjjM mrjj

                        @juaniyoalm
                        Hi
                        some function was missing in NewDialog but i just made them return a valid value.
                        You model actually seems fine (\o/)
                        but i found out you would fill the whole
                        row with same Cell , it seems.
                        Doing this instead
                        alt text

                        and little change to Cell ( to to be able to see difference)

                        Cell::Cell(QObject *parent) : QObject(parent)
                        {
                            this->fungus = 10 + (rand() % 5);
                        }
                        
                        

                        It now seems to edit fine :)

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

                        @mrjj Thank you so much!!

                        But if you modify a cell with the setfood method, does it modify it well?

                        mrjjM 1 Reply Last reply
                        0
                        • J juaniyoalm

                          @mrjj Thank you so much!!

                          But if you modify a cell with the setfood method, does it modify it well?

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

                          @juaniyoalm
                          Hi
                          You mean via

                          void MainWindow::changeVal()
                          {
                              model->mundo->world[0][0]->setFood(200);
                          }
                          
                          

                          Yes it does, but you first see it if u click in cell as you secretly change it directly in list
                          and the model dont know.

                          so do like this
                          model->setData(model->index(2,2),200,Qt::EditRole);
                          and it refesh it self.

                          alt text

                          J 1 Reply Last reply
                          2
                          • mrjjM mrjj

                            @juaniyoalm
                            Hi
                            You mean via

                            void MainWindow::changeVal()
                            {
                                model->mundo->world[0][0]->setFood(200);
                            }
                            
                            

                            Yes it does, but you first see it if u click in cell as you secretly change it directly in list
                            and the model dont know.

                            so do like this
                            model->setData(model->index(2,2),200,Qt::EditRole);
                            and it refesh it self.

                            alt text

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

                            @mrjj

                            Thanks so much!!

                            The problem was in World constructor , as you said.

                            once solved this problem, as you will have noticed, I only change the color of the cell, and I also need to change the size of the circle, according to the number of fungivores that there are. How can I pass this data to the model, or to differentiate what data is happening to it?

                            EDIT:

                            Another problem is that if I modify the values in a loop, for example:

                                int count = 0;
                                while (count < 3000) {
                                    for(int i = 0; i < this->rows; i++) {
                                        for(int j = 0; j < this->col; j++) {
                                            model->mundo->world[i][j]->setFood(rand() % 255);
                                            //model->setData(model->index(i,j),200,Qt::EditRole);
                                            qDebug("El ModelIndex[%i][%i] lleva los datos: %i", i, j, model->mundo->world[i][j]->getFood());
                                        }
                                    }
                            
                                    count++;
                                }
                            

                            The UI is not updated until the loop completes. It may be that the loop strangles the events ... that's a big problem for me.

                            mrjjM 1 Reply Last reply
                            0
                            • J juaniyoalm

                              @mrjj

                              Thanks so much!!

                              The problem was in World constructor , as you said.

                              once solved this problem, as you will have noticed, I only change the color of the cell, and I also need to change the size of the circle, according to the number of fungivores that there are. How can I pass this data to the model, or to differentiate what data is happening to it?

                              EDIT:

                              Another problem is that if I modify the values in a loop, for example:

                                  int count = 0;
                                  while (count < 3000) {
                                      for(int i = 0; i < this->rows; i++) {
                                          for(int j = 0; j < this->col; j++) {
                                              model->mundo->world[i][j]->setFood(rand() % 255);
                                              //model->setData(model->index(i,j),200,Qt::EditRole);
                                              qDebug("El ModelIndex[%i][%i] lleva los datos: %i", i, j, model->mundo->world[i][j]->getFood());
                                          }
                                      }
                              
                                      count++;
                                  }
                              

                              The UI is not updated until the loop completes. It may be that the loop strangles the events ... that's a big problem for me.

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

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

                              How can I pass this data to the model, or to differentiate what data is happening to it?

                              But the model has already access to the Cell structure. ( via model)
                              So delegate can read the information and calculate the size of circle with more.

                              Regarding the loop.
                              Yes, i think the loop will strangle the event loop as 3000 x row x col might take some time.
                              One option is to use QCoreApplication::processEvents in the loop but thats not so pretty.
                              It would be better to use a timer to change the food over time and keep the UI responsive.

                              J 1 Reply Last reply
                              0
                              • mrjjM mrjj

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

                                How can I pass this data to the model, or to differentiate what data is happening to it?

                                But the model has already access to the Cell structure. ( via model)
                                So delegate can read the information and calculate the size of circle with more.

                                Regarding the loop.
                                Yes, i think the loop will strangle the event loop as 3000 x row x col might take some time.
                                One option is to use QCoreApplication::processEvents in the loop but thats not so pretty.
                                It would be better to use a timer to change the food over time and keep the UI responsive.

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

                                @mrjj

                                But the delegate receives the data of the model and this only has the value of the food (color).

                                Regarding the timer. I need not delay the execution time, because later I must see the difference between parallel version of the app and this version.

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

                                          • Login

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