Frage

I working on a small QlistView which is Sortable.

    iListView = new QListView(this);
    //Creating a standard item model
    iStandardModel = new QStandardItemModel(this);
    
    //First item
    QStandardItem* item1 = new QStandardItem(QIcon(":/cover-story-album-art.jpg"),"First Item");
    //Second item
    QStandardItem* item2 = new QStandardItem(QIcon(":/cover-story-album-art.jpg"),"Second item");
    //third item 
    QStandardItem* item3 = new QStandardItem(QIcon(":/cover-story-album-art.jpg"),"Third item");
    
    //Appending the items into model
    iStandardModel->appendRow(item1);
    iStandardModel->appendRow(item2);
    iStandardModel->appendRow(item3);
    
    //Setting the icon size
    iListView->setIconSize(QSize(40,30));
    
    //Setting the model
    iListView->setModel(iStandardModel);
    
    //Setting listview geometry
    iListView->setGeometry(QRect(0,0,240,320));
    iListView->setDragEnabled(true);
    iListView->setAcceptDrops(true);
    iListView->setDragDropMode(QAbstractItemView::InternalMove);

Well the Drag and Drop works but there is a issuse if i drop the item on the any other item replaced other than the end of the list.The "dragged" item replaces the "released up on" item.

Screen shot of QListView at different Scenarios Screen shot of QListView  at different Scenarios

War es hilfreich?

Lösung

That is because by default QStandardItem has Qt::ItemIsDropEnabled flag set. Just remove it by using QStandardItem::setFlags() function. Add following lines:

item1->setFlags(item1->flags() ^ (Qt::ItemIsDropEnabled));
item2->setFlags(item2->flags() ^ (Qt::ItemIsDropEnabled));
item3->setFlags(item3->flags() ^ (Qt::ItemIsDropEnabled));

iListView->showDropIndicator(); // For convenience..
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top