Qtは、カスタムスライドボタンを実装します

  次のレコード今日、スライドボタンで効果を達成するために、Qtの既存のコンポーネントを使用しました。私はそれが同様の前に行われていたと思いますが、イベントにpaintEventストロークは絵で、私がやる、怠惰な、既製の部品を使用することで、以下の効果を見て、それが実装します:

  

  これは、一緒にQLabelで、上記の効果を達成するためにQWidgetの継承のカスタムクラスの使用です。ところで、あなたは、使用中、将来のプロジェクトを容易にするために、組み立てダウン後にやって自分を保存することができます。

ADOは、直接コアコード上で、決して来て、手動でそれを体験して、再生されません。

  ここでは、ヘッダファイルには、次のとおりです。

#ifndefのCUSTOMBUTTON_H
 の#define CUSTOMBUTTON_H 

の#include <QWidgetの> 
する#include <QLabel> 
の#include <QTimer> 
の#include <QMouseEvent> 
の#include <QStyleOption> 
の#include <QPainterの> クラス CustomButton:公共QWidgetの
{ 
    Q_OBJECTの公共明示 CustomButton(QWidgetの*親= nullptr、INT幅= 50int型の高さ= 20 )。プライベート
    QLabel * mylabelという。
    QTimerタイマー;





    int型のm_width;
     int型m_height;
     int型のDIR;
     int型位置;
     int型の最大値;
     int型の分;
     int型の長さ;
 保護無効 paintEvent(QPaintEvent * イベントオーバーライド ; // これは書き直さなければなりませんが、そうでない場合、スタイルシートが無効なロード。
    ボイド mousePressEvent(QMouseEvent * イベントオーバーライド;
 公共スロット:
     ボイド move_slot(); // タイマーのタイムアウト溝がスライドボタンの効果を達成するように機能
公開ボイドのinit(); 
};

#endifの // CUSTOMBUTTON_H

 

  ここでCPPファイルは次のとおりです。

#include "CustomButton.h"

/*** 我的是MSVC的编译器,必须要这段代码,不然中文就是乱码。如果Mingw编译器可以忽略。
#if _MSC_VER >=1800
#pragma execution_character_set("utf-8")
#endif
********/
CustomButton::CustomButton(QWidget *parent,int width, int height) : QWidget(parent),m_width(width),m_height(height),position(0)
{
    max = qMax(m_width,m_height);
    min = m_width>m_height?m_height:m_width;
    length = max - min;
    dir = m_width>=m_height? Qt::AlignLeft : Qt::AlignTop;
    connect(&timer,SIGNAL(timeout()),this,SLOT(move_slot()));
    init();
}

void CustomButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);

}

void CustomButton::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        switch (dir)
        {
        case Qt::AlignLeft: case Qt::AlignRight:
            position = myLabel->pos().x();
            timer.start(5);
            break;

        case Qt::AlignTop: case Qt::AlignBottom:
            position = myLabel->pos().y();
            timer.start(5);
            break;
        }
    }
    QWidget::mousePressEvent(event);
}

void CustomButton::move_slot()
{
    switch(dir)
    {
    case Qt::AlignLeft:
        if(position>length)
        {
            timer.stop();
            this->setStyleSheet(QString("CustomButton{background-color:red;border-radius:%1px;}").arg(min/2));
            myLabel->setText("");
            myLabel->setStyleSheet(QString("#myLabel{background-color: /*lightgrey*/rgb(144,236,144);border-radius:%1px}").arg(min/2));
            dir = Qt::AlignRight;
            return;
        }
        ++position;
        myLabel->move(position,0);
        break;

    case Qt::AlignRight:
        if(position<=0)
        {
            timer.stop();
            this->setStyleSheet(QString("CustomButton{background-color:rgb(144,236,144);border-radius:%1px;}").arg(min/2));
            myLabel->setStyleSheet(QString("#myLabel{background-color:red;border-radius:%1px;}").arg(min/2));
            myLabel->setText("");
            dir = Qt::AlignLeft;
            return;
        }
        --position;
        myLabel->move(position,0);
        break;

    case Qt::AlignTop:
        if(position>length)
        {
            timer.stop();
            this->setStyleSheet(QString("CustomButton{background-color:red;border-radius:%1px;}").arg(min/2));
            myLabel->setText("");
            myLabel->setStyleSheet(QString("#myLabel{background-color: /*lightgrey*/rgb(144,236,144);border-radius:%1px}").arg(min/2));
            dir = Qt::AlignBottom;
            return;
        }
        ++position;
        myLabel->move(0,position);
        break;

    case Qt::AlignBottom:
        if(position<=0)
        {
            timer.stop();
            this->setStyleSheet(QString("CustomButton{background-color:rgb(144,236,144);border-radius:%1px;}").arg(min/2));
            myLabel->setStyleSheet(QString("#myLabel{background-color:red;border-radius:%1px;}").arg(min/2));
            myLabel->setText("");
            dir = Qt::AlignTop;
            return;
        }
        --position;
        myLabel->move(0,position);
        break;
    }

}

void CustomButton::init()
{
    this->resize(m_width,m_height);
    QString leftBtn_style = QString("CustomButton{background-color:rgb(144,236,144);border-radius:%1px;}").arg(min/2);
    this->setStyleSheet(leftBtn_style);
    myLabel = new QLabel(this);
    myLabel->setObjectName("myLabel");
    myLabel->resize(min,min);
    myLabel->setText("");
    myLabel->setAlignment(Qt::AlignCenter);
    QString lab_style = QString("#myLabel{background-color: red;border-radius:%1px}").arg(min/2);
    myLabel->setStyleSheet(lab_style);
    this->setFixedSize(m_width,m_height);
}

 

最后呢,在主函数调用下,并new一个我们写好的类的实例,来展示效果吧。

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
   btn1 = new CustomButton(this,58,22);
   btn2 = new CustomButton(this,60,20);
   btn3 = new CustomButton(this,18,50);
   btn4 = new CustomButton(this,50,22);
   label1 = new QLabel("客厅开关:");
   label2 = new QLabel("卧室开关:");
   label3 = new QLabel("水泵开关:");
   label4 = new QLabel("总闸开关:");
   form = new QFormLayout(this); //这里是表单布局
   form->addRow(label1,btn1);
   form->addRow(label2,btn2);
   form->addRow(label3,btn3);
   form->addRow(label4,btn4);
   setStyleSheet("QWidget{background-color:rgb(124,134,146);}.QLabel{background-color:lightgrey;border-radius:3px;}");
   resize(sizeHint());
}

  上面的代码,实现的功能如下:

  1. 可以自定义组件的大小,实例化时可以不指定大小,因为有默认的参数。

  2. 如果宽度小于高度,就会实现上下滑动,否则就是左右滑动。

  目前就是这些功能,当然还可以添加自定义颜色呀之类的,那么只要在源码里添加相关的接口供外部调用就行了。

 

おすすめ

転載: www.cnblogs.com/dz-study/p/12466969.html