有没有办法在QListWidget中添加可折叠组(如在Windows 7资源管理器图标视图中)。或者我们可以修改QTreeWidget以具有类似的行为吗?
答案 0 :(得分:2)
最后,我派生了一个QTreeWidget并将QListWidgets添加为ItemWidgets。
class ExpandedList : public QListWidget
{
Q_OBJECT
public:
ExpandedList(QWidget* pParent) : QListWidget(pParent)
{
setViewMode(IconMode);
setResizeMode(Adjust);
setMovement(Static);
for (int i = 0; rand() % 15; ++i)
{
addItem(new QListWidgetItem(QIcon(":/Images/AlertsIcon.ico"), "Text"));
}
}
virtual QSize sizeHint() const { return QSize(30, 30); }
};
class IconTree : public QTreeWidget
{
Q_OBJECT
public:
IconTree(QWidget* pParent) : QTreeWidget(pParent)
{
for (int i = 0; i < 10; ++i)
{
QTreeWidgetItem* pGroup = new QTreeWidgetItem;
pGroup->setText(0, "Group");
addTopLevelItem(pGroup);
QTreeWidgetItem* pContent = new QTreeWidgetItem;
pGroup->addChild(pContent);
setItemWidget(pContent, 0, new ExpandedList(this));
}
}
virtual void resizeEvent(QResizeEvent * pp)
{
QTreeWidget::resizeEvent(pp);
Arrange();
}
void Arrange()
{
for (int i = 0; i < 10; ++i)
{
QTreeWidgetItem* pContent = (QTreeWidgetItem*)topLevelItem(i)->child(0);
ExpandedList* pList = (ExpandedList*)itemWidget(pContent, 0);
QRect r0 = pList->visualItemRect(pList->item(0));
QRect r1 = pList->visualItemRect(pList->item(pList->count() - 1));
pContent->setSizeHint(0, QSize(200, r1.bottom() - r0.top() + 20));
}
updateGeometries();
}
};