PDA

View Full Version : QTreeWidget column size problem



Cruz
9th November 2012, 00:31
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.

8401

Here is how I create it:



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);



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 (http://doc.qt.digia.com/qt/itemviews-simpletreemodel.html). How can I get that?

norobro
9th November 2012, 01:04
Set the checkState and the text on the same column:
QTreeWidgetItem* ti2 = new QTreeWidgetItem();
ti2->setCheckState(0, Qt::Unchecked);
ti2->setText(0, QString("item: %1").arg(j));

The plus button is a function of the style. QCleanlooksStyle has a plus button.

Cruz
9th November 2012, 14:36
Thanks mate! Both suggestions work perfectly.

I'm going to need a second widget just like this, but instead of the checkboxes I will have to use sliders. Before I get lost in the depths of the model view framework, can anyone please point out the clear path to enlightment?