欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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++中 Sort函數(shù)詳細解析

    C++中 Sort函數(shù)詳細解析

    這篇文章主要介紹了C++中 Sort函數(shù)詳細解析,sort函數(shù)是algorithm庫下的一個函數(shù),sort函數(shù)是不穩(wěn)定的,即大小相同的元素在排序后相對順序可能發(fā)生改變
    2022-08-08
  • C++實現(xiàn)基于靜態(tài)數(shù)組的順序表

    C++實現(xiàn)基于靜態(tài)數(shù)組的順序表

    這篇文章主要介紹了C++實現(xiàn)基于靜態(tài)數(shù)組的順序表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • C++學習之智能指針中的unique_ptr與shared_ptr

    C++學習之智能指針中的unique_ptr與shared_ptr

    吃獨食的unique_ptr與樂于分享的shared_ptr是C++中常見的兩個智能指針,本文主要為大家介紹了這兩個指針的使用以及智能指針使用的原因,希望對大家有所幫助
    2023-05-05
  • 詳解Matlab中自帶的Java操作合集

    詳解Matlab中自帶的Java操作合集

    其實Matlab中也有一些自帶的Java操作,例如:獲取鼠標在全屏位置、獲取當前剪切板內(nèi)容、獲取鼠標處像素顏色等,本文總結(jié)了七個這樣的操作,感興趣的可以了解一下
    2022-03-03
  • C語言快速實現(xiàn)掃雷小游戲

    C語言快速實現(xiàn)掃雷小游戲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • C語言二維數(shù)組指針的概念及使用

    C語言二維數(shù)組指針的概念及使用

    C語言中的二維數(shù)組是按行排列的,也就是先存放a[0]行,再存放a[1]行,最后存放a[2]行;每行中的4個元素也是依次存放。數(shù)組a為int類型,每個元素占用4個字節(jié),整個數(shù)組共占用48個字節(jié)
    2023-02-02
  • C++ Easylogging++日志庫配置使用超詳細講解

    C++ Easylogging++日志庫配置使用超詳細講解

    這篇文章主要介紹了C++ Easylogging++日志庫配置使用,Easylogging++是用于C++應用程序的單頭高效日志庫。它非常強大,高度可擴展并且可以根據(jù)用戶的要求進行配置
    2022-11-11
  • 淺談c和c++的某些小區(qū)別

    淺談c和c++的某些小區(qū)別

    下面小編就為大家?guī)硪黄獪\談c和c++的某些小區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • C語言 以字符串的形式讀寫文件詳解及示例代碼

    C語言 以字符串的形式讀寫文件詳解及示例代碼

    本文主要介紹 C語言以字符串的形式讀寫文件,這里提供了詳細的資料及簡單示例代碼以便大家學習參考,有學習此部分的小伙伴可以參考下
    2016-08-08
  • VS2017開發(fā)C語言出現(xiàn)“no_init_all“的解決辦法

    VS2017開發(fā)C語言出現(xiàn)“no_init_all“的解決辦法

    這篇文章介紹了VS2017開發(fā)C語言出現(xiàn)“no_init_all“的解決辦法,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12

最新評論