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. can not update data of qml tableview from QAbstractTableModel
Forum Updated to NodeBB v4.3 + New Features

can not update data of qml tableview from QAbstractTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 245 Views
  • 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.
  • JoeCFDJ Offline
    JoeCFDJ Offline
    JoeCFD
    wrote on last edited by JoeCFD
    #1

    I have a column of buttons dynamically created with loader in a qml tableview.
    All buttons have default grey color. When a button A is clicked, a green color is set to the button A to show it is active. If the button A is clicked again, the same button will have the default grey color.

    But if another button B is clicked while button A is on, button A will be set inactive with grey color and button B will be highlighted with green color. For doing this, I have a vector of bool to keep the (active or inactive) states of all buttons and this vector works fine at start-up.

    My problem is I can not update the state of any button by calling func setData or my own func updateButtonState in a model class inherited from QAbstractTableModel to change the state of any Button with emit dataChanged signal. What is the right way to update table model data?

    I have a listview with QAbstractLListModel and no problems with update.

    Button Delegate

                 Component {
                        id: buttonDelegate
                        ClickButton {
                            text: model.name
                            active: model.active
    
                            onClicked: {
                                //active = !active
                                if ( -1 !== currentIndex ) {
                                    /* clear current selection */
                                    tableViewModel.activateButton( currentIndex, false )
                                }
    
                                if ( currentIndex !== index ) {
                                    currentIndex = index
                                }
                                else {
                                    currentIndex = -1
                                }
    
                                if ( -1 !== currentIndex ) {
                                    tableViewModel.activateButton( currentIndex, true )
                                }              
                            }
                        }
                    }
       
    

    ClickButton is a customized Button and currentIndex is a global int in the TableView

    class TableViewModel : public QAbstractTableModel
    public:
       Q_INVOKABLE void activateButton( int row_index, bool value );
    
    private:
        std::vector< bool > m_buttonActiveStateList;
    };
     
    void TableViewModel::activateButton( int row_index, bool value )
    {
        m_buttonActiveStateList[ row_index ] = value;
        QModelIndex current_idx = index( row_index, 2 );
        emit dataChanged( current_idx, current_idx, { ActiveRole } );
    }
    

    I confirmed that activateButton is called.

    Ubuntu 22.04 and Qt 5.15.3

    JoeCFDJ 1 Reply Last reply
    0
    • JoeCFDJ JoeCFD

      I have a column of buttons dynamically created with loader in a qml tableview.
      All buttons have default grey color. When a button A is clicked, a green color is set to the button A to show it is active. If the button A is clicked again, the same button will have the default grey color.

      But if another button B is clicked while button A is on, button A will be set inactive with grey color and button B will be highlighted with green color. For doing this, I have a vector of bool to keep the (active or inactive) states of all buttons and this vector works fine at start-up.

      My problem is I can not update the state of any button by calling func setData or my own func updateButtonState in a model class inherited from QAbstractTableModel to change the state of any Button with emit dataChanged signal. What is the right way to update table model data?

      I have a listview with QAbstractLListModel and no problems with update.

      Button Delegate

                   Component {
                          id: buttonDelegate
                          ClickButton {
                              text: model.name
                              active: model.active
      
                              onClicked: {
                                  //active = !active
                                  if ( -1 !== currentIndex ) {
                                      /* clear current selection */
                                      tableViewModel.activateButton( currentIndex, false )
                                  }
      
                                  if ( currentIndex !== index ) {
                                      currentIndex = index
                                  }
                                  else {
                                      currentIndex = -1
                                  }
      
                                  if ( -1 !== currentIndex ) {
                                      tableViewModel.activateButton( currentIndex, true )
                                  }              
                              }
                          }
                      }
         
      

      ClickButton is a customized Button and currentIndex is a global int in the TableView

      class TableViewModel : public QAbstractTableModel
      public:
         Q_INVOKABLE void activateButton( int row_index, bool value );
      
      private:
          std::vector< bool > m_buttonActiveStateList;
      };
       
      void TableViewModel::activateButton( int row_index, bool value )
      {
          m_buttonActiveStateList[ row_index ] = value;
          QModelIndex current_idx = index( row_index, 2 );
          emit dataChanged( current_idx, current_idx, { ActiveRole } );
      }
      

      I confirmed that activateButton is called.

      Ubuntu 22.04 and Qt 5.15.3

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      @JoeCFD fixed by calling setData with proper role index.

      1 Reply Last reply
      0
      • JoeCFDJ JoeCFD has marked this topic as solved on

      • Login

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