Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableView, QStandardItemModel and drag and drop sorting



  • Hey All,

    I've found quite a few search results on this topic but still don't have an answer to my question.

    I've created a QTableView with a QStandardItemModel and drag and drop enabled like so:

        m_model = new QStandardItemModel( 0, 2, this);
    
        m_model->setHorizontalHeaderItem(0, new QStandardItem(QString("Switch Name")));
        m_model->setHorizontalHeaderItem(1, new QStandardItem(QString("Mnemonic")));
    
        ui->mnemonicTable->verticalHeader()->setSectionsMovable(true);
        ui->mnemonicTable->setDragEnabled(true);
        ui->mnemonicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
        ui->mnemonicTable->setSelectionMode(QAbstractItemView::SingleSelection);
        ui->mnemonicTable->setDragDropMode(QAbstractItemView::InternalMove);
        ui->mnemonicTable->setDropIndicatorShown(true);
        ui->mnemonicTable->setDragDropOverwriteMode(false);
    
        ui->mnemonicTable->setModel(m_model);
    

    Visually I am able to drag and drop to sort the table rows successfully. However once I retrieve the data from the table, it is still in the same order it was in prior to the sort. I traverse the table data like this:

        // Save out the switch name and its mnemonic
        for ( int i = 0; i < m_model->rowCount(); i++ )
        {
            key = "Switch" + QString::number(i + 1);
    
            item = m_model->item( i, 0 );
    
            if ( item ) switchName = item->text();
    
            item = m_model->item( i, 1 );
    
            if ( item ) mnemonic = item->text();
    
            if ( mnemonic.isEmpty() ) mnemonic = "????";
    
            iniReaderSave.beginGroup( key );
            iniReaderSave.setValue( "SwitchName", switchName );
            iniReaderSave.setValue( "Mnemonic", mnemonic );
            iniReaderSave.endGroup();
    
            count++;
    
            mnemonic = "";
        }
    
    

    It's like the table is not passing on the re-sort to the model.

    • Could someone possibly clear this up for me please. What am I doing wrong?

    Many thanks,

    Steve Q. :-)



  • Hi.
    You can save the visual order of your rows by mapping the visual indices of the verticalHeader object to their logical indices, for example:

        // Save out the switch name and its mnemonic
        for ( int i = 0; i < m_model->rowCount(); i++ )
        {
            key = "Switch" + QString::number(i + 1);
            
            const int k = ui->mnemonicTable->verticalHeader()->logicalIndex(i);
            item = m_model->item( k, 0 );
    
            if ( item ) switchName = item->text();
    
            item = m_model->item( k, 1 );
           ...
        }
    


  • Hi Diracsbracket,

    That's exactly what I was after! Legend.

    Thanks so much for your help!

    Steve Q. :-)


Log in to reply