1 设置某列不可编辑
QTableWidgetItem *item = new QTableWidgetItem("test"); item->setFlags(item->flags() & (~Qt::ItemIsEditable));
2 设置表格禁止编辑
ui.tablewidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
3 设置单击选择某一行,不是选择某一项
//设置单击选择一行 ui.tablewidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置只能选择一行,不能多行选中 ui.tablewidget->setSelectionMode(QAbstractItemView::SingleSelection);
4 设置表格列宽,设置表格最后一列自适应拉伸
ui.tablewidget->setColumnWidth(0, 220); ui.tablewidget->setColumnWidth(1, 65); ui.tablewidget->horizontalHeader()->setStretchLastSection(true);
5 表格插入数据
int row_count = ui.tablewidget->rowCount(); ui.tablewidget->insertRow(row_count); QTableWidgetItem *item_time = new QTableWidgetItem(current_date); QTableWidgetItem *item_level = new QTableWidgetItem(QString::fromStdString(level)); QTableWidgetItem *item_data = new QTableWidgetItem(QString::fromStdString(data)); ui.tablewidget->setItem(row_count, 0, item_time); ui.tablewidget->setItem(row_count, 1, item_level); ui.tablewidget->setItem(row_count, 2, item_data);
6 删除表格的所有行
ui.table_widget_->clearContents(); for (int i = ui.log_widget_->rowCount(); i > 0; i--) { ui.log_widget_->removeRow(0); }
7 设置表格自适应拉伸(均分表格),表格可以随着窗口的变大而变大
ui.tableWidget->horizontalHeader()->setSectionResizeMode( QHeaderView::Stretch); ui.tableWidget->verticalHeader()->setSectionResizeMode( QHeaderView::Stretch);
8 设置表格的字体颜色和背景颜色
newItem = QTableWidgetItem("测试") newItem.setBackgroundColor(QColor(0,60,10)) newItem.setTextColor(QColor(200,111,100)) self.MyTable.setItem(0, 0, newItem)