#include "ColorSelWidget.h"
ColorSelWidget::ColorSelWidget(QWidget *parent) : QWidget(parent)
{
// 初始化界面
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
this->setFixedSize(730, 420);
this->setFocusPolicy(Qt::ClickFocus);
this->setObjectName("ColorSelWidget");
this->setStyleSheet("QWidget#ColorSelWidget{background: rgb(23,25,29);}");
// 标题栏窗口部件
m_pLabTitle = new QLabel;
m_pBtnClose = new QPushButton;
m_pLabSplitLine = new QLabel;
m_pLabTitle->setText("编辑颜色");
m_pLabTitle->setObjectName("LabTitle");
m_pBtnClose->setObjectName("m_pCloseBtn");
m_pLabSplitLine->setObjectName("LabSplitLine");
m_pBtnClose->setFixedSize(30, 30);
m_pLabSplitLine->setFixedSize(width(), 2);
// 初始化基本颜色界面
m_pBasicColorArea = new BasicColorArea;
// 自定义颜色区域
m_pCustomColorArea = new CustomColorArea;
// 初始化饱和度(S)与亮度(V)区域
m_pSVColorArea = new SVColorArea;
// 初始化色调区域
m_pHColorArea = new HColorArea;
// 初始化预览颜色界面
m_pPreviewColorArea = new PreviewColorArea;
m_pLabCurColor = new QLabel;
m_pLabNewColor = new QLabel;
m_pBtnOk = new QPushButton;
m_pBtnCancle = new QPushButton;
m_pLabCurColor->setText("当前\n颜色");
m_pLabNewColor->setText("新的\n颜色");
m_pBtnOk->setText("确定");
m_pBtnCancle->setText("取消");
m_pLabCurColor->setFixedSize(30, 40);
m_pLabNewColor->setFixedSize(30, 40);
m_pBtnOk->setFixedSize(62, 26);
m_pBtnCancle->setFixedSize(62, 26);
// 初始化GroupBox
m_pGroupBoxBasic = new QGroupBox;
m_pGroupBoxCustom = new QGroupBox;
m_pGroupBoxBasic->setTitle("基本颜色");
m_pGroupBoxCustom->setTitle("自定义颜色");
// 色调、饱和度、亮度 - 标签、计数器控件
m_pLabH = new QLabel;
m_pSpinBoxH = new QSpinBox;
m_pLabS = new QLabel;
m_pSpinBoxS = new QSpinBox;
m_pLabV = new QLabel;
m_pSpinBoxV = new QSpinBox;
m_pLabR = new QLabel;
m_pSpinBoxR = new QSpinBox;
m_pLabG = new QLabel;
m_pSpinBoxG = new QSpinBox;
m_pLabB = new QLabel;
m_pSpinBoxB = new QSpinBox;
m_pLabH->setText("色调(H):");
m_pLabS->setText("饱和度(S):");
m_pLabV->setText("亮度(V):");
m_pLabR->setText("红(R):");
m_pLabG->setText("绿(G):");
m_pLabB->setText("蓝(B):");
m_pLabH->setFixedSize(66, 26);
m_pLabS->setFixedSize(66, 26);
m_pLabV->setFixedSize(66, 26);
m_pLabR->setFixedSize(50, 26);
m_pLabG->setFixedSize(50, 26);
m_pLabB->setFixedSize(50, 26);
m_pLabColor = new QLabel; // 颜色文本标签
m_pEditColor = new QLineEdit; // 颜色编辑框
m_pBtnCustom = new QPushButton;
m_pLabColor->setText("#");
m_pEditColor->setText("000000");
m_pBtnCustom->setText("添加到自定义颜色");
m_pBtnCustom->setFixedSize(128, 26);
m_pEditColor->setFixedSize(72, 26);
// 正则表达式应用在颜色编辑栏
QRegExp rx("(\\d?[a-f]?){0,6}");
m_pEditColor->setValidator(new QRegExpValidator(rx, this));
m_pEditColor->setText("000000");
// 标题栏布局
QHBoxLayout *pLayoutTitle = new QHBoxLayout;
pLayoutTitle->addSpacing(6);
pLayoutTitle->addWidget(m_pLabTitle);
pLayoutTitle->addStretch();
pLayoutTitle->addWidget(m_pBtnClose, 0, Qt::AlignTop);
pLayoutTitle->addSpacing(4);
pLayoutTitle->setMargin(0);
// 基本颜色布局
QVBoxLayout *pLayoutBasic = new QVBoxLayout(m_pGroupBoxBasic);
pLayoutBasic->addWidget(m_pBasicColorArea);
// 自定义颜色布局
QVBoxLayout *pLayoutCustom = new QVBoxLayout(m_pGroupBoxCustom);
pLayoutCustom->addWidget(m_pCustomColorArea);
// 下方布局
QHBoxLayout *pLayoutDown = new QHBoxLayout;
pLayoutDown->addStretch();
pLayoutDown->addWidget(m_pLabColor);
pLayoutDown->addSpacing(2);
pLayoutDown->addWidget(m_pEditColor);
pLayoutDown->addStretch();
pLayoutDown->addWidget(m_pBtnCustom);
pLayoutDown->addSpacing(0);
pLayoutDown->setSpacing(0);
pLayoutDown->setMargin(0);
// 左侧布局
QVBoxLayout *pLayoutLeft = new QVBoxLayout;
pLayoutLeft->addWidget(m_pGroupBoxBasic);
pLayoutLeft->addSpacing(12);
pLayoutLeft->addWidget(m_pGroupBoxCustom);
pLayoutLeft->addSpacing(12);
pLayoutLeft->addLayout(pLayoutDown);
pLayoutLeft->setSpacing(0);
pLayoutLeft->addStretch();
pLayoutLeft->setMargin(0);
// 网格布局
QGridLayout *m_pGridLayout = new QGridLayout;
m_pGridLayout->addWidget(m_pLabH, 0, 0, Qt::AlignRight);
m_pGridLayout->addWidget(m_pSpinBoxH, 0, 1);
m_pGridLayout->addWidget(m_pLabS, 1, 0, Qt::AlignRight);
m_pGridLayout->addWidget(m_pSpinBoxS, 1, 1);
m_pGridLayout->addWidget(m_pLabV, 2, 0, Qt::AlignRight);
m_pGridLayout->addWidget(m_pSpinBoxV, 2, 1);
m_pGridLayout->addWidget(m_pLabR, 3, 0, Qt::AlignRight);
m_pGridLayout->addWidget(m_pSpinBoxR, 3, 1);
m_pGridLayout->addWidget(m_pLabG, 4, 0, Qt::AlignRight);
m_pGridLayout->addWidget(m_pSpinBoxG,4, 1);
m_pGridLayout->addWidget(m_pLabB, 5, 0, Qt::AlignRight);
m_pGridLayout->addWidget(m_pSpinBoxB, 5, 1);
// 色调布局
QHBoxLayout *pLayoutHue = new QHBoxLayout;
pLayoutHue->addStretch();
pLayoutHue->addWidget(m_pSVColorArea);
pLayoutHue->addSpacing(10);
pLayoutHue->addWidget(m_pHColorArea);
pLayoutHue->addSpacing(10);
pLayoutHue->addLayout(m_pGridLayout);
pLayoutHue->addStretch();
pLayoutHue->setSpacing(0);
pLayoutHue->setMargin(0);
// 颜色预览布局
QHBoxLayout *pLayoutPreview = new QHBoxLayout;
pLayoutPreview->addSpacing(12);
pLayoutPreview->addWidget(m_pLabCurColor);
pLayoutPreview->addSpacing(8);
pLayoutPreview->addWidget(m_pPreviewColorArea);
pLayoutPreview->addSpacing(8);
pLayoutPreview->addWidget(m_pLabNewColor);
pLayoutPreview->addStretch();
pLayoutPreview->addWidget(m_pBtnOk);
pLayoutPreview->addSpacing(10);
pLayoutPreview->addWidget(m_pBtnCancle);
pLayoutPreview->addSpacing(4);
pLayoutPreview->setSpacing(0);
pLayoutPreview->setMargin(0);
// 中间布局
QVBoxLayout *pLayoutCenter = new QVBoxLayout;
pLayoutCenter->addStretch();
pLayoutCenter->addLayout(pLayoutHue);
pLayoutCenter->addLayout(pLayoutPreview);
pLayoutCenter->addStretch();
pLayoutCenter->setMargin(0);
// 布局
QHBoxLayout *pLayoutMainR = new QHBoxLayout;
pLayoutMainR->addSpacing(0);
pLayoutMainR->addLayout(pLayoutLeft);
pLayoutMainR->addSpacing(12);
pLayoutMainR->addLayout(pLayoutCenter);
pLayoutMainR->addSpacing(0);
pLayoutCenter->setSpacing(0);
pLayoutMainR->setContentsMargins(12, 0, 12, 12);
// 主布局
QVBoxLayout *pLayoutMain = new QVBoxLayout(this);
pLayoutMain->addSpacing(0);
pLayoutMain->addLayout(pLayoutTitle);
pLayoutMain->addWidget(m_pLabSplitLine);
pLayoutMain->addLayout(pLayoutMainR);
pLayoutMain->addStretch();
pLayoutMain->setMargin(0);
// 各个界面的信号槽连接
connect(m_pBasicColorArea, &BasicColorArea::sigColorItemSel, this, &ColorSelWidget::slotColorItemSel);
connect(m_pCustomColorArea, &CustomColorArea::sigColorItemSel, this, &ColorSelWidget::slotColorItemSel);
connect(m_pSVColorArea, &SVColorArea::sigSvChanged, m_pPreviewColorArea, &PreviewColorArea::slotSvChanged);
connect(m_pPreviewColorArea, &PreviewColorArea::sigSvChanged, this, &ColorSelWidget::slotUpdateEditData);
connect(m_pHColorArea, &HColorArea::sigHueChanged, m_pSVColorArea, &SVColorArea::slotHueChanged);
// 颜色编辑框改变
connect(m_pEditColor, &QLineEdit::textEdited, this, &ColorSelWidget::slotEditChanged);
connect(m_pEditColor, &QLineEdit::editingFinished, this, &ColorSelWidget::slotEditFinished);
// 各按钮
connect(m_pBtnCustom, &QPushButton::clicke
没有合适的资源?快使用搜索试试~ 我知道了~
使用C++实现 QT实现颜色选择器
共25个文件
cpp:9个
h:8个
png:3个
需积分: 5 1 下载量 31 浏览量
2023-12-13
21:03:39
上传
评论
收藏 23KB ZIP 举报
温馨提示
QT是一个跨平台的C++图形用户界面应用程序开发框架,可以用于创建各种类型的应用程序,包括颜色选择器。 要实现一个颜色选择器,可以使用QT中提供的QColorDialog类。该类提供了一个对话框,允许用户选择颜色。可以通过调用其静态函数getColor()来打开颜色选择对话框并获取所选的颜色。
资源推荐
资源详情
资源评论
收起资源包目录
ColorSelWidget.zip (25个子文件)
ColorSelWidget
MainWidget.h 362B
CustomColorArea.h 724B
ColorSelWidget.pro.user.ec7100c 22KB
BasicColorItem.cpp 2KB
MainWidget.cpp 803B
HColorArea.h 823B
ColorSelWidget.h 3KB
PreviewColorArea.h 668B
style.qss 2KB
PreviewColorArea.cpp 1KB
ColorSelWidget.pro.user 22KB
Resource.qrc 223B
main.cpp 420B
SVColorArea.h 1KB
ColorSelWidget.cpp 19KB
BasicColorItem.h 688B
ColorSelWidget.pro 1KB
BasicColorArea.cpp 2KB
Image
btn_close_pre.png 219B
btn_close_nor.png 219B
btn_close_hov.png 219B
HColorArea.cpp 2KB
CustomColorArea.cpp 1KB
SVColorArea.cpp 3KB
BasicColorArea.h 466B
共 25 条
- 1
资源评论
houxian1103
- 粉丝: 1w+
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功