Skip to content

Demo how to do two list panels add/remove selection with drag/drop reorder in selected list view

Notifications You must be signed in to change notification settings

yungyeh22/AddRemoveList

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

AddRemoveList

This is my Qt project demonstrating how to do add/remove with two list panel views. What I learned from this project are

  • How to do model/view
  • Moving items between views
  • Drap/drop reorder
  • Installing event filter
  • Connect SIGNAL/SLOT
  • Use QModelIndex
  • Return item from selected view and back to its original order

Here is my idea of handling drag/drop reorder action.

  1. In the constructor
// Enable drag/drop
ui->_listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui->_listView->setDragDropOverwriteMode(false);
ui->_listView->setDragDropMode(QAbstractItemView::DragDrop);
ui->_listView->setDefaultDropAction(Qt::MoveAction);
ui->_listView->setMovement(QListView::Snap);
// Install a event filter to the QListView::viewport()
ui->_listView->viewport()->installEventFilter(this);
  1. Because the default QStandardItem has Qt::ItemIsDropEnable flag set, we have to switch it off. So at the method which you create the items
QStandardItem* item = new QStandardItem();
item->setFlags(selectedItem->flags() ^ (Qt::ItemIsDropEnabled));
  1. Handle the dropEvent() with the event filter. For example:
bool MyWidget::eventFilter(QObject *object, QEvent *event) {
    if (object == ui->_listView->viewport() && event->type() == QEvent::Drop) {
        // Do something when the event fired
    }
    return QObject::eventFilter(object, event);
}
  1. Implement SIGNAL/SLOT for the QStandardItemModel::itemChangeCheck(), QStandardItemModel::rowsInserted(), or QStandardItemModel::rowsRemoved() if necessary as those signals would be triggered by the Drag/Drop action.

About

Demo how to do two list panels add/remove selection with drag/drop reorder in selected list view

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published