PDA

View Full Version : Adding strings in QTreeWidget ?



npc
24th January 2007, 09:25
Hi All,

I am using QTreeWidget class to display the datas in tree.

Having list of qstrings which are stored in qstringlist, how can i move these strings to first column of qtreewidget ? and also i want to display those items with selection boxes which provide multiple selection.

thanks in advance,
:)

jpn
24th January 2007, 09:38
What have you tried so far? Did you notice the example in the QTreeWidget docs?


Use QTreeWidget::setColumnCount() to set sufficient amount of columns
iterate the list of strings, create a QTreeWidgetItem per each
use QTreeWidgetItem::setFlags() to set the item checkable
use QTreeWidgetItem::setCheckState() to initialize the check state

aamer4yu
24th January 2007, 09:48
and also QAbstractItemView::SelectionMode :)

npc
24th January 2007, 10:48
Thanks JPN and aamer4yu,
Now I'm able to get the texts in tree widget.

actually each item should preceded by check box, for the user selection.
tried ItemIsUserCheckable and other flags in the setflag option, not working.

how to display each item with check box ?

jpn
24th January 2007, 10:57
Just follow the steps above.. ;) Did you initialize the check state?


item->setFlags(item->flags() | Qt::ItemIsUserCheckable); // default flags + checkable
item->setCheckState(0, Qt::Unchecked); // you must also initialize the state

npc
29th January 2007, 07:29
Could you tell how to get selected items into a list, from a treewidget ?.

jpn
29th January 2007, 07:41
Could you tell how to get selected items into a list, from a treewidget ?.
Come on! :) QTreeWidget::selectedItems()

npc
29th January 2007, 10:37
:( sorry, I forgot to tell that, I used that function... it always retuns the count of 1.
the list is in a multiple selection mode.



QList<QTreeWidgetItem *> selectedGrItems = ui.GroupsTreeWidget->selectedItems();
int gcount = selectedGroupItems.count ();


and I used the following code to get the item text.



QTreeWidgetItem it(selectedGrItems.at(0));
QString text = it.text(1);


but it is not getting the text

plz tell me how to get those items in a qstringlist ?

jpn
29th January 2007, 12:05
QTreeWidgetItem* it = selectedGrItems.at(0);
QString text = it->text(1);

npc
30th January 2007, 06:39
Hi,

When I tried to get all selected item in selectedGrItems, its always returns only single item, i.e; the gcount is 1 always.



QList<QTreeWidgetItem *> selectedGrItems = ui.GroupsTreeWidget->selectedItems();
int gcount = selectedGroupItems.count ();


How to get a whole list of selected items ?

jpn
30th January 2007, 07:09
Hi,

When I tried to get all selected item in selectedGrItems, its always returns only single item, i.e; the gcount is 1 always.



QList<QTreeWidgetItem *> selectedGrItems = ui.GroupsTreeWidget->selectedItems();
int gcount = selectedGroupItems.count ();


How to get a whole list of selected items ?
Hi

A QTreeWidgetItem represents the whole row. Do you have several rows selected? Perhaps you want the text from different columns?



QString col1 = item->text(0);
QString col2 = item->text(1);
...

npc
30th January 2007, 07:53
Hi

A QTreeWidgetItem represents the whole row. Do you have several rows selected? Perhaps you want the text from different columns?


Yes, I have several rows selected. In my code the QTreeWidgetItem has the item which is selected last.

actually i used the following code to add the items into the the QTreeWidget



QList<QTreeWidgetItem *> groupItems;

for (int i = 0; i < gName.count(); ++i)
{
QTreeWidgetItem* n;
n = new QTreeWidgetItem((QTreeWidget*)0, QStringList(gName.at(i)));
n->setFlags( n->flags() | Qt::ItemIsUserCheckable );
n->setCheckState(0,Qt::Unchecked);
items.append(n);
}

int count = groupItems.count ();

ui.GroupsTreeWidget->insertTopLevelItems(0, groupItems);


so i want to get the selected rows in a list

help me in this regard.

jpn
30th January 2007, 08:04
I see, the items are set as checkable. So just to make sure, are we talking about "checked" or "selected" items?

For checked items you can use QTreeWidgetItemIterator:


QTreeWidgetItemIterator it(treeWidget, QTreeWidgetItemIterator::Checked);
while (*it)
{
QString col0 = (*it)->text(0);
...

++it;
}

For selected items, try playing around with this:


#include <QtGui>

class TreeWidget : public QTreeWidget
{
Q_OBJECT

public:
TreeWidget(QWidget* parent = 0) : QTreeWidget(parent)
{
connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(printSelection()));
}

private slots:
void printSelection()
{
qDebug() << "selection:";
foreach (QTreeWidgetItem* item, selectedItems())
{
QStringList row;
for (int col = 0; col < item->columnCount(); ++col)
{
row += item->text(col);
}
qDebug() << "row:" << row.join(", ");
}
}
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TreeWidget treeWidget;
treeWidget.setColumnCount(2);
treeWidget.setSelectionMode(QAbstractItemView::Ext endedSelection);
for (int i = 0; i < 10; ++i)
new QTreeWidgetItem(&treeWidget, QStringList() << QString("item %1 col 0").arg(i) << QString("item %1 col 1").arg(i));
treeWidget.show();
return a.exec();
}

#include "main.moc"

npc
30th January 2007, 09:24
oops I confused with checked items and selected items, got it right now.

I am doing with checked items.