can not update data of qml tableview from QAbstractTableModel
-
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
-
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
-
J JoeCFD has marked this topic as solved on