Important: Please read the Qt Code of Conduct -

[SOLVED] mvc - update items as result of changing other items

  • Hi,

    I use a QtTreeView with a QStandardItemModel to display and edit some parameters.

    • Name of parameter | Value as Decimal | Value as Hex | Value Calibrated

    I want to edit one of the three values. Then hit Enter and the other two values should be updated. I used the model's itemChanged() signal to update the other two values.

    • Problem 1: loop of updates
      Changing an item in the model emit the itemChanged() signal. This also happens inside the onItemChanged handler if I update the other two values. To prevent this chain of signals I use blockSignals() from the model. I don't know if this is the correct way to handle this?

    • Problem 2: update of tree view lags
      If I use the blockSignals() solution the update of the values is ok. But the values have to be read from a network source at button press and if I change the items in the model with 'item->setText( "the new value" );' inside of my updateFromNetwork-function just the first item is updated but not the other two values. Only after I move the mouse cursor over the row with the values they will be updated in the view. I didn't understand why this happens?

    thx :)

    Reduced code sippets:

    void onItemChanged(...)
        // block signals to prevent loop
        bool bOldBlockSignals = m_dataStoreModel->blockSignals( true );
        // model index of item
        QModelIndex idx = item->index();
        switch ( idx.column() )
                // update hex value
                itemSibling->setText( QString::number( uiValue, 16 ) );
                // update calibrated value
                itemSibling->setText( QString::number( fValue ) );
        } // switch
        m_dataStoreModel->blockSignals( bOldBlockSignals );
    void MainWindow::on_actionUpdateFromNetwork_triggered()
        //... some stuff
        QModelIndex idxItemValue = iter->sibling( currentRow, DATA_MODEL_COLUMN_VALUE );
        QStandardItem *itemValue = m_dataStoreModel->itemFromIndex( idxItemValue );
        // write the value back to the data model
        itemValue->setText( resultStringDouble );

  • Hi,

    I think I find a possible explanation.

    • problem 1: loop of update
      it may be a little bit to restrictive to disable the signals of the model. The QStandardItemModel takes care of not to loop infinetly through the update cycle because if the values are the same during a second (recursive) call of the 'onItemChanged()' handler there will be no further itemChanged() signal. And setting the model to blockSignals() is not necessary.

    • problem2: update of view
      this is solved if I don't block the signals of the view. :)

  • Lifetime Qt Champion


    Why not keep only one value in your model and then depending on the column asked just translate the value in the right format ?

  • Hi,

    sorry for the late answer. I didn't get the notify email.

    The items of the model represent parameters which i read from a memory location of a controller chip. I can only read blocks of bytes. These bytes must be converted to uint8,16,32,int8...,fixedpoint values, depending on a format specification string of the parameter (preconfigured in a parameter spec list).

    At the tree view i have to do some calibration calculation and if i convert to QString and back to fixed point i lost some precision. So i decided to use a user role to save the raw data value at the item model and use the DisplayRole just for a human readable string, limited to a 6-digit-precision.

  • Lifetime Qt Champion

    No problem !

    That's probably because they are not enabled, check your account settings for that.