I'd claim this does not FIX the problem, this method only covers it.
It really is interesting why does the QTreeView draw the contents of the label.
The rejecting reason says "you will see both the item and the label contents"..
But the items in the second column should be empty. There has been set text only
for the items in the first column, and a label widget for the items in the second column!
Besides, forcing the QLabel to fill it's background also naturally overrides the selection drawing.
I managed to find something very interesting by accident:
#include <QtGui>
int main( int argc, char **argv )
{
tree->setColumnCount(1);
for (int i = 0; i < 10; ++i)
{
tree->setItemWidget(item, 0, widget);
}
tree->show();
return app.exec();
}
#include <QtGui>
int main( int argc, char **argv )
{
QApplication app(argc,argv);
QTreeWidget* tree = new QTreeWidget;
tree->setColumnCount(1);
for (int i = 0; i < 10; ++i)
{
QTreeWidgetItem* item = new QTreeWidgetItem(tree);
QWidget* widget = new QWidget;
tree->setItemWidget(item, 0, widget);
}
tree->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
The above code is a dummy example showing an "empty" tree with widgets.
The funny thing is, when you click items, there is an output like this:
QWidget:
roperty("text") failed: property invalid or does not exist
QWidget::setProperty("text", value) failed: property invalid, read-only or does not exist
So clearly the text-property of a widget is requested.
I took a look at the Qt sources, and I found that it's QItemDelegate::setModelData() which requests this property.
As for a test, I subclassed QItemDelegate and overrode setData() by an empty mplementation body.
Voila! The QLabel gets drawn correctly in a QTreeWidget. Or more correctly the item does not draw the label contents. Quite an ugly workaround, that is..
I guess I'll have to bug the Trolls, maybe there is a solution out there..
Bookmarks