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();
}
相关推荐
LDR0061 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术1 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园1 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob1 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享1 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.1 天前
C语言--day30
c语言·开发语言
何以解忧,唯有..1 天前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang
謓泽1 天前
C语言不是语法,是通往机器的地图。
c语言·开发语言
云水一下1 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php
飞天狗1111 天前
零基础JavaWeb入门——第五课第二小节:九大内置对象 · 第2个:response(响应对象)
java·开发语言