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:
while (*it)
{
...
++it;
}
QTreeWidgetItemIterator it(treeWidget, QTreeWidgetItemIterator::Checked);
while (*it)
{
QString col0 = (*it)->text(0);
...
++it;
}
To copy to clipboard, switch view to plain text mode
For selected items, try playing around with this:
#include <QtGui>
{
Q_OBJECT
public:
{
connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(printSelection()));
}
private slots:
void printSelection()
{
qDebug() << "selection:";
{
for (int col = 0; col < item->columnCount(); ++col)
{
row += item->text(col);
}
qDebug() << "row:" << row.join(", ");
}
}
};
int main(int argc, char *argv[])
{
TreeWidget treeWidget;
treeWidget.setColumnCount(2);
for (int i = 0; i < 10; ++i)
treeWidget.show();
return a.exec();
}
#include "main.moc"
#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::ExtendedSelection);
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"
To copy to clipboard, switch view to plain text mode
Bookmarks