PDA

View Full Version : List Widget drag drop items



alexoparin
10th January 2014, 16:49
Hi everyone!

I'm trying to implement the List Widget's drag and drop feature. It almost works, but every time I drop an item on a new position it moves but the list scrolls to the top and the item I move doesn't get selected.

What I did is just added a List Widget as a centralWidget using the designer. It's name is as simple as "listWidget", and I set it's properties as follows:

DragDropMode - internalMove;
selectionMode - extendedSelection;
movement - snap;

Then i just populate it with some items in my mainwindow.cpp as follows:

for(int i = 0; i < 51; i++)
{
new QListWidgetItem("Item " + QString::number(i), ui->listWidget);
}
That's it. So why the whole list scrolls to the top and the moved item doesn't stay selected?

Timely thanks for your help guys!

alexoparin
10th January 2014, 16:58
Hi everyone!

I'm trying to implement the List Widget's drag and drop feature. It almost works, but every time I drop an item on a new position it moves but the list scrolls to the top and the item I move doesn't get selected.

What I did is just added a List Widget as a centralWidget using the designer. It's name is as simple as "listWidget", and I set it's properties as follows:

DragDropMode - internalMove;
selectionMode - extendedSelection;
movement - snap;

Then i just populate it with some items in my mainwindow.cpp as follows:

for(int i = 0; i < 51; i++)
{
new QListWidgetItem("Item " + QString::number(i), ui->listWidget);
}

The same happens with List View event if I reimplement and give it my own model with such reimplemented methods as:

Qt::DropActions supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}

and


Qt::ItemFlags flags(const QModelIndex &index) const
{
if (index.isValid())
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
}
else
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}
}


That's it. So why the whole list scrolls to the top and the moved item doesn't stay selected?

Timely thanks for your help guys!