qml 与 qwidget结合做的一个 checkbox

1 篇文章 0 订阅
本文档介绍了一个使用QML和QWidget结合的实现,通过自定义的CCheckBox和CLabel组件,扩展了QLabel的点击事件,并在QML中创建了一个带勾选功能的矩形元素。在C++中,定义了CCheckBox类来处理信号和槽,同时封装了信号以实现QLabel的点击交互。此外,还提供了项目的.pro配置文件和主函数main.cpp的代码。
摘要由CSDN通过智能技术生成


前言

在这里插入图片描述
在这里插入图片描述

勾选部分使用了qml, 说明部分使用了QLabel, 通过qml和qwidget交互实现checkbox功能。
另QLabel本身不支持clicked事件, 在此自己封装了一层clicked事件。

pro文件

QT       += core gui quickwidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    CCheckBox.cpp \
    CLabel.cpp \
    main.cpp \

HEADERS += \
    CCheckBox.h \
    CLabel.h \

FORMS += \
    CCheckBox.ui \

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

DISTFILES += \
    CCheckItem.qml

main.cpp

#include <QApplication>
#include "CCheckBox.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CCheckBox w;
    w.show();
    return a.exec();
}

CCheckItem.qml

import QtQuick 2.0

Rectangle {
    id: rectangle
    signal qslotChecked
    signal qsigChecked(bool bChecked)
    property bool bChecked: false

    width: 200
    height: 200
    radius: 90
    color: "red"
    clip:true

    MouseArea{
        anchors.fill: parent
        onClicked: {
            bChecked = changeBoolValue(bChecked);
            rectangle.color = getColor(bChecked)

            qsigChecked(bChecked)
        }
    }

    function changeBoolValue(bFlag){
        var bRes;

        if (bFlag == true)
        {
            bRes = false;
        }
        else
        {
            bRes = true;
        }

        return bRes;
    }

    function getColor(bChecked){
        var bRes;

        if (bChecked == true)
        {
            bRes = "green";
        }
        else
        {
            bRes = "red";
        }

        return bRes;
    }

    onQslotChecked: {
        bChecked = changeBoolValue(bChecked);
        rectangle.color = getColor(bChecked)
    }

}

CCheckBox(.h, .cpp)

#ifndef CCHECKBOX_H
#define CCHECKBOX_H

#include <QWidget>
#include <QDebug>

namespace Ui {
class CCheckBox;
}

class CCheckBox : public QWidget
{
    Q_OBJECT

public:
    explicit CCheckBox(QWidget *parent = nullptr);
    ~CCheckBox();

private:
    Ui::CCheckBox *ui;

signals:
    void sigClicked();

public slots:
    void slotClicked();
    void slotChecked(bool bChecked);
};

#endif // CCHECKBOX_H

#include "CCheckBox.h"
#include "ui_CCheckBox.h"

CCheckBox::CCheckBox(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CCheckBox)
{
    ui->setupUi(this);


    QObject *pRoot = (QObject*)ui->quickWidget->rootObject();
    if (pRoot != NULL)
    {
        connect(pRoot, SIGNAL(qsigChecked(bool)), this, SLOT(slotChecked(bool)));
        connect(this, SIGNAL(sigClicked()), pRoot, SIGNAL(qslotChecked()));
    }

    connect(ui->label, &CLabel::sigClicked, this, &CCheckBox::slotClicked);
}

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

void CCheckBox::slotClicked()
{
    emit sigClicked();
}

void CCheckBox::slotChecked(bool bChecked)
{
    qDebug()<<"slotChecked from qml."<<bChecked;
}

CLabel(.h, .cpp)

#ifndef CLABEL_H
#define CLABEL_H

#include <QLabel>
#include <QKeyEvent>
#include <QDebug>

class CLabel : public QLabel
{
    Q_OBJECT
public:
    explicit CLabel(QWidget *parent = nullptr);

private:
    bool m_bPressed;

signals:
    void sigClicked();

private slots:
    void mousePressEvent(QMouseEvent *) override;
    void mouseReleaseEvent(QMouseEvent *) override;

};

#endif // CLABEL_H

#include "CLabel.h"

CLabel::CLabel(QWidget *parent) : QLabel(parent)
{
    m_bPressed = false;
}

void CLabel::mousePressEvent(QMouseEvent*)
{
    m_bPressed = true;
}

void CLabel::mouseReleaseEvent(QMouseEvent*)
{
    if (m_bPressed == true)
    {
        QPoint cPos = mapFromGlobal(QCursor::pos());

        if (cPos.x() >= 0 && cPos.x() <= width() && cPos.y() >= 0 && cPos.y() <= height())
        {
            emit sigClicked();
        }
    }

    m_bPressed = false;
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值