Hello!
This is my first time using a tree widget. I managed to put it together using QTreeWidget and QTreeWidgetItem. In the first column of every item I have a checkbox and in the second colum there is a QString. I want the string to be flushed left to the checkbox on every level, but it doesn't work. If I open the tree deep enough, upper levels resize and create a gap between the checkbox and the text. The attached picture illustrates the problem.
treeview.png
Here is how I create it:
treeWidget->setColumnCount(2);
//treeWidget->header()->hide();
treeWidget
->header
()->setResizeMode
(0,
QHeaderView::ResizeToContents);
treeWidget->setAnimated(true);
treeWidget
->setSelectionMode
(QTreeWidget::NoSelection);
treeWidget->setFocusPolicy(Qt::NoFocus);
QList<QTreeWidgetItem*> items;
for (int i = 0; i < 3; ++i)
{
ti
->setText
(0,
QString("item: %1").
arg(i
));
for (int j = 0; j < 3; ++j)
{
ti2->setCheckState(0, Qt::Unchecked);
ti2
->setText
(1,
QString("item: %1").
arg(j
));
for (int k = 0; k < 3; ++k)
{
ti3->setCheckState(0, Qt::Unchecked);
ti3
->setText
(1,
QString("item: %1").
arg(k
));
ti2->addChild(ti3);
}
ti->addChild(ti2);
}
items.append(ti);
}
treeWidget->insertTopLevelItems(0, items);
QTreeWidget *treeWidget = new QTreeWidget(this);
treeWidget->setColumnCount(2);
//treeWidget->header()->hide();
treeWidget->header()->setResizeMode(0, QHeaderView::ResizeToContents);
treeWidget->setAnimated(true);
treeWidget->setSelectionMode(QTreeWidget::NoSelection);
treeWidget->setFocusPolicy(Qt::NoFocus);
QList<QTreeWidgetItem*> items;
for (int i = 0; i < 3; ++i)
{
QTreeWidgetItem* ti = new QTreeWidgetItem();
ti->setText(0, QString("item: %1").arg(i));
for (int j = 0; j < 3; ++j)
{
QTreeWidgetItem* ti2 = new QTreeWidgetItem();
ti2->setCheckState(0, Qt::Unchecked);
ti2->setText(1, QString("item: %1").arg(j));
for (int k = 0; k < 3; ++k)
{
QTreeWidgetItem* ti3 = new QTreeWidgetItem();
ti3->setCheckState(0, Qt::Unchecked);
ti3->setText(1, QString("item: %1").arg(k));
ti2->addChild(ti3);
}
ti->addChild(ti2);
}
items.append(ti);
}
treeWidget->insertTopLevelItems(0, items);
To copy to clipboard, switch view to plain text mode
How can I fix this?
And while we are at it, I would prefer to have this Windows Explorer style with the plus button, as can be seen in the simple tree model example. How can I get that?
Bookmarks