main.cpp
#include "qslidertest.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSliderTest w;
w.show();
return a.exec();
}
qslider.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_qslidertest.h"
class QSliderTest : public QWidget
{
Q_OBJECT
public:
QSliderTest(QWidget *parent = Q_NULLPTR);
public slots:
void Move(int);
void Change(int);
void Press();
void Rel();
void Click();
private:
Ui::QSliderTestClass ui;
};
qslider.cpp
#include "qslidertest.h"
#include <QDebug>
QSliderTest::QSliderTest(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
//拖动滑块
void QSliderTest::Move(int v)
{
qDebug() << "Move " << v;
}
//点击、拖动滑动条
void QSliderTest::Change(int c)
{
qDebug() << "Change " << c;
}
//点击滑块
void QSliderTest::Press()
{
qDebug() << "Press " << ui.horizontalSlider->value();
}
void QSliderTest::Rel()
{
qDebug() << "Rel " << ui.horizontalSlider->value();
}
void QSliderTest::Click()
{
int v = ui.horizontalSlider->value() + 10;
ui.horizontalSlider->setValue(v);
}
qxslider.h
#pragma once
#include <QSlider>
class qxslider : public QSlider
{
public:
qxslider(QWidget *p = NULL);
~qxslider();
virtual void mousePressEvent(QMouseEvent* e);
};
qxslider.cpp
#include "qxslider.h"
#include <QMouseEvent>
#include <QDebug>
qxslider::qxslider(QWidget* p ):QSlider(p) //继承父类
{
}
qxslider::~qxslider()
{
}
void qxslider::mousePressEvent(QMouseEvent* e)
{
qDebug() << e->pos().x();
QSlider::mousePressEvent(e);
//显示相对位置百分比
double p = (double)e -> pos().x() / (double)width();
int val = p * (maximum() - minimum()) + minimum();
setValue(val);
}
ui_qslidertest.h
/********************************************************************************
** Form generated from reading UI file 'qslidertest.ui'
**
** Created by: Qt User Interface Compiler version 5.14.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_QSLIDERTEST_H
#define UI_QSLIDERTEST_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSlider>
#include <QtWidgets/QWidget>
#include "qxslider.h"
QT_BEGIN_NAMESPACE
class Ui_QSliderTestClass
{
public:
qxslider *horizontalSlider;
QSlider *verticalSlider;
QPushButton *pushButton;
void setupUi(QWidget *QSliderTestClass)
{
if (QSliderTestClass->objectName().isEmpty())
QSliderTestClass->setObjectName(QString::fromUtf8("QSliderTestClass"));
QSliderTestClass->resize(600, 400);
horizontalSlider = new qxslider(QSliderTestClass);
horizontalSlider->setObjectName(QString::fromUtf8("horizontalSlider"));
horizontalSlider->setGeometry(QRect(120, 70, 421, 81));
horizontalSlider->setStyleSheet(QString::fromUtf8("QSlider::groove{\n"
" background-color: rgba(200,83,245,100);\n"
" height:28px;\n"
" border:1px solid rgba(200,83,245,100);\n"
" border-radius:10px;\n"
"}\n"
"QSlider::handle{\n"
" width: 30px;\n"
" background-color: rgba(225,10,0,200);\n"
" border:1px solid rgba(225,10,0,200);\n"
" border-radius:10px;\n"
" margin:-10px 0;\n"
"}\n"
"QSlider::sub-page{\n"
" border:1px solid rgba(200,83,245,100);\n"
" border-radius:10px;\n"
" background-color:rgba(125,13,135,80);\n"
"}\n"
"QSlider::add-page {\n"
" border:1px solid rgba(200,83,245,100);\n"
" border-radius:10px;\n"
" background-color: rgba(0,13,135,80);\n"
"}\n"
"\n"
"\n"
""));
horizontalSlider->setMaximum(999);
horizontalSlider->setSingleStep(50);
horizontalSlider->setPageStep(500);
horizontalSlider->setSliderPosition(50);
horizontalSlider->setOrientation(Qt::Horizontal);
verticalSlider = new QSlider(QSliderTestClass);
verticalSlider->setObjectName(QString::fromUtf8("verticalSlider"));
verticalSlider->setGeometry(QRect(130, 170, 22, 160));
verticalSlider->setOrientation(Qt::Vertical);
pushButton = new QPushButton(QSliderTestClass);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(240, 170, 93, 28));
retranslateUi(QSliderTestClass);
QObject::connect(horizontalSlider, SIGNAL(sliderMoved(int)), QSliderTestClass, SLOT(Move(int)));
QObject::connect(horizontalSlider, SIGNAL(valueChanged(int)), QSliderTestClass, SLOT(Change(int)));
QObject::connect(horizontalSlider, SIGNAL(sliderPressed()), QSliderTestClass, SLOT(Press()));
QObject::connect(horizontalSlider, SIGNAL(sliderReleased()), QSliderTestClass, SLOT(Rel()));
QObject::connect(pushButton, SIGNAL(clicked()), QSliderTestClass, SLOT(Click()));
QMetaObject::connectSlotsByName(QSliderTestClass);
} // setupUi
void retranslateUi(QWidget *QSliderTestClass)
{
QSliderTestClass->setWindowTitle(QCoreApplication::translate("QSliderTestClass", "QSliderTest", nullptr));
pushButton->setText(QCoreApplication::translate("QSliderTestClass", "PushButton", nullptr));
} // retranslateUi
};
namespace Ui {
class QSliderTestClass: public Ui_QSliderTestClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_QSLIDERTEST_H