admin 管理员组

文章数量: 887021

【QT

1、在**.ui文件中拖入tableWidget控件

 2、示例代码

表格样式:

//Table Widget控件
//设置列数
ui->tableWidget->setColumnCount(5);//表头高度
ui->tableWidget->horizontalHeader()->setMinimumHeight(40);QString qssTV = "QTableWidget::item:hover{background-color:rgb(92,188,227,200)}""QTableWidget::item:selected{background-color:#1B89A1}""QHeaderView::section,QTableCornerButton:section{ \padding:3px; margin:0px; color:#DCDCDC;  border:1px solid #242424; \
border-left-width:0px; border-right-width:1px; border-top-width:0px; border-bottom-width:1px; \
background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #646464,stop:1 #525252); }"
"QTableWidget{background-color:rgb(211, 211, 211);border:2px;}";//ui->tableWidget->setShowGrid(true); //设置显示格子线
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //禁止编辑
//ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //行头自适应表格
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//表头等宽
ui->tableWidget->horizontalHeader()->setFont(QFont("song", 12));//点击表时不对表头行光亮(获取焦点)
ui->tableWidget->horizontalHeader()->setHighlightSections(false);//设置表头字体加粗
QFont font = ui->tableWidget->horizontalHeader()->font();
font.setBold(true);
ui->tableWidget->horizontalHeader()->setFont(font);ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:lightblue;}"); //skyblue设置表头背景色
ui->tableWidget->setStyleSheet("selection-background-color:lightblue;"); //设置选中背景色ui->tableWidget->setStyleSheet(qssTV);ui->tableWidget->horizontalHeader()->setHighlightSections(false);         //点击表头时不对表头光亮
ui->tableWidget->setSelectionMode(QAbstractItemView::ContiguousSelection);//选中模式为多行选中
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//所有单元格的字体  设置成一样
ui->tableWidget->setFont(QFont("black", 12));//隔行变色
ui->tableWidget->setAlternatingRowColors(true);//表头
QTableWidgetItem *item0 = new QTableWidgetItem;
QTableWidgetItem *item1 = new QTableWidgetItem;
QTableWidgetItem *item2 = new QTableWidgetItem;
QTableWidgetItem *item3 = new QTableWidgetItem;
QTableWidgetItem *item4 = new QTableWidgetItem;//设置显示内容
item0->setText("Length");
item1->setText("Width");
item2->setText("Height");
item3->setText("ExternalVolume");
item4->setText("IntegralVolume");//设置水平表头
ui->tableWidget->setHorizontalHeaderItem(0, item0);
ui->tableWidget->setHorizontalHeaderItem(1, item1);
ui->tableWidget->setHorizontalHeaderItem(2, item2);
ui->tableWidget->setHorizontalHeaderItem(3, item3);
ui->tableWidget->setHorizontalHeaderItem(4, item4);//ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "id" << "Length" << "Width" << "Height" << "ExternalVolume" << "IntegralVolume");
//设置行数
//ui->tableWidget->setRowCount(5);

表格插入数据:

QString l = ui->lineEdit_ObjectLength->text().toUtf8();
QString w = ui->lineEdit_ObjectWidth->text().toUtf8();
QString h = ui->lineEdit_ObjectHeight->text().toUtf8();
QString ev = ui->lineEdit_ObjectExternalVolume->text().toUtf8();
QString iv = ui->lineEdit_ObjectIntegralVolume->text().toUtf8();//在末尾插入数据
int curRow = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(curRow);QTableWidgetItem* Length = new QTableWidgetItem;
QTableWidgetItem* Width = new QTableWidgetItem;
QTableWidgetItem* Height = new QTableWidgetItem;
QTableWidgetItem* ExternalVolume = new QTableWidgetItem;
QTableWidgetItem* IntegralVolume = new QTableWidgetItem;Length->setText(l);
Width->setText(w);
Height->setText(h);
ExternalVolume->setText(ev);
IntegralVolume->setText(iv);ui->tableWidget->setItem(curRow, 0, Length);
ui->tableWidget->setItem(curRow, 1, Width);
ui->tableWidget->setItem(curRow, 2, Height);
ui->tableWidget->setItem(curRow, 3, ExternalVolume);
ui->tableWidget->setItem(curRow, 4, IntegralVolume);

单元格内容居中显示:

int rowCount = ui->tableWidget->rowCount();
int columCount = ui->tableWidget->columnCount();
for (int row = 0; row < rowCount; row++)
{for (int colum = 0; colum < columCount; colum++){QTableWidgetItem* item = ui->tableWidget->item(row, colum);if (item != nullptr){item->setTextAlignment(Qt::AlignCenter);}}
}

3、运行效果

 参考链接 QTableWidget表头、内容字体大小、颜色、背景颜色等设置_youandme520的博客-CSDN博客_qtablewidget设置表头

QT之QTableWidget控件_大草原的小灰灰的博客-CSDN博客_qt qtablewidget

QT tableWidget的简单使用 - 缘随风烬 - 博客园 

本文标签: QT