Important: Please read the Qt Code of Conduct -

QListWidget elements sorting using drag&drop

  • I have QListView. I added elements in there using @new QListWidgetItem(text, qlwElements);@. Later I will use icons for elements, so I plan to use another code @QListWidgetItem *newItem = new QListWidgetItem(text, qlwElements);
    Question: how to sort elements using drug and drop functionality and after that add some more elements?
    I want to add three elements - 1, 2, 3. Then drag first one (1) to the end, so they will be 2, 3, 1. And then I want to add element 4. So after adding fourth element I want 2, 3, 1, 4.
    Now I have 1,2,3,4 after adding fourth element.
    Is there some functionality to "auto sort elements" after drop? Or where, when and in what method I can get info about where user drop element?

  • there is a sortingenabled property that you can set to true to do this automatically

  • [quote author="chetankjain" date="1297848972"]there is a sortingenabled property that you can set to true to do this automatically[/quote]

    It sorts wonderful! But I need sorting in another way.
    I need to sort my Model somehow and then View will sort elements in proper way. But how I can get know how user sort elements in View? So I can resort elements in Model and then say View resort View. And it will sort elements in that way as user set.

  • What do you mean by "I need to sort my Model somehow". The sort order is always stored and Qt takes care of that for you. When an item is added to the model, does'nt matter whether drag and drop or some other way. Sorting is always retained. Only important part is that your model must know when the data is added.

  • [quote author="Darryl DSouza" date="1297862915"]What do you mean by "I need to sort my Model somehow".[/quote]
    I have list of my own objects. When I click add button I create object, add it to list and add QListViewItem to QListView with proper text to show user that he add element to list.
    All elements have their own place in my list and in QListView. When user sort them in UI I want to know which element now on what position.
    User drag first element to the end and now it isn't on first place. Now it at the end of list. But I can see it only in UI. I don't know how to get this info in code.

  • Did you check whether you get the signal from the view void QAbstractItemView::currentChanged ( const QModelIndex & current, const QModelIndex & previous ) or did you try to check the signal void indexesMoved ( const QModelIndexList & indexes ) ?
    You will know which item is moved where?

  • I solve this issue in other way. Not really good, but acceptable.
    I change view mode from Icon to List and that is it.
    Maybe in future versions I will reimplement.
    Thanks, Darryl DSouza!

Log in to reply