Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4524242
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-07-30 18:27:03

工程代码: 40.rar  


豆子:http://devbean.blog.51cto.com/448512/262296




  1. #ifndef TREEWIDGET_H
  2. #define TREEWIDGET_H

  3. #include <QtGui>

  4. class TreeWidget : public QWidget
  5. {
  6. public:
  7.     TreeWidget(); //构造函数

  8. private:
  9.     QTreeWidget *tree;
  10. };

  11. #endif // TREEWIDGET_H

  1. TreeWidget::TreeWidget()
  2. {
  3.     tree = new QTreeWidget(this);
  4.     tree->setColumnCount(2); //共几个 root根

  5.     QStringList headers;  //添加 头信息
  6.     headers<< "Name" << "Number";
  7.     tree->setHeaderLabels(headers);

  8.     QStringList rootTextList;
  9.     rootTextList << "Root" << "0";
  10.     QTreeWidgetItem *root = new QTreeWidgetItem(tree,rootTextList);

  11.     QStringList leafTextList;
  12.     leafTextList <<"Leaf 1" <<"1";
  13.     QTreeWidgetItem *leaf = new QTreeWidgetItem(root,leafTextList);
  14.     root->addChild(leaf);

  15.     QStringList leaf2TextList;
  16.     leaf2TextList << "Leaf 2" <<"2";
  17.     QTreeWidgetItem *leaf2 = new QTreeWidgetItem(root,leaf2TextList);
  18.     leaf2->setCheckState(0,Qt::Checked); //可选
  19.     root->addChild(leaf2);

  20.     QStringList rootTextListA;
  21.     rootTextListA << "RootA" << "0";
  22.     QTreeWidgetItem *rootA = new QTreeWidgetItem(tree,rootTextListA);

  23.     QStringList leafTextListA;
  24.     leafTextListA <<"LeafA 1" <<"1";
  25.     QTreeWidgetItem *leafA = new QTreeWidgetItem(rootA,leafTextListA);
  26.     root->addChild(leafA);

  27.     QStringList leaf2TextListA;
  28.     leaf2TextListA << "LeafA 2" <<"2";
  29.     QTreeWidgetItem *leaf2A = new QTreeWidgetItem(rootA,leaf2TextListA);
  30.     leaf2A->setCheckState(0,Qt::Checked); //可选
  31.     rootA->addChild(leaf2A);

  32.     QList<QTreeWidgetItem *>rootList;
  33.     rootList << root;
  34.     rootList << rootA;
  35.     tree->insertTopLevelItems(0,rootList);


  36. }









阅读(2423) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~