February 16, 2011

Chiz_ Chiz_
Lab Rat
16 posts

QListWidget elements sorting using drag&drop

 

I have QListView. I added elements in there using

  1. new QListWidgetItem(text, qlwElements);@. Later I will use icons for elements, so I plan to use another code @QListWidgetItem *newItem = new QListWidgetItem(text, qlwElements);
  2. newItem->setIcon(iconPath);

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?

6 replies

February 16, 2011

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

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

February 16, 2011

Chiz_ Chiz_
Lab Rat
16 posts
chetankjain wrote:
there is a sortingenabled property that you can set to true to do this automatically

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.

February 16, 2011

Darryl DSouza Darryl DSouza
Lab Rat
35 posts

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.

 Signature 

Darryl D’Souza

February 16, 2011

Chiz_ Chiz_
Lab Rat
16 posts

Darryl DSouza wrote:
What do you mean by “I need to sort my Model somehow”.

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.

February 16, 2011

Darryl DSouza Darryl DSouza
Lab Rat
35 posts

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?

 Signature 

Darryl D’Souza

February 17, 2011

Chiz_ Chiz_
Lab Rat
16 posts

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!

 
  ‹‹ Runtime Error! Remove dependency on MinGW      QFileDialog and concurrent access to QDir::currentDir() ››

You must log in to post a reply. Not a member yet? Register here!