QT自定義之滑動(dòng)開(kāi)關(guān)
本文實(shí)例為大家分享了QT自定義之滑動(dòng)開(kāi)關(guān)的具體代碼,供大家參考,具體內(nèi)容如下
寫(xiě)了一個(gè)簡(jiǎn)單的滑動(dòng)開(kāi)關(guān), 不多說(shuō),上圖:

代碼如下:
#ifndef SLIDERBUTTON_H
#define SLIDERBUTTON_H
#include <QWidget>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QPen>
#include <QPainterPath>
#include <QColor>
#include <QTimer>
#include <QDebug>
namespace Ui {
class SliderButton;
}
class SliderButton : public QWidget
{
Q_OBJECT
public:
explicit SliderButton(QWidget *parent = nullptr);
~SliderButton();
void set_button_size(const int &w, const int &h);
void set_button_color(const QColor & , const QColor & ,const QColor & );
signals:
void signal_button_on();
void signal_button_off();
protected:
virtual void mousePressEvent(QMouseEvent *event);
virtual void paintEvent(QPaintEvent *event);
public slots:
void slot_update();
private:
bool m_button_status;
int m_circle_width;
int m_button_pos;
int m_move_distance;
QColor m_backcolor_on;
QColor m_backcolor_off;
QColor m_circle_color;
QTimer *m_timer;
};
#endif // SLIDERBUTTON_H
set_button_size可設(shè)置button大小。
set_button_color可設(shè)置button顏色
#include "sliderbutton.h"
SliderButton::SliderButton(QWidget *parent) :
QWidget (parent),
m_button_status(false),
m_circle_width(30),
m_button_pos(0),
m_move_distance(20),
m_backcolor_on(Qt::red),
m_backcolor_off(Qt::blue),
m_circle_color(Qt::black)
{
setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(slot_update()));
}
SliderButton::~SliderButton()
{
}
void SliderButton::set_button_size(const int & width, const int &heigh)
{
m_circle_width = heigh;
m_move_distance = width;
}
void SliderButton::set_button_color(const QColor &on_color, const QColor &off_color, const QColor &button_color)
{
m_backcolor_on = on_color;
m_backcolor_off = off_color;
m_circle_color = button_color;
}
void SliderButton::mousePressEvent(QMouseEvent *event)
{
Q_UNUSED(event)
if (false == m_button_status)
{
m_button_status = true;
emit signal_button_off();
}
else
{
m_button_status = false;
emit signal_button_on();
}
m_timer->start(1);
}
void SliderButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
QPainterPath path;
painter.setRenderHint(QPainter::Antialiasing, true);
if (m_button_status == true)
{
painter.setBrush(m_backcolor_off);
}
else
{
painter.setBrush(m_backcolor_on);
}
QRect rect (0, 0, m_circle_width, m_circle_width);
int startX = rect.left() + rect.width() / 2;
int startY = rect.top();
path.moveTo(startX,startY);
path.arcTo(QRect(rect.left(), rect.top(), rect.width(), rect.height()),90,180);
path.lineTo((rect.left() + m_move_distance ), rect.bottom() + 1); // the graph not connet , neet 1 pixcel
path.arcTo(QRect((startX + m_move_distance),rect.top(),rect.width(),rect.height()),270,180);
path.lineTo(startX,startY);
painter.drawPath(path);
// draw small circle
painter.setBrush(m_circle_color);
painter.drawEllipse(m_button_pos ,0,m_circle_width,m_circle_width);
}
void SliderButton::slot_update()
{
if (m_button_status == true)
{
m_button_pos += 1;
if (m_button_pos == m_move_distance + m_circle_width / 2)
{
m_timer->stop();
}
}
else if(m_button_status == false)
{
m_button_pos -= 1;
if (m_button_pos == 0)
{
m_timer->stop();
}
}
update();
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)基于靜態(tài)數(shù)組的順序表
這篇文章主要介紹了C++實(shí)現(xiàn)基于靜態(tài)數(shù)組的順序表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
C++學(xué)習(xí)之智能指針中的unique_ptr與shared_ptr
吃獨(dú)食的unique_ptr與樂(lè)于分享的shared_ptr是C++中常見(jiàn)的兩個(gè)智能指針,本文主要為大家介紹了這兩個(gè)指針的使用以及智能指針使用的原因,希望對(duì)大家有所幫助2023-05-05
C++ Easylogging++日志庫(kù)配置使用超詳細(xì)講解
這篇文章主要介紹了C++ Easylogging++日志庫(kù)配置使用,Easylogging++是用于C++應(yīng)用程序的單頭高效日志庫(kù)。它非常強(qiáng)大,高度可擴(kuò)展并且可以根據(jù)用戶的要求進(jìn)行配置2022-11-11
C語(yǔ)言 以字符串的形式讀寫(xiě)文件詳解及示例代碼
本文主要介紹 C語(yǔ)言以字符串的形式讀寫(xiě)文件,這里提供了詳細(xì)的資料及簡(jiǎn)單示例代碼以便大家學(xué)習(xí)參考,有學(xué)習(xí)此部分的小伙伴可以參考下2016-08-08
VS2017開(kāi)發(fā)C語(yǔ)言出現(xiàn)“no_init_all“的解決辦法
這篇文章介紹了VS2017開(kāi)發(fā)C語(yǔ)言出現(xiàn)“no_init_all“的解決辦法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12

