QT自定義之滑動開關(guān)
更新時間:2020年08月20日 14:05:24 作者:mario_z
這篇文章主要為大家詳細介紹了QT自定義之滑動開關(guān)效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了QT自定義之滑動開關(guān)的具體代碼,供大家參考,具體內(nèi)容如下
寫了一個簡單的滑動開關(guān), 不多說,上圖:
代碼如下:
#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(); }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實現(xiàn)基于靜態(tài)數(shù)組的順序表
這篇文章主要介紹了C++實現(xiàn)基于靜態(tài)數(shù)組的順序表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05C++學習之智能指針中的unique_ptr與shared_ptr
吃獨食的unique_ptr與樂于分享的shared_ptr是C++中常見的兩個智能指針,本文主要為大家介紹了這兩個指針的使用以及智能指針使用的原因,希望對大家有所幫助2023-05-05VS2017開發(fā)C語言出現(xiàn)“no_init_all“的解決辦法
這篇文章介紹了VS2017開發(fā)C語言出現(xiàn)“no_init_all“的解決辦法,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12