2020-02-20 00:47:18 +08:00
|
|
|
#include "myswitchbutton.h"
|
|
|
|
#include "myswitchbutton_p.h"
|
|
|
|
#include "mystyle.h"
|
|
|
|
|
|
|
|
#include <QStylePainter>
|
2020-02-22 12:43:23 +08:00
|
|
|
#include <QStyleOption>
|
2020-02-23 13:14:55 +08:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
class MyStylePainter;
|
2020-02-20 00:47:18 +08:00
|
|
|
|
|
|
|
MySwitchButton::MySwitchButton(QWidget *parent)
|
|
|
|
: QAbstractButton(parent)
|
|
|
|
{
|
|
|
|
Q_D(MySwitchButton);
|
|
|
|
|
|
|
|
d->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
MySwitchButton::~MySwitchButton()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize MySwitchButton::sizeHint() const
|
|
|
|
{
|
2020-02-23 13:14:55 +08:00
|
|
|
return QSize(60, 40);
|
2020-02-20 00:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MySwitchButton::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
|
|
|
|
|
|
|
QStyleOptionButton opt;
|
|
|
|
initStyleOption(&opt);
|
|
|
|
|
2020-02-23 13:14:55 +08:00
|
|
|
MyStylePainter painter(this);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.drawControl(MyStyle::CE_SwitchButton, &opt);
|
2020-02-20 00:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MySwitchButton::initStyleOption(QStyleOptionButton *opt) const
|
|
|
|
{
|
|
|
|
if (!opt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
opt->init(this);
|
|
|
|
opt->initFrom(this);
|
|
|
|
|
|
|
|
if (isChecked())
|
|
|
|
opt->state |= QStyle::State_On;
|
|
|
|
else
|
|
|
|
opt->state |= QStyle::State_Off;
|
|
|
|
}
|
|
|
|
|
|
|
|
MySwitchButtonPrivate::MySwitchButtonPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MySwitchButtonPrivate::~MySwitchButtonPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MySwitchButtonPrivate::init()
|
|
|
|
{
|
|
|
|
Q_Q(MySwitchButton);
|
|
|
|
|
2020-02-23 13:14:55 +08:00
|
|
|
check = false;
|
2020-02-20 00:47:18 +08:00
|
|
|
q->setObjectName("MySwitchButton");
|
2020-02-23 13:14:55 +08:00
|
|
|
q->setChecked(true);
|
2020-03-28 17:33:20 +08:00
|
|
|
q->setCheckable(true); //clicked toggled 都需要开启
|
2020-02-23 13:14:55 +08:00
|
|
|
q->connect(q, SIGNAL(clicked(bool)), q, SLOT(setChecked(bool)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MySwitchButtonPrivate::switchCheck()
|
|
|
|
{
|
|
|
|
return check;
|
2020-02-20 00:47:18 +08:00
|
|
|
}
|