Qt-QPalette-调色板学习

举报
DreamLife 发表于 2022/04/15 00:00:24 2022/04/15
【摘要】   已经很久没有更新博客了,一是因为换了公司,完全是断网开发了,没有时间来写博客,最主要的就是温水煮青蛙,自己在舒适的环境中越来越懒了,最近打算强制自己更新一波。不知道能坚持多久。由于目前没有具体的Qt项目,智能更具书本内容来写博客。最为一种数据搬运和记录。本文内容来之Qt开发及实例第三版   ...

 

已经很久没有更新博客了,一是因为换了公司,完全是断网开发了,没有时间来写博客,最主要的就是温水煮青蛙,自己在舒适的环境中越来越懒了,最近打算强制自己更新一波。不知道能坚持多久。由于目前没有具体的Qt项目,智能更具书本内容来写博客。最为一种数据搬运和记录。本文内容来之Qt开发及实例第三版
 
QPalette类主要有两个基本概念,一个是ColorGroup,另一个是ColorRole。其中ColorGroup指的是一下三种不同的状态
1. QPalette::Active: 获得焦点的状态
2. QPalette::Inactive: 未获得焦点的状态
3. QPalette::Disable:不可用状态
其中Active和Inactive状态在通常情况下,颜色显示是一致的。也可以根据需求设置为不一样的颜色。

 

 

ColorRole指的是颜色的主题,即对窗体中不同部位颜色的分类。例如QPalette::Window是指背景色QPalette::WindowText指的是前景色,等等。
 
下面是运行代码截图
下面是源代码分享
palette.h
 

  
  1. #ifndef PALETTE_H
  2. #define PALETTE_H
  3. #include <QDialog>
  4. #include <QComboBox>
  5. #include <QLabel>
  6. #include <QTextEdit>
  7. #include <QPushButton>
  8. #include <QLineEdit>
  9. class Palette : public QDialog
  10. {
  11. Q_OBJECT
  12. public:
  13. Palette(QWidget *parent = 0);
  14. ~Palette();
  15. void createCtrlFrame(); //完成窗体左半部分颜色选择区的创建
  16. void createContentFrame(); //完成窗体右半部分的创建
  17. void fillColorList(QComboBox *comboBox);
  18. //完成向颜色下拉列表框中插入颜色的工作
  19. private slots:
  20. void ShowWindow();
  21. void ShowWindowText();
  22. void ShowButton();
  23. void ShowButtonText();
  24. void ShowBase();
  25. private:
  26. QFrame *ctrlFrame; //颜色选择面板
  27. QLabel *windowLabel;
  28. QComboBox *windowComboBox;
  29. QLabel *windowTextLabel;
  30. QComboBox *windowTextComboBox;
  31. QLabel *buttonLabel;
  32. QComboBox *buttonComboBox;
  33. QLabel *buttonTextLabel;
  34. QComboBox *buttonTextComboBox;
  35. QLabel *baseLabel;
  36. QComboBox *baseComboBox;
  37. QFrame *contentFrame; //具体显示面板
  38. QLabel *label1;
  39. QComboBox *comboBox1;
  40. QLabel *label2;
  41. QLineEdit *lineEdit2;
  42. QTextEdit *textEdit;
  43. QPushButton *OkBtn;
  44. QPushButton *CancelBtn;
  45. };
  46. #endif // PALETTE_H

 

 

 

 

