PDA

View Full Version : QListWidget, access to index and item after drag and drop event



FarAway
7th July 2015, 11:40
Hi,

I have a problem. I don't know how to get the access to item and it index after drag and drop event. I created a connection between signal and slot but item data are invalid. To be specific in this function: rowsInserted(QModelIndex,int,int) first variable is invalid. How to get access to valid data?


connect(ui->lwSunday->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(ItemInserted(QModelIndex,int,int)));




void MainWindow::ItemInserted(const QModelIndex &parent, int first, int /*last*/)
{
QString text = parent->data(Qt::DisplayRole).toString(); // text is "" why?
}


Can you help me?

P.S. Sorry for my poor language skills.

anda_skoa
7th July 2015, 12:26
As the arguments name suggests, the model index in this context is the parent of the inserted rows.

Since you are working with a list view, there is no such thing as a parent, all rows are top level rows.

The only arguments of interest to you are the row indexes describing the range of new rows.
See QListWidget::itemAt(int)

Cheers,
_

FarAway
7th July 2015, 13:09
Thanks for your answer. But still I don't know how to get data from inserted item. Function QListWidget::itemAt(int, int) can only take data from items which was inserted before. Is there any valid solution?

anda_skoa
7th July 2015, 13:55
Not item(int, int), item(int)

Check the documentation, those two have a very different semantic

Cheers,
_

FarAway
7th July 2015, 14:11
Function QListWidget::item(int) won't work too because inserted item is filled with data after sinal rowsInserted. When I take data when signal is emitted inserted item is empty(yes no data, just empty item).
I solved this problem but in some awful way. I created additional variables and when items are dragged and dropped first is emitted signal rowsInserted(I store a index as source of data), then item is added to list, next signal emitted id rowsRemoved(I add data now and remove unnecessary item). Awful isn't it? But it works!

UPDATE:
Well there is one drawback. When user hold CTRL when dragging(copy action) there is no rowsRemoved signal emitted :(
Well i will fix this problem someday :P

anda_skoa
7th July 2015, 18:38
Well, if you want to call the model's data method, you can ask if for a QModelindex for any row you want.

Cheers,
_