QT常用控件(三)——QTreeWidget(树控件),QTableWidget控件

QTreeWidget树控件

实例:

#include "layout.h"
#pragma execution_character_set("utf-8")
Layout::Layout(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    //treewidget树的使用
    //设置水平头信息
    ui.treeWidget->setHeaderLabels(QStringList() << "英雄" << "英雄介绍");
    //设置顶层节点
    QTreeWidgetItem* zhanitem = new QTreeWidgetItem(QStringList()<<"战士");
    ui.treeWidget->addTopLevelItem(zhanitem);
    QTreeWidgetItem* sheitem = new QTreeWidgetItem(QStringList() << "射手");
    ui.treeWidget->addTopLevelItem(sheitem);
    QTreeWidgetItem* faitem = new QTreeWidgetItem(QStringList() << "法师");
    ui.treeWidget->addTopLevelItem(faitem);
    //追加子节点
    QStringList zhanlist;
    zhanlist << "亚瑟" << "前排回血的肉盾,拥有超强恢复能力";
    QTreeWidgetItem* yase = new QTreeWidgetItem(zhanlist);
    zhanitem->addChild(yase);
    QStringList sunlist;
    sunlist << "孙尚香" << "后排收割的射手,拥有超强机动性";
    QTreeWidgetItem* sunshangxiang = new QTreeWidgetItem(sunlist);
    sheitem->addChild(sunshangxiang);
    QStringList dalist;
    dalist << "妲己" << "后排爆发的法师,拥有超强的爆发伤害";
    QTreeWidgetItem* daji = new QTreeWidgetItem(dalist);
    faitem->addChild(daji);
}

 QTableWidget控件

实例:

#include "layout.h"
#pragma execution_character_set("utf-8")
Layout::Layout(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    //TableWidget控件
    //设置列数
    ui.tableWidget->setColumnCount(3);
    //设置水平表头
    ui.tableWidget->setHorizontalHeaderLabels(QStringList() << "姓名" << "性别" << "年龄");
    //设置行数
    ui.tableWidget->setRowCount(5);
    
    //设置正文(用for循环输入信息)
    QStringList namelist;
    namelist << "亚瑟" << "小乔" << "关羽" << "张飞" << "马超";
    QStringList sexlist;
    sexlist << "" << "" << "" << "" << "";
    for (int i = 0; i < 5; i++)
    {
       auto col = 0;
       ui.tableWidget->setItem(i, col++, new QTableWidgetItem(namelist[i]));
       ui.tableWidget->setItem(i, col++, new QTableWidgetItem(sexlist[i]));
       //int转qstring类型
       ui.tableWidget->setItem(i, col++, new QTableWidgetItem(QString::number(i+18 )));
    }
}

 

posted @ 2021-08-09 16:55  唯有自己强大  阅读(1382)  评论(0编辑  收藏  举报