PDA

View Full Version : QTreeWidget itemAt



maartenS
16th September 2008, 17:22
Hi,

I'm trying to put the contents of a QTreeWidget in a QMultiMap but when I'm checking the result it seems only the first row had been inserted.
Here's my code:


QMultiMap<int, QString> mainInputWidget::returnTravelTableData()
{
QMultiMap<int, QString> rowStrings;
for (int row = 0; row < ui.MItravelTreeWidget->topLevelItemCount(); ++row)
{
for (int column = ui.MItravelTreeWidget->columnCount() - 1; column >= 0; --column)
{
qDebug() << "column" << column;
qDebug() << "row" << row;
rowStrings.insert(row, ui.MItravelTreeWidget->itemAt(row,column)->text(column));
}
}
return rowStrings;
}


The loops are working right according the qDebugged row and column values, but the problem is with
ui.MItravelTreeWidget->itemAt(row, column)
It keeps pointing to the first row.
Probably I'm not using this function correctly. What should I do different or is there another solutions that works as well?

Thanks for answering.

aamer4yu
17th September 2008, 05:44
You are right .. The problem is with

ui.MItravelTreeWidget->itemAt(row, column)

itemAt() takes coordinates as arguments , not row/column !!

second mistake is way of iterating...
remember QTreeWidget is a TREE. You get top level items from the treewidget. and u can get child items from those.
So your inner loop should be something like -


QTreeWidgetItem *item = ui.MItravelTreeWidget->topLevelItem(row);
for (int column =item->columnCount() - 1; column >= 0; --column)
{
qDebug() << "column" << column;
qDebug() << "row" << row;
rowStrings.insert(row, item->text(column));
}


Hope this helps :)

maartenS
17th September 2008, 10:23
Aamer4u thanks!

I was so focused on this itemAt() function that I completely overlooked the QTreeWidgetItem...
Thanks for helping me out!
Now I'm just curious, in what situation do you use the itemAt() function then?

aamer4yu
17th September 2008, 17:26
hmmm.... how about selection of item ??
and also if u want to manually drag drop from your tree view... itemAt() will be useful... isnt it ?

jpn
17th September 2008, 19:19
One common use case for QTreeWidget::itemAt() are item specific context menus.