Problem with custom display widget and selection in QTreeWidget
Hello,
I have a problem with Qt's QTreeWidget. Selection does not work correctly for cells containing a custom display widget. When the widget is clicked for the first time, the row is selected and then never again, the selected row is still rendered as if it was selected. When I collapse the tree's root, it works again (for a single time). The example below contains two columns, I want the selection for the first one (containing the custom widget) to work just like the second one (not having a custom widget). Do you have an idea what's causing this? My fruitless solution attempt is found below the following example (I chose a button for simplicity, but my actual widget is a different one):
Code:
int main(int argn, char** argv)
{
tree.resize(300, 500);
tree.setColumnCount(2);
tree.show();
for(unsigned int i = 0; i < 2; i++)
{
root->setFlags(root->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
root->setText(1, "A");
child->setFlags(child->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
child->setText(1, "B");
root->addChild(child);
tree.
setItemWidget(child,
0,
new QPushButton("Child"));
}
return a.exec();
}
I assumed, that mouse events are not propagated correctly. I tried to solve it with a custom widget (set uing setItemWidget), which ignores mousePressEvent. The event is then indeed passed to the tree, but no selection happens:
Code:
{
public:
~Button() {}
{
cout << "Button pressed" << endl;
event->ignore();
}
protected:
};
Thank you for your help!