QListWidget:
//利用listwidget写
QListWidgetItem *listitem = new QListWidgetItem("锄禾日当午");
//将一行诗放入listwidget控件中
ui->listWidget->addItem(listitem);
//设置listwidget
listitem->setTextAlignment(Qt::AlignHCenter);
QTreeWidget:
//添加头
ui->treeWidget->setHeaderLabels(QStringList()<<"英雄"<<"英雄介绍");
QTreeWidgetItem * liliangtreeitem = new QTreeWidgetItem(QStringList()<< "力量"); //可以匿名方式创建
QTreeWidgetItem * minjietreeitem = new QTreeWidgetItem(QStringList()<< "敏捷");
QTreeWidgetItem * zhilitreeitem = new QTreeWidgetItem(QStringList()<< "智力");
//加载顶层节点
ui->treeWidget->addTopLevelItem(liliangtreeitem);
ui->treeWidget->addTopLevelItem(minjietreeitem);
ui->treeWidget->addTopLevelItem(zhilitreeitem);
//可以追加子节点
QStringList hero1;
hero1 <<"刚背猪"<<"前排坦克";
QTreeWidgetItem * liliang_gangbeitreeitem = new QTreeWidgetItem(hero1);
liliangtreeitem->addChild(liliang_gangbeitreeitem);
QStringList shuxing;
shuxing <<"能打能抗";
QTreeWidgetItem * liliang_gangbei_shuxingtreeitem = new QTreeWidgetItem(shuxing);
liliang_gangbeitreeitem->addChild(liliang_gangbei_shuxingtreeitem);
QTableWidget:
//TabelWidget控件
//设置列数
ui->tableWidget->setColumnCount(3);
//设置水平表头
ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"姓名"<<"性别"<<"年龄");
//设置行数
ui->tableWidget->setRowCount(5);
//设置正文
// ui->tableWidget->setItem(0,0,new QTableWidgetItem("PA"));
QStringList nameList;
nameList<<"PA"<<"DG"<<"YF"<<"gl"<<"XIAOH";
QList<QString> sexList ;//QList<QString> list容器放QString类型,与QStringList一样
sexList <<"男"<<"男"<<"男"<<"男"<<"女";
for(int i = 0 ; i <5 ;i++)
{
int col = 0 ;
ui->tableWidget->setItem(i,col++, new QTableWidgetItem(nameList[i]));
ui->tableWidget->setItem(i,col++, new QTableWidgetItem(sexList.at(i))); //检查是否超范
ui->tableWidget->setItem(i,col++, new QTableWidgetItem(QString::number(i+19)));
}