26 #include <QHeaderView>
35 connect(
this, SIGNAL(itemExpanded(QTreeWidgetItem*)),
this, SLOT(updateSizeHint()));
36 connect(
this, SIGNAL(itemCollapsed(QTreeWidgetItem*)),
this, SLOT(updateSizeHint()));
39 connect(
this, &ExtendedTreeWidget::itemExpanded, [](QTreeWidgetItem* item) {
40 if (item->childCount() == 1) item->child(0)->setExpanded(
true);
44 QSize ExtendedTreeWidget::sizeHint()
const
46 QSize
size = QTreeWidget::sizeHint();
47 int height = 2 * frameWidth();
48 if (!isHeaderHidden() && header()) height += header()->sizeHint().height();
50 for (
int i = 0; i < topLevelItemCount(); ++i) height += recursiveHeightHint(topLevelItem(i));
52 size.setHeight(height);
56 int ExtendedTreeWidget::recursiveHeightHint(QTreeWidgetItem* item)
const
59 int item_widget_height = 0;
61 for (
int col_idx = 0; col_idx < item->columnCount(); ++col_idx)
67 int item_height_aux = visualItemRect(item).height();
69 item_height =
std::max(item_height, item_height_aux);
71 QWidget* item_widget = itemWidget(item, 0);
72 if (item_widget) item_widget_height =
std::max(item_widget_height, item_widget->geometry().height());
75 int height =
std::max(item_height, item_widget_height);
77 if (item->isExpanded())
79 int num_children = item->childCount();
80 for (
int child_idx = 0; child_idx < num_children; ++child_idx) height += recursiveHeightHint(item->child(child_idx));
85 void ExtendedTreeWidget::mousePressEvent(QMouseEvent* event)
90 QTreeWidget::mousePressEvent(event);