diff --git a/QtQSliderEx/ExQSlider.cpp b/QtQSliderEx/ExQSlider.cpp new file mode 100644 index 0000000..38ff05d --- /dev/null +++ b/QtQSliderEx/ExQSlider.cpp @@ -0,0 +1,52 @@ +#include "ExQSlider.h" +#include "ui_ExQSlider.h" +#include + +ExQSlider::ExQSlider(QWidget *parent) : + QWidget(parent), + ui(new Ui::ExQSlider) +{ + ui->setupUi(this); + + //设置QSlider的最大值为255 (默认范围为0~100) + ui->sliderRed->setMaximum(255); + ui->sliderGreen->setMaximum(255); + ui->sliderBlue->setMaximum(255); + ui->sliderAlpha->setMaximum(255); + + //初始化QTextEdit的颜色 (否则默认就是白灰色) + QColor color; + color.setRgb(145, 190, 251, 255); + QPalette palette = ui->textColour->palette(); + palette.setColor(QPalette::Base, color); + ui->textColour->setPalette(palette); + + //连接信号与槽,多个信号,对应一个槽函数 + connect(ui->sliderRed, SIGNAL(valueChanged(int)), this, SLOT(onSetClolor(int))); + connect(ui->sliderGreen, SIGNAL(valueChanged(int)), this, SLOT(onSetClolor(int))); + connect(ui->sliderBlue, SIGNAL(valueChanged(int)), this, SLOT(onSetClolor(int))); + connect(ui->sliderAlpha, SIGNAL(valueChanged(int)), this, SLOT(onSetClolor(int))); +} + +ExQSlider::~ExQSlider() +{ + delete ui; +} + +void ExQSlider::onSetClolor(int val) +{ + Q_UNUSED(val) + + int nRed = ui->sliderRed->value(); //获取红绿蓝(RGB)的Slider的数值 + int nGreen = ui->sliderGreen->value(); + int nBlue = ui->sliderBlue->value(); + int nAlpha = ui->sliderAlpha->value(); + + QColor color; + color.setRgb(nRed, nGreen, nBlue, nAlpha); + QPalette palette = ui->textColour->palette(); //获取textColour控件的所有颜色值(调色板) + palette.setColor(QPalette::Base, color); //设置textColou的某一角色(即控件)的颜色 + ui->textColour->setPalette(palette); + + ui->labRgbVal->setText(QString("RGB(%1, %2, %3, %4)").arg(nRed).arg(nGreen).arg(nBlue).arg(nAlpha)); //时刻显示RGBa的具体值 +} diff --git a/QtQSliderEx/ExQSlider.h b/QtQSliderEx/ExQSlider.h new file mode 100644 index 0000000..4d6276a --- /dev/null +++ b/QtQSliderEx/ExQSlider.h @@ -0,0 +1,25 @@ +#ifndef EXQSLIDER_H +#define EXQSLIDER_H + +#include + +namespace Ui { +class ExQSlider; +} + +class ExQSlider : public QWidget +{ + Q_OBJECT + +public: + explicit ExQSlider(QWidget *parent = nullptr); + ~ExQSlider(); + +public slots: + void onSetClolor(int val); + +private: + Ui::ExQSlider *ui; +}; + +#endif // EXQSLIDER_H diff --git a/QtQSliderEx/ExQSlider.ui b/QtQSliderEx/ExQSlider.ui new file mode 100644 index 0000000..34e93d6 --- /dev/null +++ b/QtQSliderEx/ExQSlider.ui @@ -0,0 +1,157 @@ + + + ExQSlider + + + + 0 + 0 + 644 + 284 + + + + ExQSlider + + + + + + + + false + + + + 200 + 0 + + + + + + + + false + + + RGB(145, 190, 251, 255) + + + false + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + 320 + 0 + + + + 255 + + + 145 + + + Qt::Horizontal + + + + + + + 255 + + + 251 + + + Qt::Horizontal + + + + + + + Alpha + + + Qt::AlignCenter + + + + + + + Blue + + + Qt::AlignCenter + + + + + + + Red + + + Qt::AlignCenter + + + + + + + Green + + + Qt::AlignCenter + + + + + + + 255 + + + 190 + + + Qt::Horizontal + + + + + + + 255 + + + 255 + + + Qt::Horizontal + + + + + + + + + + + diff --git a/QtQSliderEx/QtQSliderEx.pro b/QtQSliderEx/QtQSliderEx.pro new file mode 100644 index 0000000..39b50e1 --- /dev/null +++ b/QtQSliderEx/QtQSliderEx.pro @@ -0,0 +1,40 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-08-20T23:59:16 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtQSliderEx +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + ExQSlider.cpp + +HEADERS += \ + ExQSlider.h + +FORMS += \ + ExQSlider.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/QtQSliderEx/main.cpp b/QtQSliderEx/main.cpp new file mode 100644 index 0000000..499545a --- /dev/null +++ b/QtQSliderEx/main.cpp @@ -0,0 +1,11 @@ +#include "ExQSlider.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ExQSlider w; + w.show(); + + return a.exec(); +}