// 方法1:
QTableWidget *tableWidget = new QTableWidget(10,5,ui->centralWidget);
// 使表格平铺在界面
tableWidget-> horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
tableWidget->show();
// 方法2
QTableWidget *table2 = new QTableWidget;
table2->setRowCount(10);
table2->setColumnCount(10);
table2->setParent(ui->centralWidget);
table2->show();
// 使表格平铺在界面
tableWidget-> horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// 设置控制大小
tableWidget->resize(500,500);
// 删除列
tableWidget->removeColumn(0);
// 删除行
tableWidget->removeRow(0);
QStringList header;
header<<"姓名"<<"年龄";
header.push_back(QString("备注"));
tableWidget->setHorizontalHeaderLabels(header);
// 隐藏表头
tableWidget->verticalHeader()->setVisible(false);
// 最后一列自动宽度
tableWidget->horizontalHeader()->setStretchLastSection(true);
tableWidget->setItem(0,0,new QTableWidgetItem("张三"));
tableWidget->setItem(0,1,new QTableWidgetItem("33"));
// 内容居中显示
tableWidget->item(0,0)->setTextAlignment(Qt::AlignCenter);
// 文本颜色
tableWidget->item(0,0)->setTextColor(QColor(200,100,100));
// 字体
tableWidget->item(0,0)->setFont(QFont("Microsoft Yahei"));
// 背景颜色
tableWidget->item(0,0)->setBackgroundColor(QColor(100,100,100));
// 单元格里放一个控件
QComboBox *comBox = new QComboBox();
comBox->addItem("Male");
comBox->addItem("Female");
tableWidget->setCellWidget(1,1,comBox);
QTableWidget *tableWidget = new QTableWidget(10,5,ui->centralWidget);
tableWidget->show();
QStringList header;
header<<"姓名"<<"年龄";
tableWidget->setHorizontalHeaderLabels(header);
tableWidget->setItem(0,0,new QTableWidgetItem(QIcon("D:/1.png"),"张三"));
tableWidget->setItem(0,1,new QTableWidgetItem("33"));
QString string = tableWidget->item(0,0)->text();
qDebug() << string << endl;
// 清除所有内容含表头
tableWidget->clear();
// 清除内容不含表头
// 表格只读
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
原型:tableWidget->setEditTriggers(QAbstractItemView::EditTrigger);
QAbstractItemView.EditTrigger
枚举:
参数 | 值 | 说明 |
---|---|---|
QAbstractItemView.NoEditTriggers | 0 | 不能对表格内容进行修改 |
QAbstractItemView.CurrentChanged | 1 | 任何时候都能对单元格修改 |
QAbstractItemView.DoubleClicked | 2 | 双击单元格 |
QAbstractItemView.SelectedClicked | 4 | 单击已选中的内容 |
QAbstractItemView.CurrentChanged | 8 | 任何时候都能对单元格修改 |
QAbstractItemView.AnyKeyPressed | 16 | 双击单元格 |
QAbstractItemView.AllEditTriggers | 31 | 双击单元格 |
# 设置行高和列宽
tableWidget->setColumnWidth(3,300);
tableWidget->setRowHeight(3,30);
// 选中整行
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
// 选中单元格
tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);
// 选中一列
tableWidget->setSelectionBehavior(QAbstractItemView::SelectColumns);
// 只允许选中一行
tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
// 允许选中多行(默认情况)
tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
QTableWidget *tableWidget = new QTableWidget(10,5,ui->centralWidget);
tableWidget->show();
QStringList header;
header<<"姓名"<<"年龄";
tableWidget->setHorizontalHeaderLabels(header);
tableWidget->setItem(0,0,new QTableWidgetItem("张三"));
tableWidget->setItem(0,1,new QTableWidgetItem("33"));
tableWidget->setSpan(1,0,1,2);
public slots:
void on_itemChanged(QTableWidgetItem *item);
QTableWidget *tableWidget = new QTableWidget(10,5,ui->centralWidget);
tableWidget->show();
QStringList header;
header<<"姓名"<<"年龄";
tableWidget->setHorizontalHeaderLabels(header);
connect(tableWidget, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(on_itemChanged(QTableWidgetItem *)));