您好,登錄后才能下訂單哦!
這篇文章主要介紹了QT自定義之滑動開關的實現方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
寫了一個簡單的滑動開關, 不多說,上圖:
代碼如下:
#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可設置button大小。
set_button_color可設置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(); }
感謝你能夠認真閱讀完這篇文章,希望小編分享QT自定義之滑動開關的實現方法內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。