红外检测开关修改为开关样式,删除观瞄型设置
@ -0,0 +1,89 @@
|
||||
#include "imageswitch.h"
|
||||
#include <QPainter>
|
||||
|
||||
ImageSwitch::ImageSwitch(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
isChecked = false;
|
||||
buttonStyle = ButtonStyle_1;
|
||||
|
||||
imgOffFile = ":/res/switch/switch_close.png";
|
||||
imgOnFile = ":/res/switch/switch_open.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);
|
||||
}
|
||||
|
||||
QSize ImageSwitch::sizeHint() const
|
||||
{
|
||||
return QSize(87, 28);
|
||||
}
|
||||
|
||||
QSize ImageSwitch::minimumSizeHint() const
|
||||
{
|
||||
return QSize(87, 28);
|
||||
}
|
||||
|
||||
bool ImageSwitch::getChecked() const
|
||||
{
|
||||
return isChecked;
|
||||
}
|
||||
|
||||
void ImageSwitch::setChecked(bool isChecked)
|
||||
{
|
||||
if (this->isChecked != isChecked) {
|
||||
this->isChecked = isChecked;
|
||||
imgFile = isChecked ? imgOnFile : imgOffFile;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
ImageSwitch::ButtonStyle ImageSwitch::getButtonStyle() const
|
||||
{
|
||||
return this->buttonStyle;
|
||||
}
|
||||
|
||||
void ImageSwitch::setButtonStyle(const ImageSwitch::ButtonStyle &buttonStyle)
|
||||
{
|
||||
if (this->buttonStyle != buttonStyle) {
|
||||
this->buttonStyle = buttonStyle;
|
||||
|
||||
if (buttonStyle == ButtonStyle_1) {
|
||||
imgOffFile = ":/res/switch/switch_close.png";
|
||||
imgOnFile = ":/res/switch/switch_open.png";
|
||||
this->resize(87, 28);
|
||||
} else if (buttonStyle == ButtonStyle_2) {
|
||||
imgOffFile = ":/res/switch/switch_close1.png";
|
||||
imgOnFile = ":/res/switch/switch_open1.png";
|
||||
this->resize(87, 28);
|
||||
} else if (buttonStyle == ButtonStyle_3) {
|
||||
imgOffFile = ":/res/switch/switch_close3.png";
|
||||
imgOnFile = ":/res/switch/switch_open3.png";
|
||||
this->resize(96, 38);
|
||||
}
|
||||
|
||||
imgFile = isChecked ? imgOnFile : imgOffFile;
|
||||
setChecked(isChecked);
|
||||
this->update();
|
||||
updateGeometry();
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
#ifndef IMAGESWITCH_H
|
||||
#define IMAGESWITCH_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class ImageSwitch : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_ENUMS(ButtonStyle)
|
||||
|
||||
Q_PROPERTY(bool isChecked READ getChecked WRITE setChecked)
|
||||
Q_PROPERTY(ButtonStyle buttonStyle READ getButtonStyle WRITE setButtonStyle)
|
||||
|
||||
public:
|
||||
ImageSwitch();
|
||||
|
||||
|
||||
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:
|
||||
//默认尺寸和最小尺寸
|
||||
QSize sizeHint() const;
|
||||
QSize minimumSizeHint() const;
|
||||
|
||||
//获取和设置是否选中
|
||||
bool getChecked() const;
|
||||
void setChecked(bool isChecked);
|
||||
|
||||
//获取和设置按钮样式
|
||||
ButtonStyle getButtonStyle() const;
|
||||
void setButtonStyle(const ImageSwitch::ButtonStyle &buttonStyle);
|
||||
|
||||
signals:
|
||||
void checkedChanged(bool checked);
|
||||
|
||||
};
|
||||
|
||||
#endif // IMAGESWITCH_H
|
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 465 B |
After Width: | Height: | Size: 464 B |
After Width: | Height: | Size: 1.9 KiB |
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 330 B |
After Width: | Height: | Size: 328 B |
After Width: | Height: | Size: 1.6 KiB |