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. QTableView, QStandardItemModel and drag and drop sorting
QtWS25 Last Chance

QTableView, QStandardItemModel and drag and drop sorting

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.6k 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.
  • S Offline
    S Offline
    steveq
    wrote on 23 Apr 2018, 13:25 last edited by
    #1

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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Diracsbracket
      wrote on 23 Apr 2018, 17:33 last edited by Diracsbracket
      #2

      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 );
             ...
          }
      
      1 Reply Last reply
      3
      • S Offline
        S Offline
        steveq
        wrote on 24 Apr 2018, 01:19 last edited by
        #3

        Hi Diracsbracket,

        That's exactly what I was after! Legend.

        Thanks so much for your help!

        Steve Q. :-)

        1 Reply Last reply
        0

        1/3

        23 Apr 2018, 13:25

        • Login

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