目錄
1.簡介
本文主要介紹瞭如何在QTableWidget表格的單元格中添加和刪除控件,內容包含動態效果圖、重點講解和源碼,讀者們可以方便查看學習和交流。
回目錄
2.效果圖
3.重點講解
1) 通過以下語句,我們可以在指定單元格中設置我們期望的控件(如示例中的勾選框、下拉框、按鈕、旋鈕框等); ui->tableWidget->setCellWidget(row, col, wdgAdd);
2)單元格中刪除控件的語句如下:
ui->tableWidget->removeCellWidget(row, col);
從下面的代碼片段及實際運行打印信息發現,以上remove僅僅是把控件從單元格中移除,但是並未delete釋放掉,需要主動釋放避免內存泄漏,大家使用中請注意該問題;
QWidget *wdgPre = ui->tableWidget->cellWidget(row, col); //之前的控件
QString strMsg = tabPosInfo;
if( nullptr != wdgPre ){
qDebug() << "removeCellWidget at(" << row << "," << col << ")";
ui->tableWidget->removeCellWidget(row, col);
QString objN = wdgPre->objectName();
delete wdgPre;
wdgPre = nullptr;
strMsg += QString("移除控件\"%1\",").arg(objN);
}
ui->tableWidget->setCellWidget(row, col, wdgAdd);
strMsg += QString("添加控件\"%1\"").arg(txt);
ui->plainTextEdit->appendPlainText(strMsg);
打印信息截圖:
4.源碼
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
enum CellWidgetType {
CWT_CheckBox, // 勾選框
CWT_SpinBox, // 旋鈕
CWT_PushButton, // 按鈕
CWT_ComboBox, // 下拉框
};
private slots:
void on_btnAddCellWdg_clicked();
void on_tableWidget_cellClicked(int row, int column);
private:
void initData(); // 初始化數據
void initForm(); // 初始化窗體
private:
Ui::Widget *ui;
QHash<int, QString> m_hashIdxType; // <索引, 控件名稱>
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QCheckBox>
#include <QPushButton>
#include <QComboBox>
#include <QSpinBox>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
initData();
initForm();
}
Widget::~Widget()
{
delete ui;
}
void Widget::initData()
{
m_hashIdxType.insert(CWT_CheckBox, "勾選框");
m_hashIdxType.insert(CWT_SpinBox, "旋鈕");
m_hashIdxType.insert(CWT_PushButton, "按鈕");
m_hashIdxType.insert(CWT_ComboBox, "下拉框");
}
void Widget::initForm()
{
{
int count = 3;
QString strPre("列");
QStringList headerLabs;
for(int i=0; i<count; i++){
QString lab = strPre + QString::number(i+1);
headerLabs.append(lab);
}
ui->tableWidget->setColumnCount(count);
ui->tableWidget->setHorizontalHeaderLabels(headerLabs);
ui->spinBoxCol->setRange(1, count);
}
{
int count = 10;
QString strPre("行");
QStringList headerLabs;
for(int i=0; i<count; i++){
QString lab = strPre + QString::number(i+1);
headerLabs.append(lab);
}
ui->tableWidget->setRowCount(count);
ui->tableWidget->setVerticalHeaderLabels(headerLabs);
ui->spinBoxRow->setRange(1, count);
}
QStringList listType = m_hashIdxType.values();
ui->comboBox->addItems(listType);
}
void Widget::on_btnAddCellWdg_clicked()
{
int row = ui->spinBoxRow->value();
int col = ui->spinBoxCol->value();
QString tabPosInfo = QString("單元格(行%1列%2)").arg(row).arg(col);
QString tabPos = QString("%1%2").arg(row).arg(col);
QString strTypeName = ui->comboBox->currentText();
int curIdx = m_hashIdxType.key(strTypeName);
QString txt = strTypeName + tabPos;
QWidget *wdgAdd = nullptr;
switch (curIdx) {
case CWT_CheckBox:{
QCheckBox *chkBox = new QCheckBox(txt);
chkBox->setChecked(true);
wdgAdd = chkBox;
break;
}
case CWT_SpinBox:{
QSpinBox *spinBox = new QSpinBox;
QString strVal = QString("%1%2").arg(row).arg(col);
spinBox->setValue(strVal.toInt());
wdgAdd = spinBox;
break;
}
case CWT_PushButton:{
wdgAdd = new QPushButton(txt);
break;
}
case CWT_ComboBox:{
QComboBox *cBox = new QComboBox;
cBox->addItem(txt);
wdgAdd = cBox;
break;
}
default:
break;
}
if( nullptr != wdgAdd ){
row--; // 轉換爲tab中真實的行
col--; // 轉換爲tab中真實的列
wdgAdd->setObjectName(txt);
QWidget *wdgPre = ui->tableWidget->cellWidget(row, col); //之前的控件
QString strMsg = tabPosInfo;
if( nullptr != wdgPre ){
qDebug() << "removeCellWidget at(" << row << "," << col << ")";
ui->tableWidget->removeCellWidget(row, col);
QString objN = wdgPre->objectName();
delete wdgPre;
wdgPre = nullptr;
strMsg += QString("移除控件\"%1\",").arg(objN);
}
ui->tableWidget->setCellWidget(row, col, wdgAdd);
strMsg += QString("添加控件\"%1\"").arg(txt);
ui->plainTextEdit->appendPlainText(strMsg);
}
}
void Widget::on_tableWidget_cellClicked(int row, int column)
{
ui->spinBoxCol->setValue(column+1);
ui->spinBoxRow->setValue(row+1);
}
加油,向未來!GO~
Come on!