Examples of QT program - QTableWidget Form add / delete cells control

table of Contents

1. Introduction
2. renderings
3. focus on explaining
4. Source



1 Introduction

This article describes how to add a table in the cell QTableWidget and remove controls, including dynamic content rendering, and focus on explaining the source, readers can easily view learning and communication.


Back catalog

2. renderings

Run renderings
Run renderings


Back catalog

3. focus on explaining

1) by the following statement, we can set our desired control (as in the example check boxes, drop-down boxes, buttons, knobs, block, etc.) in the specified cell;
       ui->tableWidget->setCellWidget(row, col, wdgAdd);

2) cell to remove a control statement is as follows:

         ui->tableWidget->removeCellWidget(row, col);

Print information found that more than just remove the control is removed from the cell, but did not delete freed, the need for active release to avoid memory leaks, note that the use of problem from the following code fragment and the actual operation;

       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);

Print information Screenshot:
Here Insert Picture Description


Back catalog

4. Source

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);
}

Back catalog




Come on, into the future! ~ GO
Come ON!


Published 18 original articles · won praise 3 · Views 3950

Guess you like

Origin blog.csdn.net/Redboy_Crazy/article/details/105235707