10.Qt元素QTreeWidgetItem

TreeWidget
    设置头
    ui->treeWidget->setHeaderLabels(QStringList() << "英雄"<<"英雄介绍");
    设置具体内容
    创建顶层的item
        QTreeWidgetItem * liItem = new …
        ui->treeWidget->addTopLevelItem(liItem);
    设置子节点
        addChild()
.pro

#-------------------------------------------------
#
# Project created by QtCreator 2019-09-19T00:58:19
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = 07_treewidget
TEMPLATE = app


SOURCES += main.cpp\
        widget.cpp

HEADERS  += widget.h

FORMS    += widget.ui

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //treeWidget的使用
    ui->treeWidget->setHeaderLabels(QStringList()<< "英雄姓名"<< "英雄介绍");
    //添加项目
    QTreeWidgetItem * liItem = new QTreeWidgetItem(QStringList() <<"力量");
    QTreeWidgetItem * minItem  = new QTreeWidgetItem(QStringList() << "敏捷");
    QTreeWidgetItem * zhiItem  = new QTreeWidgetItem(QStringList() << "智力");
    //添加顶层项目
    ui->treeWidget->addTopLevelItem(liItem);
    ui->treeWidget->addTopLevelItem(minItem);
    ui->treeWidget->addTopLevelItem(zhiItem);

    //追加子项目  子项目也是QTreeWidgeTItem
    QStringList heroL1;
    QStringList heroL2;
    QStringList heroM1;
    QStringList heroM2;
    QStringList heroZ1;
    QStringList heroZ2;
    heroL1 << "刚被猪" << "前排坦克,能在吸收伤害的同时造成可观的范围输出";
    heroL2 << "船长" << "前排坦克,能肉能输出能控场的全能英雄";

    heroM1 << "月骑" << "中排物理输出,可以使用分裂利刃攻击多个目标";
    heroM2 << "小鱼人" << "前排战士,擅长偷取敌人的属性来增强自身战力";

    heroZ1 << "死灵法师" << "前排法师坦克,魔法抗性较高,拥有治疗技能";
    heroZ2 << "巫医" << "后排辅助法师,可以使用奇特的巫术诅咒敌人与治疗队友";

    //追加子项目 子项也是QTreeWidgetItem
    QTreeWidgetItem * li1 =new QTreeWidgetItem(heroL1);
    liItem->addChild(li1);
    QTreeWidgetItem * li2 =new QTreeWidgetItem(heroL2);
    liItem->addChild(li2);
    QTreeWidgetItem * Min1 =new QTreeWidgetItem(heroM1);
    minItem->addChild(Min1);
    QTreeWidgetItem * Min2 =new QTreeWidgetItem(heroM2);
    minItem->addChild(Min2);
    QTreeWidgetItem * Zhi1 =new QTreeWidgetItem(heroZ1);
    zhiItem->addChild(Zhi1);
    QTreeWidgetItem * Zhi2 =new QTreeWidgetItem(heroZ2);
    zhiItem->addChild(Zhi2);

}

Widget::~Widget()
{
    delete ui;
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

图片

10.Qt元素QTreeWidgetItem_第1张图片

你可能感兴趣的:(QT)