QtQSliderEx

Update QtQSliderEx
This commit is contained in:
touwoyimuli@gmail.com 2019-08-21 01:09:34 +08:00
parent 5b853efa3c
commit 2a24f02b90
5 changed files with 285 additions and 0 deletions

52
QtQSliderEx/ExQSlider.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "ExQSlider.h"
#include "ui_ExQSlider.h"
#include <QPalette>
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的具体值
}

25
QtQSliderEx/ExQSlider.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef EXQSLIDER_H
#define EXQSLIDER_H
#include <QWidget>
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

157
QtQSliderEx/ExQSlider.ui Normal file
View File

@ -0,0 +1,157 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ExQSlider</class>
<widget class="QWidget" name="ExQSlider">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>644</width>
<height>284</height>
</rect>
</property>
<property name="windowTitle">
<string>ExQSlider</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="1">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextEdit" name="textColour">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labRgbVal">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>RGB(145, 190, 251, 255)</string>
</property>
<property name="scaledContents">
<bool>false</bool>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QSlider" name="sliderRed">
<property name="minimumSize">
<size>
<width>320</width>
<height>0</height>
</size>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="value">
<number>145</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSlider" name="sliderBlue">
<property name="maximum">
<number>255</number>
</property>
<property name="value">
<number>251</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="labAlpha">
<property name="text">
<string>Alpha</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="labBlue">
<property name="text">
<string>Blue</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="labRed">
<property name="text">
<string>Red</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labGreen">
<property name="text">
<string>Green</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSlider" name="sliderGreen">
<property name="maximum">
<number>255</number>
</property>
<property name="value">
<number>190</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QSlider" name="sliderAlpha">
<property name="maximum">
<number>255</number>
</property>
<property name="value">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

View File

@ -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

11
QtQSliderEx/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "ExQSlider.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ExQSlider w;
w.show();
return a.exec();
}