QT实战 之TableWidget控件
- 指定列数setColumnCount(3)
- 指定表头setHorizontalHeaderLabels(QStringList)
- 指定行数setRowCount(5)
- 设置每个单元格的内容setItem(int行数,int列数,*QTableWidgetItem内容)
#include "tablewidget.h" #include "ui_tablewidget.h"
TableWidget::TableWidget(QWidget *parent) : QWidget(parent), ui(new Ui::TableWidget) { ui->setupUi(this); ui->tableWidget->setColumnCount(3); ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"姓名"<<"性别"<<"年龄"); ui->tableWidget->setRowCount(5); QStringList nameList; nameList<<"亚瑟"<<"孙悟空"<<"刘总"<<"赵本山"<<"芙蓉姐姐"; QList<QString>sexList; sexList<<"男"<<"男"<<"男"<<"男"<<"女"; QList<int> ageList; ageList<<30<<55<<20<<66<<44; 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[i])); ui->tableWidget->setItem(i,col++,new QTableWidgetItem(QString::number(ageList[i]))); } }
TableWidget::~TableWidget() { delete ui; }
|