1.设置列数 列宽
ui->tableWidget->setColumnCount(4); //设置列数
ui->tableWidget->setColumnWidth(0, 90); //0 设置列宽
ui->tableWidget->setColumnWidth(1, 190); //1 设置列宽
ui->tableWidget->setColumnWidth(2, 95); //2 设置列宽
ui->tableWidget->setColumnWidth(3, 95); //3 设置列宽
2.设置每一列的表头
QStringList sListHeader;
sListHeader << "预处理类型" << "窗口大小" << "类型" << "阀值";
ui->tableWidget->setHorizontalHeaderLabels(sListHeader);
//设置列内容自适应宽度
#if defined(SS_UBUNTU) //UBUNTU
ui->twShowData->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->twShowData->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
#else //ARM
ui->twShowData->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
ui->twShowData->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
#endif
3.添加一行
int iRow = ui->tableWidget->rowCount();
ui->tableWidget->setRowCount(iRow + 1);//总行数增加1
4.设置表头字体类型
QFont font = ui->tableWidget->horizontalHeader()->font(); //先获取字体
font.setBold(true); //字体设置为粗体
ui->tableWidget->horizontalHeader()->setFont(font); //设置每一列的标题字体为粗体
5.设置行属性
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择模式,选择单行
ui->tableWidget->verticalHeader()->setDefaultSectionSize(10); //设置行高
ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);//禁止鼠标拖动缩放行高度
ui->tableWidget->setRowHeight(1, 35);//设置第一行高度
ui->tableWidget->setRowHidden(1, true);//隐藏第一行
6.设置列属性
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectColumns); //设置选择模式,选择单列
ui->tableWidget->horizontalHeader()->setDefaultSectionSize(10); //设置列宽
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);//禁止鼠标拖动缩放行列宽
ui->tableWidget->setColumnWidth(1, 35);//设置第一列宽度
ui->tableWidget->setColumnHidden(1, true);//隐藏第一行
7. 设置综合属性
ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //设置充满表宽度
ui->tableWidget->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);//设置表格内容自动适应宽度
ui->tableWidget->verticalHeader()->setDefaultSectionSize(10); //设置行高
ui->tableWidget->setFrameShape(QFrame::NoFrame); //设置无边框
ui->tableWidget->setShowGrid(false); //设置不显示格子线
ui->tableWidget->verticalHeader()->setVisible(false); //设置垂直头不可见
ui->tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); //可多选(Ctrl、Shift、 Ctrl+A都可以)
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次选择一行
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
ui->tableWidget->horizontalHeader()->resizeSection(0,150); //设置表头第一列的宽度为150
ui->tableWidget->horizontalHeader()->setFixedHeight(25); //设置表头的高度
ui->tableWidget->setStyleSheet("selection-background-color:lightblue;"); //设置选中背景色
ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}"); //设置表头背景色
8.动态添加控件
//第一列 动态添加QTableWidgetItem
int iRow = ui->tableWidget->rowCount();
QTableWidgetItem *item = new QTableWidgetItem("二值化");
ui->tableWidget->setItem(iRow, 0, item); //添加到界面
//第二列 动态添加QComboBox
QComboBox* comboBox1 = new QComboBox();
comboBox1->insertItem(0, "3x3");
comboBox1->insertItem(1, "5x5");
comboBox1->setCurrentIndex(data.iDlgSize);
comboBox1->setProperty("row", iRow);
comboBox1->setProperty("col", 1);
ui->tableWidget->setCellWidget(iRow, 1, comboBox1);
connect(comboBox1, SIGNAL(currentIndexChanged(int)), this, SLOT(onCobColumnChanged(int)));
//第三列 动态添加QComboBox
QComboBox* comboBox2 = new QComboBox();
comboBox2->insertItem(0, tr("绝对"));
comboBox2->insertItem(1, tr("相对"));
comboBox2->insertItem(2, tr("自适应"));
comboBox2->setCurrentIndex(data.iType);
comboBox2->setProperty("row", iRow);
comboBox2->setProperty("col", 2);
ui->tableWidget->setCellWidget(iRow, 2, comboBox2);
connect(comboBox2, SIGNAL(currentIndexChanged(int)), this, SLOT(onCobColumnChanged(int)));
//第四列 动态添加QSpinBox
QSpinBox* spinBox3 = new QSpinBox();
spinBox3->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
spinBox3->setMinimum(0);
spinBox3->setMaximum(100);
spinBox3->setSuffix(" %");
spinBox3->setFocusPolicy(Qt::ClickFocus);
spinBox3->setProperty("row", iRow);
spinBox3->setProperty("col", 3);
spinBox3->setValue(data.iThreshold);
ui->tableWidget->setCellWidget(iRow, 3, spinBox3);
connect(spinBox3, SIGNAL(valueChanged(int)), this, SLOT(onSpbColumnChanged(int)));
9.运行结果
————————————————
版权声明:本文为CSDN博主「mark-puls」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cwj066/java/article/details/83344705