2014-02-10 117 views
2

有沒有在QListWidget中添加可摺疊組的方法(如在Windows7資源管理器圖標視圖中)。或者我們可以修改QTreeWidget以具有類似的行爲?QListWidget是否支持分組?

+2

您應該使用QTreeView/QTreeWidget。列表小部件 - 適用於objets列表,無需分組。 –

回答

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(); 
    } 
}; 
+0

類似文章[只讀,可滾動,可摺疊,圖標列表](http://stackoverflow.com/questions/3639468/what-qt-widgets-to-use-for-read-only-scrollable-collapsible-icon-名單) – rmi