目录
一、前言
二、函数API说明
三、代码区域
四、槽函数连接
QTableWidget 是 Qt 提供的⼀种表格控件,类似于我们经常使⽤的 Excel 表格,可以将数据以表格的⽅式展⽰给⽤户
自动调整行高和列宽
QTableWidget 有几个函数自动调整表格的行高和列宽,分别如下:
resizeColumnsToContents():自动调整所有列的宽度,以适应其内容。
resizeColumnToContents(int column):自动调整列号为 co/www 的列的宽度。
resizeRowsToContents():自动调整所有行的高度,以适应其内容。
resizeRowToContents(int row):自动调整行号为 raw 的行的高度。
设置表格内容是否可编辑
setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);//双击或获取焦点后单击,进入编辑状态
setEditTriggers(QAbstractItemView::NoEditTriggers); //不允许编辑
设置行表头、列表头是否显示
horizontalHeader()->setVisible(checked);//是否显示水平表头
verticalHeader()->setVisible(checked);//是否显示垂直表头
选择模式
setSelectionBehavior(QAbstractltemView::Selectltems); 单元格选择
setSelectionBehavior(QAbstractltemView::SelectRows); 行选择
//显示窗口
m_configWidget = new QWidget();
//表格申请内存
m_table = new QTableWidget(m_configWidget);
m_configWidget->setMinimumSize(400, 600);
QVBoxLayout* tableLayout = new QVBoxLayout(m_configWidget);
tableLayout->addLayout(addrConfigLayout);
tableLayout->addWidget(m_table);
tableLayout->addWidget(m_tableError);
QStringList channelList = {1,2,3,4,5,6};
QStringList nameList={“test1”,“test2”,“test3”,“test4”,“test5”,“test6”};
QStringList rangeList={“2,5”,“8,13”,“1,46”,“89,456”,“,7,315”,“0,9”};
int size = channelList.size();
m_table->setRowCount(size); //设置行数
m_table->setColumnCount(3); // 设置列数
m_table->setHorizontalHeaderLabels(QStringList() << tr("通道(唯一)") << tr("名称(唯一)") << tr("范围(最小值,最大值)"));
for (int i = 0; i < size; i++)
{
m_table->blockSignals(true);
QTableWidgetItem * item = new QTableWidgetItem(channelList[i]);
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
m_table->setItem(i, 0, item);
item = new QTableWidgetItem(nameList[i]);
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
m_table->setItem(i, 1, item);
item = new QTableWidgetItem(rangeList[i]);
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
m_table->setItem(i, 2, item);
m_table->setColumnWidth(i, 100); //设置列宽
m_table->blockSignals(false);
}
// 初始化配置表格
m_table->setStyle(new MyProxyStyle);//去除选中虚框
m_table->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;color: black;}");//表头设置天空蓝背景色
m_table->setSelectionBehavior(QAbstractItemView::SelectRows); //设置表格选择方式
m_table->setSelectionMode(QAbstractItemView::ExtendedSelection); //多个选中的设置
m_table->setAlternatingRowColors(true); //设置行背景色(颜色) 交替
m_table->verticalHeader()->setVisible(false); //设置垂直头不可见
m_table->horizontalHeader()->setStretchLastSection(true); //最后一个单元格扩展
m_table->verticalHeader()->setDefaultSectionSize(24);
m_table->setContextMenuPolicy(Qt::CustomContextMenu); //右键菜单
//channel右键菜单
m_tableMenu = new QMenu(m_table);
QAction* actionadd = new QAction(tr("添加列"), m_tableMenu);
QAction* actiondel = new QAction(tr("删除选中列"), m_tableMenu);
m_tableMenu->addAction(actionadd);
m_tableMenu->addAction(actiondel);
//添加一行
connect(actionadd, &QAction::triggered, m_table, [=] {
m_table->blockSignals(true);
int row = m_table->rowCount();
m_table->setRowCount(m_table->rowCount() + 1);
QTableWidgetItem * item = new QTableWidgetItem(QString::number(row));
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
m_table->setItem(row, 0, item);
item = new QTableWidgetItem("channel");
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
m_table->setItem(row, 1, item);
item = new QTableWidgetItem("0,0,0");
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
m_table->setItem(row, 2, item);
m_table->blockSignals(false);
});
//删除列
connect(actiondel, &QAction::triggered, m_table, [=] {
m_table->blockSignals(true);
//选择的行
QModelIndexList indexList = m_table->selectionModel()->selectedRows();
QMap rowMap;
foreach(QModelIndex index, indexList)
{
rowMap.insert(index.row(), 0);
}
int row;
QMapIterator rowMapIterator(rowMap);
rowMapIterator.toBack();
while (rowMapIterator.hasPrevious())//倒序删除
{
rowMapIterator.previous();
row = rowMapIterator.key();
//释放内存
delete m_table->item(row, 0);
delete m_table->item(row, 1);
delete m_table->item(row, 2);
//删除tableWidget的行
m_table->removeRow(row);
}
m_table->blockSignals(false);
});
connect(m_table, &QTableWidget::customContextMenuRequested, this, [=] {
m_tableMenu->popup(QCursor::pos());
});