Question

Is there a way of adding collapsible groups in QListWidget (as in windows7 explorer icon view). Or can we modify QTreeWidget to have similar behavior ?

Was it helpful?

Solution

Finally I derived a QTreeWidget and added QListWidgets as 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();
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top