ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//选中一行
ui->tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);//可多选
ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}"); //设置表头背景色
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置只读
ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);//鼠标右键发出customContextMenuRequested信号
ui->tableWidget->verticalHeader()->setVisible(false); //隐藏行表头
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//占满
ui->tableWidget->insertRow(ui->tableWidget->rowCount());//这是在最后面增加一行,在当前增加一行是使用currentRow
ui->tablelWidget->insertColumn(ui->tableWidget->columnCount());//在最后面增加一列,在当前增加一列是使用currentColumn
int rowCount = ui.tableWidget->rowCount() - 1;//获取总的行号
ui.tableWidget->setItem(rowCount, 0, new QTableWidgetItem(QString(内容)));//设置新增行的0列内容
ui.tableWidget->setItem(rowCount, 1, new QTableWidgetItem(QString(内容)));//设置新增行的1列内容
ui->tableWidget->setItem(行号,列号,new QTableWidgetItem(QString(内容))); //设置内容
ui->tableWidget->removeRow( int 行号);
ui->tableWidget->removeColumn(int 列号);
ui->tableWidget->resizeRowsToContents();//界面适应数据
ui->tableWidget->resizeColumnsToContents();
ui->tableWidget->scrollToBottom(); //显示最后一行
if(NULL==ui->tableWidget->item(i,j)) //判断tablewidget格子是否为空
//删除所有行
ui.tableWidget->clearContents();
ui.tableWidget->setRowCount(0);
ui->tableWidget->item(i, j)->setTextAlignment(Qt::AlignCenter); //设置文字居中
ui->tableWidget->horizontalHeader()->setSectionResizeMode(true); //设置均匀拉直表头
ui->tableWidget->horizontalHeader()->setStretchLastSection(true) ;//设置最后一列填充满表格
//表格中插入一个Check
QTableWidgetItem *check = new QTableWidgetItem;
check->setCheckState(Qt::Unchecked);
ui.tableWidget->setItem(0, 0, check);
if (ui.tableWidget->item(0, 0)->checkState() == Qt::Checked)
{
}
//设置表格相邻行颜色交替
ui->tableWidget->setAlternatingRowColors(true);
ui->tableWidget->setStyleSheet("border 0px;color:#6b6d7b;alternate-background:white;");
//设置选中没有虚线
QTableWidget{
outline:0px;
}
//设置表格文本居中
void mainwindow::setTableAligCenter(QTableWidget *tw)
{
int rowCount = tw->rowCount();
int columCount = tw->columnCount();
for(int row=0;row<rowCount;row++)
{
for(int colum=0;colum<columCount;colum++)
{
QTableWidgetItem* item = tw->item(row, colum);
if(item != nullptr)
{
item->setTextAlignment(Qt::AlignCenter);//感觉效率不高
}
}
}
}
//设置某些列指定宽度,同时某些列占满
ui->tableWidget->setColumnWidth(0, 200);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch);
ui->tableWidget->setColumnWidth(4, 200);
ui->tableWidget->resizeColumnsToContents();
ui->tableWidget->resizeRowsToContents();
ui->tableWidget->setWordWrap(true);
//设置某一个单元格不可编辑
QTableWidgetItem* pItemNum = pTableWidget->item(row, VALUE_1);
pItemNum->setFlags(pItemNum->flags() & 33);
//设置格式通用
void Mystack::setCommonTable(QTableWidget *tw)
{
tw->setSelectionBehavior(QAbstractItemView::SelectRows);
tw->setEditTriggers(QAbstractItemView::NoEditTriggers);
tw->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
tw->verticalHeader()->setVisible(false);
}
//右键槽函数
void Mystack::tableWidget_customContextMenuRequested(const QPoint &pos)
{
tableWidget->exec(QCursor::pos());
}