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. [SOLVED] mvc - update items as result of changing other items

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

Scheduled Pinned Locked Moved General and Desktop
mvcqtreeviewqstandarditemmo
5 Posts 2 Posters 1.9k 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.
  • sahadumS Offline
    sahadumS Offline
    sahadum
    wrote on last edited by sahadum
    #1

    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() )
        {
        case DATA_MODEL_COLUMN_VALUE:
                // update hex value
                ...
                itemSibling->setText( QString::number( uiValue, 16 ) );
                // update calibrated value
                ...
                itemSibling->setText( QString::number( fValue ) );
                break;
    
        case DATA_MODEL_COLUMN_VALUE_HEX:
                ...
                break;
        case DATA_MODEL_COLUMN_VALUE_CALIBRATED:
                ...
                break;
        } // 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 );
        //...somestuff
    }
    
    1 Reply Last reply
    0
    • sahadumS Offline
      sahadumS Offline
      sahadum
      wrote on last edited by
      #2

      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. :)

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by SGaist
        #3

        Hi,

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

        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
        1
        • sahadumS Offline
          sahadumS Offline
          sahadum
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            No problem !

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

            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

            • Login

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