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

QTableView drag-n-drop to change order, empty row left



  • Dear all,

    I use a QTableView to display products information described with QStandardItemModel. I need to implement the feature to change row order via drag-and-drop. Here is the code:
    .h file:
    @
    QTableView *tableView_allProducts
    QStandardItemModel *ensembleProductModel;
    @

    .cpp file
    @
    ensembleProductModel = new QStandardItemModel(0, PRODUCT_COLUMN_COUNT);
    tableView_allProducts->setModel(ensembleProductModel);
    tableView_allProducts->setSelectionMode(QAbstractItemView::ExtendedSelection);
    // When user select an item, the whole row is selected
    tableView_allProducts->setSelectionBehavior(QAbstractItemView::SelectRows);
    // Implement the feature to change row order via drag-n-drop
    // TODO: this feature is not done right yet
    tableView_allProducts->setDragEnabled(true);
    tableView_allProducts->setDragDropMode(QAbstractItemView::InternalMove);
    tableView_allProducts->setDropIndicatorShown(true);
    @

    The problem with the above code is that after the selected row is moved to the new position, an empty row remains at the original position. So there is one more row after drag-n-drop. What's problem with my code? Thanks.

    My development tools are Visual Studio 2008, Qt 4.8.3.


Log in to reply