palette.cpp

 

   
  1. #include "palette.h"
  2. #include <QHBoxLayout>
  3. #include <QGridLayout>
  4. Palette::Palette(QWidget *parent)
  5. : QDialog(parent)
  6. {
  7. createCtrlFrame();
  8. createContentFrame();
  9. QHBoxLayout *mainLayout =new QHBoxLayout(this);
  10. mainLayout->addWidget(ctrlFrame);
  11. mainLayout->addWidget(contentFrame);
  12. }
  13. Palette::~Palette()
  14. {
  15. }
  16. void Palette::createCtrlFrame()
  17. {
  18. ctrlFrame =new QFrame; //颜色选择面板
  19. windowLabel =new QLabel(tr("QPalette::Window: "));
  20. windowComboBox =new QComboBox; //创建一个QComboBox对象
  21. fillColorList(windowComboBox);
  22. connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindow()));
  23. windowTextLabel =new QLabel(tr("QPalette::WindowText: "));
  24. windowTextComboBox =new QComboBox;
  25. fillColorList(windowTextComboBox);
  26. connect(windowTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindowText()));
  27. buttonLabel =new QLabel(tr("QPalette::Button: "));
  28. buttonComboBox =new QComboBox;
  29. fillColorList(buttonComboBox);
  30. connect(buttonComboBox,SIGNAL(activated(int)),this,SLOT(ShowButton()));
  31. buttonTextLabel =new QLabel(tr("QPalette::ButtonText: "));
  32. buttonTextComboBox =new QComboBox;
  33. fillColorList(buttonTextComboBox);
  34. connect(buttonTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowButtonText()));
  35. baseLabel =new QLabel(tr("QPalette::Base: "));
  36. baseComboBox =new QComboBox;
  37. fillColorList(baseComboBox);
  38. connect(baseComboBox,SIGNAL(activated(int)),this,SLOT(ShowBase()));
  39. QGridLayout *mainLayout=new QGridLayout(ctrlFrame);
  40. mainLayout->setSpacing(20);
  41. mainLayout->addWidget(windowLabel,0,0);
  42. mainLayout->addWidget(windowComboBox,0,1);
  43. mainLayout->addWidget(windowTextLabel,1,0);
  44. mainLayout->addWidget(windowTextComboBox,1,1);
  45. mainLayout->addWidget(buttonLabel,2,0);
  46. mainLayout->addWidget(buttonComboBox,2,1);
  47. mainLayout->addWidget(buttonTextLabel,3,0);
  48. mainLayout->addWidget(buttonTextComboBox,3,1);
  49. mainLayout->addWidget(baseLabel,4,0);
  50. mainLayout->addWidget(baseComboBox,4,1);
  51. }
  52. void Palette::createContentFrame()
  53. {
  54. contentFrame =new QFrame; //具体显示面板
  55. label1 =new QLabel(tr("请选择一个值:"));
  56. comboBox1 =new QComboBox;
  57. label2 = new QLabel(tr("请输入字符串: "));
  58. lineEdit2 =new QLineEdit;
  59. textEdit =new QTextEdit;
  60. QGridLayout *TopLayout =new QGridLayout;
  61. TopLayout->addWidget(label1,0,0);
  62. TopLayout->addWidget(comboBox1,0,1);
  63. TopLayout->addWidget(label2,1,0);
  64. TopLayout->addWidget(lineEdit2,1,1);
  65. TopLayout->addWidget(textEdit,2,0,1,2);
  66. OkBtn =new QPushButton(tr("确认"));
  67. CancelBtn =new QPushButton(tr("取消"));
  68. QHBoxLayout *BottomLayout =new QHBoxLayout;
  69. BottomLayout->addStretch(1);
  70. BottomLayout->addWidget(OkBtn);
  71. BottomLayout->addWidget(CancelBtn);
  72. QVBoxLayout *mainLayout =new QVBoxLayout(contentFrame);
  73. mainLayout->addLayout(TopLayout);
  74. mainLayout->addLayout(BottomLayout);
  75. }
  76. void Palette::fillColorList(QComboBox *comboBox)
  77. {
  78. QStringList colorList = QColor::colorNames();
  79. QString color;
  80. foreach(color,colorList)
  81. {
  82. QPixmap pix(QSize(70,20));
  83. pix.fill(QColor(color));
  84. comboBox->addItem(QIcon(pix),NULL);
  85. comboBox->setIconSize(QSize(70,20));
  86. comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
  87. //(f)
  88. }
  89. }
  90. /*
  91. * 用于响应对背景颜色的选择
  92. */
  93. void Palette::ShowWindow()
  94. {
  95. QStringList colorList = QColor::colorNames();
  96. QColor color = QColor(colorList[windowComboBox->currentIndex()]);
  97. QPalette p = contentFrame->palette();
  98. p.setColor(QPalette::Window,color);
  99. contentFrame->setPalette(p);
  100. contentFrame->update();
  101. }
  102. /*
  103. * 用于响应对应文字颜色的选择
  104. */
  105. void Palette::ShowWindowText()
  106. {
  107. QStringList colorList = QColor::colorNames();
  108. QColor color = colorList[windowTextComboBox->currentIndex()];
  109. QPalette p = contentFrame->palette();
  110. p.setColor(QPalette::WindowText,color);
  111. contentFrame->setPalette(p);
  112. }
  113. void Palette::ShowButton()
  114. {
  115. QStringList colorList = QColor::colorNames();
  116. QColor color =QColor(colorList[buttonComboBox->currentIndex()]);
  117. QPalette p = contentFrame->palette();
  118. p.setColor(QPalette::Button,color);
  119. contentFrame->setPalette(p);
  120. contentFrame->update();
  121. }
  122. void Palette::ShowButtonText()
  123. {
  124. QStringList colorList = QColor::colorNames();
  125. QColor color = QColor(colorList[buttonTextComboBox->currentIndex()]);
  126. QPalette p =contentFrame->palette();
  127. p.setColor(QPalette::ButtonText,color);
  128. contentFrame->setPalette(p);
  129. }
  130. void Palette::ShowBase()
  131. {
  132. QStringList colorList = QColor::colorNames();
  133. QColor color = QColor(colorList[baseComboBox->currentIndex()]);
  134. QPalette p = contentFrame->palette();
  135. p.setColor(QPalette::Base,color);
  136. contentFrame->setPalette(p);
  137. }

 

 

 

 

文章来源: dreamlife.blog.csdn.net,作者:DreamLife.,版权归原作者所有,如需转载,请联系作者。

原文链接:dreamlife.blog.csdn.net/article/details/79381247

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200