#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);//TableWidget控件//设置列数ui->tableWidget->setColumnCount(3);//设置水平表头ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"名字"<<"性别"<<"年龄");//设置行数ui->tableWidget->setRowCount(5);// //设置正文
// ui->tableWidget->setItem(0,0,new QTableWidgetItem("亚索"));QQStringList nameList;nameList<<"亚索"<<"张飞"<<"刘备"<<"赵云"<<"关羽";QList<QString>sexList;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)));//int 转 QStringui->tableWidget->setItem(i,col++,new QTableWidgetItem(QString::number(i+18)));}
}Widget::~Widget()
{delete ui;
}
测试结果: