PDA

View Full Version : QTableView drag-n-drop to change order, an empty row remains



leonard
5th March 2014, 00:33
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::ExtendedSelect ion);
// 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
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 with default values (zeros) 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.

neuronet
12th October 2014, 02:07
I had this (and other) problems with QTableWidget. Got some help with it here:
http://stackoverflow.com/questions/26227885/drag-and-drop-rows-within-qtablewidget