QT QDockWidget锚接部件 和 QTreeWidget 树形部件 构成树形选择项

1.  如图,在mainwindow中 添加DockWidget到右侧,里面镶嵌TreeWidget.

 

2.

    QTreeWidget *treeWidget = new QTreeWidget;  //创建树形控件

    QStringList headers;		//树头
    headers << "Name" << "Number";
    treeWidget->setHeaderLabels(headers);

    QStringList rootTextList;	//树的根节点
    rootTextList << "Root" << "0";    //添加选项 Root --- 0
    QTreeWidgetItem *root = new QTreeWidgetItem(treeWidget, rootTextList);
    new QTreeWidgetItem(root, QStringList() << QString("Leaf 1") << "1");   //以根节点为父控件,添加选项 Leaf1 ---- 1
    QTreeWidgetItem *leaf2 = new QTreeWidgetItem(root,
                    QStringList() << QString("Leaf 2") << "2"); //以根节点为父控件,添加选项 Leaf2 ---- 2
    leaf2->setCheckState(0, Qt::Checked);  //为Leaf2 设置勾选窗

    QList rootList;
    rootList << root;
    treeWidget->insertTopLevelItems(0, rootList);  //将树形选项 添加入Tree控件

    QDockWidget *dockWidget = new QDockWidget(tr("dockWidget"));   //创建锚接部件
    dockWidget->setObjectName("dockWidget test");  
    dockWidget->setAllowedAreas(Qt::RightDockWidgetArea | Qt::LeftDockWidgetArea);  //锚接部件 允许停靠的区域,左右
    dockWidget->setWidget(treeWidget);  //设置锚接部件的内容 TreeWidget
    addDockWidget(Qt::RightDockWidgetArea, dockWidget);


 

 

你可能感兴趣的:(QT)