feat: v1.4 添加和使用一个自定义控件类

This commit is contained in:
touwoyimuli 2019-12-06 00:04:18 +08:00
parent 90c8883162
commit 76f5be9614
4 changed files with 170 additions and 2 deletions

View File

@ -0,0 +1,70 @@
/*
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
*
* Author: touwoyimuli <touwoyimuli@gmai.com>
*
* github: https://github.com/touwoyimuli
* blogs: https://touwoyimuli.github.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://touwoyimuli.github.io/>.
*/
#include "ExSwitchButton.h"
//#include "qglobal.h"
EXWIDGET_BEGIN_NAMESPACE
//xxxPrivate class 会在 xxx.cpp 里面来实现
/*!
* \~chinese \brief ExSwitchButtonPrivate
*/
ExSwitchButtonPrivate::ExSwitchButtonPrivate(ExSwitchButton *qq)
{
}
ExSwitchButtonPrivate::~ExSwitchButtonPrivate()
{
}
void ExSwitchButtonPrivate::init()
{
m_checked = false; //xxxxPrivate 自带的变量
Q_Q(ExSwitchButton); //Q_Q 可以在 xxxxPrivate 里面调用 xxxx 的方法和变量
q->setObjectName("ExSwitchButtonPrivate");
q->setCheckable(true);
q->connect(q, SIGNAL(toggled(bool)), q, SLOT(onChange(bool)));
}
ExSwitchButtonPrivate::ExSwitchButton()
{
}
void ExSwitchButton::onChange(bool)
{
Q_D(ExSwitchButton);
d->m_checked = (!d->m_checked);
qDebug()<<"------01-----ExSwitchButton::onChange(bool):"<<d->m_checked;
}
EXWIDGET_END_NAMESPACE

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
*
* Author: touwoyimuli <touwoyimuli@gmai.com>
*
* github: https://github.com/touwoyimuli
* blogs: https://touwoyimuli.github.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://touwoyimuli.github.io/>.
*/
#ifndef EXSWITCHBUTTON_H
#define EXSWITCHBUTTON_H
#include "ExMyGlobal.h"
#include "ExSwitchButton_p.h"
#include <QAbstractButton>
EXWIDGET_BEGIN_NAMESPACE
class ExSwitchButton : public QAbstractButton
{
public:
explicit ExSwitchButton();
~ExSwitchButton();
private slots:
void onChange(bool);
};
EXWIDGET_END_NAMESPACE
#endif

View File

@ -0,0 +1,51 @@
/*
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
*
* Author: touwoyimuli <touwoyimuli@gmai.com>
*
* github: https://github.com/touwoyimuli
* blogs: https://touwoyimuli.github.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://touwoyimuli.github.io/>.
*/
#ifndef EXSWITCHBUTTON_P_H
#define EXSWITCHBUTTON_P_H
#include "ExMyGlobal.h"
#include <QWidget>
EXWIDGET_BEGIN_NAMESPACE
/*!
* \~chinese \class ExSwitchButtonPrivate
* \~chinese \brief ExSwitchButton 便/, _Private
* \~chinese \sa Qt QPushButtonPrivate QPushButton
*/
class ExSwitchButton;
class ExSwitchButtonPrivate //自定义控件通常继承于 QObjectPrivate 之类的
{
public:
ExSwitchButtonPrivate(ExSwitchButton *qq);
~ExSwitchButtonPrivate();
void init();
public:
bool m_checked; //switch 是否处于开启状态
};
EXWIDGET_END_NAMESPACE
#endif

View File

@ -27,12 +27,15 @@ CONFIG += c++11
SOURCES += \ SOURCES += \
main.cpp \ main.cpp \
Example.cpp \ Example.cpp \
ExMyStyle.cpp ExMyStyle.cpp \
MyWidget/ExSwitchButton.cpp
HEADERS += \ HEADERS += \
Example.h \ Example.h \
ExMyStyle.h \ ExMyStyle.h \
ExMyGlobal.h ExMyGlobal.h \
MyWidget/ExSwitchButton.h \
MyWidget/ExSwitchButton_p.h
# Default rules for deployment. # Default rules for deployment.