qml 与 qwidget结合做的一个 checkbox


前言

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

勾选部分使用了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;
}


  • 0
    点赞
  • 3
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页

打赏

朴勇佑

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者