根据一位Qt博主的开源代码学习一下,顺便做个笔记。
- 新建一个类继承自QWidget
- 重写paintEvent函数把图片画出来
- 重写mousePressEvent处理数据点击事件,鼠标每次点击就切换按钮状态,并通过emit发送按钮状态信号
- 重写sizeHint函数,让定义的按钮在布局中可以适应大小
- 重写minimumSizeHint函数,定义最小大小
- 最后,可以根据需要自己定义加上setChecked和getChecked关于状态的函数,getButtonStyle和setButtonStyle关于状态的函数
.h文件
#ifndef IMAGESWITCH_H
#define IMAGESWITCH_H
/**
* 图片开关控件 作者:feiyangqingyun(QQ:517216493) 2016-11-25
* 1. 自带三种开关按钮样式。
* 2. 可自定义开关图片。
*/
#include <QWidget>
#ifdef quc
class Q_DECL_EXPORT ImageSwitch : public QWidget
#else
class ImageSwitch : public QWidget
#endif
{
Q_OBJECT
Q_ENUMS(ButtonStyle)
Q_PROPERTY(bool isChecked READ getChecked WRITE setChecked)
Q_PROPERTY(ButtonStyle buttonStyle READ getButtonStyle WRITE setButtonStyle)
public:
enum ButtonStyle {
ButtonStyle_1 = 0, //开关样式1
ButtonStyle_2 = 1, //开关样式2
ButtonStyle_3 = 2 //开关样式3
};
explicit ImageSwitch(QWidget *parent = 0);
protected:
void mousePressEvent(QMouseEvent *);
void paintEvent(QPaintEvent *event);
private:
bool isChecked;
ButtonStyle buttonStyle;
QString imgOffFile;
QString imgOnFile;
QString imgFile;
public:
bool getChecked() const;
ButtonStyle getButtonStyle() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//设置是否选中
void setChecked(bool isChecked);
//设置按钮样式
void setButtonStyle(const ImageSwitch::ButtonStyle &buttonStyle);
Q_SIGNALS:
void checkedChanged(bool checked);
};
#endif // IMAGESWITCH_H
.cpp文件
#pragma execution_character_set("utf-8")
#include "imageswitch.h"
#include "qpainter.h"
#include "qdebug.h"
ImageSwitch::ImageSwitch(QWidget *parent) : QWidget(parent)
{
isChecked = false;
buttonStyle = ButtonStyle_2;
imgOffFile = ":/image/imageswitch/btncheckoff2.png";
imgOnFile = ":/image/imageswitch/btncheckon2.png";
imgFile = imgOffFile;
}
void ImageSwitch::mousePressEvent(QMouseEvent *)
{
imgFile = isChecked ? imgOffFile : imgOnFile;
isChecked = !isChecked;
emit checkedChanged(isChecked);
this->update();
}
void ImageSwitch::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::SmoothPixmapTransform);
QImage img(imgFile);
img = img.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
//按照比例自动居中绘制
int pixX = rect().center().x() - img.width() / 2;
int pixY = rect().center().y() - img.height() / 2;
QPoint point(pixX, pixY);
painter.drawImage(point, img);
}
bool ImageSwitch::getChecked() const
{
return isChecked;
}
ImageSwitch::ButtonStyle ImageSwitch::getButtonStyle() const
{
return this->buttonStyle;
}
QSize ImageSwitch::sizeHint() const
{
return QSize(87, 28);
}
QSize ImageSwitch::minimumSizeHint() const
{
return QSize(87, 28);
}
void ImageSwitch::setChecked(bool isChecked)
{
if (this->isChecked != isChecked) {
this->isChecked = isChecked;
imgFile = isChecked ? imgOnFile : imgOffFile;
this->update();
}
}
void ImageSwitch::setButtonStyle(const ImageSwitch::ButtonStyle &buttonStyle)
{
if (this->buttonStyle != buttonStyle) {
this->buttonStyle = buttonStyle;
if (buttonStyle == ButtonStyle_1) {
imgOffFile = ":/image/imageswitch/btncheckoff1.png";
imgOnFile = ":/image/imageswitch/btncheckon1.png";
this->resize(87, 28);
} else if (buttonStyle == ButtonStyle_2) {
imgOffFile = ":/image/imageswitch/btncheckoff2.png";
imgOnFile = ":/image/imageswitch/btncheckon2.png";
this->resize(87, 28);
} else if (buttonStyle == ButtonStyle_3) {
imgOffFile = ":/image/imageswitch/btncheckoff3.png";
imgOnFile = ":/image/imageswitch/btncheckon3.png";
this->resize(96, 38);
}
imgFile = isChecked ? imgOnFile : imgOffFile;
setChecked(isChecked);
this->update();
updateGeometry();
}
}
这里是引用,https://gitee.com/feiyangqingyun