QT4 自己封装的QSlider 滑块 滚动条 控件源码

.h 文件

#ifndef MYQSLIDER_H
#define MYQSLIDER_H

#include <QDialog>
#include <QLineEdit>
#include <QSlider>


class MyqSlider : public QDialog
{
    Q_OBJECT
public:
    explicit MyqSlider(int startx,int starty,int wide,int hidth,QWidget *parent = 0);
    ~MyqSlider();
signals:
public slots:
    void setLineEditValue(int value);
private:
    QLineEdit *lineEdit;
    QSlider* slider;

};

#endif // MYQSLIDER_H

。cpp文件

#include "myqslider.h"
#include <QHBoxLayout>

MyqSlider::MyqSlider(int startx,int starty,int wide,int hidth,QWidget *parent) :
     QDialog(parent)
{
    lineEdit = new QLineEdit("50");

    slider = new QSlider(Qt::Horizontal);
    slider->setMinimum(0);      // 设置滑动条的最小值
    slider->setMaximum(100);   // 设置滑动条的最大值

    slider->setValue(50); // 设置滑动条初始值

    connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setLineEditValue(int)));

    QHBoxLayout *layout = new QHBoxLayout();
    layout->addWidget(lineEdit);
    layout->addWidget(slider);

    setWindowFlags(Qt::FramelessWindowHint);                                       ///<去除边框
    //this->setAttribute(Qt::WA_TranslucentBackground);                              ///<透明设置
    this->setGeometry(startx,starty,wide,hidth);                     ///<设置程序的起始位置与长宽
    this->setLayout(layout);
    //this->resize(150, 50);
    this->setWindowTitle("QSliderDemo");
}

void MyqSlider::setLineEditValue(int value)
{
    int pos = slider->value();
    QString str = QString("%1").arg(pos);

    lineEdit->setText(str);
}

MyqSlider::~MyqSlider()
{

}

调用方式

    MyqSlider *tempslider1 = new MyqSlider(210,214,180,50,this);
    tempslider1->show();
    MyqSlider *tempslider2 = new MyqSlider(210,244,180,50,this);
    tempslider2->show();
发布了49 篇原创文章 · 获赞 3 · 访问量 5545

猜你喜欢

转载自blog.csdn.net/nb_zsy/article/details/104405034