QListWidget reorder with Drag and Drop
-
Is there a way to allow a user to reorder QListWidgetItems in a QListWidget by dragging and dropping them, like you can do in Windows Explorer?
-
Yes, set listWidget's dragDropMode to QAbstractItemView::DragDrop and defaultDropAction to Qt::MoveAction.
-
I tried this, but when I try to drag an item, it just selects more items.
-
Set selectionMode to QAbstractItemView::SingleSelection.
-
Then, when I try to drag it, it just selects a different row.
-
This does the job:
@
setDragDropMode(QAbstractItemView::InternalMove);
@ -
Set dragEnabled to true.
-
Neither of those are working! :(
Am I the only one with this problem? -
Which platform are you working on? Is your qt compiled with QT_NO_DRAGANDDROP defined?
-
I am using Windows 7 with the Qt SDK downloaded from qt.nokia.com/downloads. I did not build it myself.
-
This works for me:
@
#include <QApplication>
#include <QListWidget>int main(int argc, char **argv)
{
QApplication a(argc, argv);QListWidget lw; for(int i = 1; i < 10; ++i) lw.addItem(new QListWidgetItem(QString("Item %1").arg(i))); lw.setDragDropMode(QAbstractItemView::InternalMove); lw.show(); a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit())); a.exec();
}
@ -
Strange. It works for me, too. I wonder why my other application doesn't then.
-
Do you use a user interface made with Qt Designer and did you change some of the drag/drop settings there?
-
Do you use QListWidget or QListWidget inherited widget? If you inherit from listWidget and use mouse or drag events and didn't pass event to QListWidget, drag and drop will not work.
-
I made it with Qt Designer and I changed the drag and drop properties a lot, but now I changed them back to just dragDropMode = internalMove and put the others back to default. The selection mode is ExtendedSelection, but I tried the default, too.
-
That should work. I tried this with designer and could move the items in the preview mode (Ctrl-R/Cmd-R). That should work in the real code too, if you do not change other drag/drop settings there too.
-
Even stranger news:
I created a new project, copied and pasted all of the code / widgets, and now its working!
Any ideas why this happened and how to prevent it in the future?
-
Sorry, I do not have any clue what's happening there. Maybe some alien code from outer space? :-)
-
Here is my scenario: You use visual studio. Your code had little error. Complier didn't compile code and ran old executable.
-
Actually, I use Qt Creator.