QListWidget, access to index and item after drag and drop event
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?
Code:
connect(ui
->lwSunday
->model
(),
SIGNAL(rowsInserted
(QModelIndex,
int,
int)),
this,
SLOT(ItemInserted
(QModelIndex,
int,
int)));
Code:
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.
Re: QListWidget, access to index and item after drag and drop event
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,
_
Re: QListWidget, access to index and item after drag and drop event
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?
Re: QListWidget, access to index and item after drag and drop event
Not item(int, int), item(int)
Check the documentation, those two have a very different semantic
Cheers,
_
Re: QListWidget, access to index and item after drag and drop event
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
Re: QListWidget, access to index and item after drag and drop event
Well, if you want to call the model's data method, you can ask if for a QModelindex for any row you want.
Cheers,
_