2011年10月9日 星期日

QTreeWidget簡單例子

QTreeWidget是蠻符合可以讓使用客制化選擇的元件~~
裡面可以包含icon的設定,
子Item的設定
勾選的選項
當有需要的時候,Scroll會自動出現

#include <QApplication>
#include <QTreeWidget>

int
main (
int argc,
char *argv[]
)
{
QApplication app (argc, argv);

//
// Create a TreeWidget for store TreeWidgetItem
//

QTreeWidget *TreeWidget = new QTreeWidget;

//
// Set the title for QTreeWidget
//

QStringList ColumnTitle;
ColumnTitle.append ("Name");
ColumnTitle.append ("Comment");
TreeWidget->setHeaderLabels (ColumnTitle);

//
// Add the first item in the root of QTreeWidget
//

QStringList fileColumn;
fileColumn.append ("File Name");
fileColumn.append ("Comment for File Name");
QTreeWidgetItem *ParentItem = new QTreeWidgetItem (fileColumn);
ParentItem->setCheckState (0, Qt::PartiallyChecked);
TreeWidget->addTopLevelItem (ParentItem);

//
// Add the sub item of ParentItem QTreeWidget
//

QStringList ChildFileColumn;
ChildFileColumn.append ("Child Name");
ChildFileColumn.append ("Commet for Child Name");
QTreeWidgetItem *ChildItem = new QTreeWidgetItem (ChildFileColumn);
ChildItem->setCheckState (0, Qt::Checked);
ParentItem->addChild (ChildItem);

//
// Add the second sub item of ParentItem QTreeWidget
//

QStringList ChildFileColumn2;
ChildFileColumn2.append ("Child Name2");
ChildFileColumn2.append ("Comment for Child Name2");
QTreeWidgetItem *ChildItem2 = new QTreeWidgetItem (ChildFileColumn2);
ChildItem2->setCheckState (0, Qt::Unchecked);
ParentItem->addChild (ChildItem2);

//
// Set another root item into the QTreeWidget
//

QStringList AnotherfileColumn;
AnotherfileColumn.append ("Another Name");
AnotherfileColumn.append ("Comment for Another Name");
QTreeWidgetItem *AnotherItem = new QTreeWidgetItem (AnotherfileColumn);
AnotherItem->setCheckState (0, Qt::Unchecked);
TreeWidget->addTopLevelItem (AnotherItem);

TreeWidget->show ();

return app.exec ();
}


內容小於顯示區域,則不會出現ScrollBar


若內容大於要顯示的區域,則會出現ScrollBar


參考資料:
Qt4 Gossip: QTreeWidget 與 QTreeWidgetItem
QTreeWidget Class Reference
QTreeWidgetItem Class Reference

沒有留言: