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):
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();
}
int main(int argn, char** argv)
{
QApplication a(argn, argv);
QTreeWidget tree;
tree.resize(300, 500);
tree.setColumnCount(2);
tree.show();
for(unsigned int i = 0; i < 2; i++)
{
QTreeWidgetItem *root = new QTreeWidgetItem(&tree);
root->setFlags(root->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
tree.setItemWidget(root, 0, new QPushButton("Root"));
root->setText(1, "A");
QTreeWidgetItem *child = new QTreeWidgetItem();
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();
}
To copy to clipboard, switch view to plain text mode
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:
{
public:
~Button() {}
{
cout << "Button pressed" << endl;
event->ignore();
}
protected:
};
class Button : public QPushButton
{
public:
Button(const QString& text) : QPushButton(text) {}
~Button() {}
void mousePressEvent(QMouseEvent *event)
{
QPushButton::mousePressEvent(event);
cout << "Button pressed" << endl;
event->ignore();
}
protected:
};
To copy to clipboard, switch view to plain text mode
Thank you for your help!
Bookmarks