Qt中继承QCheckBox的类结合QTableWidget实现多选并且每个多选的id都不一样

1.相关描述

继承QCheckBox的类MyCheckBox,利用QTableWidget的setCellWidget方式添加MyCheckBox类的对象

2.相关页面

3.相关代码

mycheckbox.h

cpp 复制代码
#ifndef MYCHECKBOX_H
#define MYCHECKBOX_H

#include <QCheckBox>
#include <QObject>

class MyCheckBox : public QCheckBox
{
public:
    MyCheckBox(QWidget *parent = nullptr);

    void SetID(int id);
    int GetID();
private:
    int mId;
};

#endif // MYCHECKBOX_H

mycheckbox.cpp

cpp 复制代码
#include "mycheckbox.h"
#include <QDebug>
MyCheckBox::MyCheckBox(QWidget *parent):QCheckBox(parent) {}

void MyCheckBox::SetID(int id)
{
    this->mId = id;
}

int MyCheckBox::GetID()
{
    return mId;
}

mainwindow.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public slots:
    void recvCheckBox(bool checked);
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp

cpp 复制代码
#include "checkboxdelegate.h"
#include "mainwindow.h"
#include "mycheckbox.h"
#include "ui_mainwindow.h"

#include <QCheckBox>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tableWidget->setColumnCount(3);
    ui->tableWidget->setRowCount(3);
    for(int i = 0; i < 3; i++){
        MyCheckBox *checkBox = new MyCheckBox();
        checkBox->setText(QString::number(i+1));
        QWidget *widget = new QWidget();
        QHBoxLayout *layout = new QHBoxLayout(widget);
        layout->addWidget(checkBox);
        layout->setAlignment(Qt::AlignCenter);
        layout->setContentsMargins(0, 0, 0, 0);
        widget->setLayout(layout);
        ui->tableWidget->setCellWidget(i, 0, widget);
        checkBox->SetID(i);
        connect(checkBox, &MyCheckBox::clicked, this, &MainWindow::recvCheckBox);
    }
    // ui->tableWidget->insertRow(3);
    // MyCheckBox *checkBox = new MyCheckBox();
    // checkBox->setText(QString::number(3));
    // QWidget *widget = new QWidget();
    // QHBoxLayout *layout = new QHBoxLayout(widget);
    // layout->addWidget(checkBox);
    // layout->setAlignment(Qt::AlignCenter);
    // layout->setContentsMargins(0, 0, 0, 0);
    // widget->setLayout(layout);
    // ui->tableWidget->setCellWidget(3, 0, widget);
    // checkBox->SetID(3);
    // connect(checkBox, &MyCheckBox::clicked, this, &MainWindow::recvCheckBox);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::recvCheckBox(bool checked)
{
    MyCheckBox* checkBox = static_cast<MyCheckBox*>(sender());
    qDebug() << "checked = " << checked << ";id = " << checkBox->GetID();
}
相关推荐
疯狂的挖掘机5 小时前
记一次基于QT的图片操作处理优化思路(包括在图上放大缩小,截图,画线,取值等)
开发语言·数据库·qt
cnxy1885 小时前
围棋对弈Python程序开发完整指南:步骤4 - 提子逻辑和劫争规则实现
开发语言·python·机器学习
意趣新5 小时前
C 语言源文件从编写完成到最终生成可执行文件的完整、详细过程
c语言·开发语言
李艺为6 小时前
根据apk包名动态修改Android品牌与型号
android·开发语言
奇树谦6 小时前
Qt | 利用map创建多个线程和定时器
网络·数据库·qt
黄河滴滴6 小时前
java系统变卡变慢的原因是什么?从oom的角度分析
java·开发语言
老华带你飞6 小时前
农产品销售管理|基于java + vue农产品销售管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
superman超哥7 小时前
Rust Workspace 多项目管理:单体仓库的优雅组织
开发语言·rust·多项目管理·rust workspace·单体仓库
kylezhao20197 小时前
C#通过HSLCommunication库操作PLC用法
开发语言·c#
JIngJaneIL8 小时前
基于springboot + vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端