效果图
设置列数
void QTableWidget::setColumnCount(int columns)
ui->tableWidget->setCoumnCount(3);
设置水平表头
void QTableWidget::setVerticalHeaderItems(int row, QTableWidgetItem *item)
ui->tableWidget->setHorizontalHearderLabels(QStringList() << "姓名" << "。。")
设置行数
void QTableWidget::setRowCount(int rows)
ui.tableWidget->setRowCount(5)
设置正文
void QTableWidget::setItem(int row, int column, QTableWidgetItem *item)
ui->tableWidget->setItem(0, 0, new QTableWidgetItem("佛说"));
代码
#include "table_widget.h"
#include "ui_table_widget.h"
Table_Widget::Table_Widget(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Table_Widget)
{
ui->setupUi(this);
//设置列数
ui->tableWidget->setColumnCount(3);
//设置水平表头
ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "姓名" << "性别" << "年龄" );
//设置行数
ui->tableWidget->setRowCount(5);
//设置正文
//ui->tableWidget->setItem(0, 0, new QTableWidgetItem("佛说"));
QStringList nameList;
nameList << "亚瑟" << "猴子" << "露娜" <<"张飞" << "花木兰";
QList<QString> sexList;
sexList << "男" << "男" << "女" << "男" << "女" ;
for(int i = 0; i < 5; i++){
int count = 0;
ui->tableWidget->setItem(i, count++, new QTableWidgetItem(nameList[i]));
ui->tableWidget->setItem(i, count++, new QTableWidgetItem(sexList.at(i)));
ui->tableWidget->setItem(i, count++, new QTableWidgetItem(QString::number(i+18)));
}
}
Table_Widget::~Table_Widget()
{
delete ui;
}