QPalette类详细使用方法

QPalette( [ˈpælət] 调色板)类相当于对话框或控件的调色板,它管理着控件或窗体的所有颜色信息,每个窗体或控件都包含一个QPalette对象,在显示时按照它的QPalette对象中对各部分各状态下的颜色的描述来进行绘制。

QPalette类有两个基本的概念,一个是ColorGroup,另一个是ColorRole。

void QPalette::setColor ( ColorRole role, const QColor & color );

void QPalette::setColor ( ColorGroup group, ColorRole role, const QColor & color );

void QPalette::setBrush ( ColorRole role, const QBrush & brush );

void QPalette::setBrush ( ColorGroup group, ColorRole role, const QBrush & brush );

ColorGroup:

QPalette::Disabled不可用状态
QPalette::Active活跃状态(获得焦点)
QPalette::Inactive不活跃状态(未获得焦点)

ColorRole:

QPalette::Window一个常规的背景颜色
QPalette::Background这个值是废弃的,使用window代替
QPalette::WindowText一个一般的前景颜色
QPalette::Foreground这个值是废弃的,使用windowText代替.
QPalette::Base最长使用来作为text背景颜色为整个widget,但是也能被用来为其他的绘画,像combobox的上下清单的背景和工具栏句柄。它通常是白的或者其他亮的颜色.
QPalette::AlternateBase被用来作为轮流的背景颜色,轮流的行颜色
QPalette::ToolTipBase被用来作为背景颜色为QToolTip和QWhatsThis。工具尖端使用QPalette不活跃的颜色组,因为工具尖端不是活跃的窗口.
QPalette::ToolTipText被用来作为前景颜色为QToolTip和QWhatsThis.工具尖端使用QPalette不活跃的颜色组,因为工具尖端不是活跃的窗口.
QPalette::Text前景颜色使用base.这通常和windowText相同,它一定提供好的对比window和base
QPalette::Buttonbutton背景颜色。这个背景颜色能是不同于window作为一些风格,要求一个不同的背景颜色作为button
QPalette::ButtonText一个前景颜色被用来作为button颜色.
QPalette::BrightText一个text颜色是很不同于windowText,很好的对比与dark。典型的被用来为text,需要被画,在text或者windowText将给差的对比,就像在按下的button。注意text颜色能被用来为事情,而不只是单词;text颜色通常被用来为text,但是他是相当普通的使用text颜色角色为行,图标,等等。

另外,在设置对话框和控件的背景色时还会用到:

void setAutoFillBackground ( bool enabled );

 
  1. .h文件

  2.  
  3. #include <QtWidgets/QDialog>

  4. #include<QComboBox>

  5. #include<QFrame>

  6. #include "ui_Palette.h"

  7.  
  8. class Palette : public QDialog

  9. {

  10. Q_OBJECT

  11.  
  12. public:

  13. Palette(QWidget *parent = Q_NULLPTR);

  14. void createSelectFrame();

  15. void createContentFrame();

  16. void fillColorList(QComboBox *);

  17. public slots:

  18. void sl_window();

  19. void sl_windowText();

  20. void sl_button();

  21. void sl_buttonText();

  22. void sl_base();

  23. private:

  24.  
  25. QFrame *selectFrame; //颜色选择面板

  26. QFrame *contentFrame; //具体显示面板

  27. QComboBox *cbbWindow;

  28. QComboBox *cbbWindowText;

  29. QComboBox *cbbButton;

  30. QComboBox *cbbButtonText;

  31. QComboBox * cbbBase;

  32. };

  33.  
  34.  
 
  1. .cpp文件

  2. #include "Palette.h"

  3. #include <QPalette>

  4. #include <QHBoxLayout>

  5. #include <QVBoxLayout>

  6. #include <QGridLayout>

  7. #include <QStringList>

  8. #include <QColor>

  9. #include <QPixmap>

  10. #include <QSpinBox>

  11. #include <QTextEdit>

  12. #include <QLineEdit>

  13. #include <QPushButton>

  14. #include <QLabel>

  15. #pragma execution_character_set("utf-8")

  16. Palette::Palette(QWidget *parent)

  17. : QDialog(parent)

  18. {

  19.  
  20. createSelectFrame();

  21. createContentFrame();

  22. QHBoxLayout *mainLayout = new QHBoxLayout(this);

  23. mainLayout->addWidget(selectFrame);

  24. mainLayout->addWidget(contentFrame);

  25. mainLayout->setMargin(10);

  26. mainLayout->setSpacing(5);

  27. //固定窗口的大小,且根据内部控件所占用的位置自动调节大小

  28. mainLayout->setSizeConstraint(QLayout::SetFixedSize);

  29.  
  30. }

  31. void Palette::createSelectFrame()

  32. {

  33. selectFrame = new QFrame;

  34.  
  35. QLabel *labWindow = new QLabel(tr("QPalette::Window:"));

  36. QLabel *labWindowText = new QLabel(tr("QPalette::WindowText:"));

  37. QLabel *labButton = new QLabel(tr("QPalette::Button:"));

  38. QLabel *labButtonText = new QLabel(tr("QPalette::ButtonText:"));

  39. QLabel *labBase = new QLabel(tr("QPalette::Base:"));

  40.  
  41. //组合框

  42. cbbWindow = new QComboBox;

  43. fillColorList(cbbWindow);

  44. connect(cbbWindow, SIGNAL(activated(int)), this, SLOT(sl_window()));

  45. cbbWindowText = new QComboBox;

  46. fillColorList(cbbWindowText);

  47. connect(cbbWindowText, SIGNAL(activated(int)), this, SLOT(sl_windowText()));

  48. cbbButton = new QComboBox;

  49. fillColorList(cbbButton);

  50. connect(cbbButton, SIGNAL(activated(int)), this, SLOT(sl_button()));

  51. cbbButtonText = new QComboBox;

  52. fillColorList(cbbButtonText);

  53. connect(cbbButtonText, SIGNAL(activated(int)), this, SLOT(sl_buttonText()));

  54. cbbBase = new QComboBox;

  55. fillColorList(cbbBase);

  56. connect(cbbBase, SIGNAL(activated(int)), this, SLOT(sl_base()));

  57.  
  58. QGridLayout *ctrlLayout = new QGridLayout(selectFrame);

  59. ctrlLayout->addWidget(labWindow, 0, 0);

  60. ctrlLayout->addWidget(labWindowText, 1, 0);

  61. ctrlLayout->addWidget(labButton, 2, 0);

  62. ctrlLayout->addWidget(labButtonText, 3, 0);

  63. ctrlLayout->addWidget(labBase, 4, 0);

  64. ctrlLayout->addWidget(cbbWindow, 0, 1);

  65. ctrlLayout->addWidget(cbbWindowText, 1, 1);

  66. ctrlLayout->addWidget(cbbButton, 2, 1);

  67. ctrlLayout->addWidget(cbbButtonText, 3, 1);

  68. ctrlLayout->addWidget(cbbBase, 4, 1);

  69.  
  70. ctrlLayout->setMargin(5); //设置边距

  71. ctrlLayout->setSpacing(5); //间距

  72.  
  73. }

  74. void Palette::fillColorList(QComboBox *cbb)

  75. {

  76. QStringList colorNameList = QColor::colorNames();

  77.  
  78. QString colorName;

  79. foreach(colorName, colorNameList)

  80. {

  81. QPixmap pix_color(70, 20);

  82. pix_color.fill(QColor(colorName));

  83.  
  84. cbb->addItem(QIcon(pix_color), NULL);

  85. cbb->setIconSize(QSize(70, 20));

  86. cbb->setSizeAdjustPolicy(QComboBox::AdjustToContents); //设置下拉列表的尺寸符合内容的大小

  87. }

  88. }

  89. void Palette::createContentFrame()

  90. {

  91. contentFrame = new QFrame;

  92. QLabel *labValue = new QLabel("Please select one of the values");

  93. QSpinBox *spbValue = new QSpinBox;

  94.  
  95. QHBoxLayout *valueLayout =new QHBoxLayout;

  96. valueLayout->addWidget(labValue);

  97. valueLayout->addWidget(spbValue);

  98. valueLayout->setSpacing(5);

  99.  
  100. QLabel *labString = new QLabel("please input a string");

  101. QLineEdit *edtString = new QLineEdit;

  102. QHBoxLayout *stringWidget = new QHBoxLayout;

  103. stringWidget->addWidget(labString);

  104. stringWidget->addWidget(edtString);

  105. stringWidget->setSpacing(5);

  106.  
  107. QTextEdit *edtHello = new QTextEdit("hello Qt");

  108.  
  109. QPushButton *btnOk = new QPushButton("OK");

  110. QPushButton *btnCancel = new QPushButton("Cancel");

  111.  
  112. QHBoxLayout *buttonLayout = new QHBoxLayout;

  113. buttonLayout->addStretch(1); //添加一个填充

  114. buttonLayout->addWidget(btnOk);

  115. buttonLayout->addWidget(btnCancel);

  116. buttonLayout->setSpacing(5);

  117.  
  118. QVBoxLayout *contentLayout = new QVBoxLayout(contentFrame);

  119. contentLayout->addLayout(valueLayout);

  120. contentLayout->addLayout(stringWidget);

  121. contentLayout->addWidget(edtHello);

  122. contentLayout->addLayout(buttonLayout);

  123.  
  124. btnOk->setAutoFillBackground(true);

  125. btnCancel->setAutoFillBackground(true);

  126. contentFrame->setAutoFillBackground(true);

  127. }

  128.  
  129. void Palette::sl_window()

  130. {

  131. QStringList colorList = QColor::colorNames();

  132. QColor color = QColor(colorList[cbbWindow->currentIndex()]);

  133. //获取当前调色板

  134. QPalette p = contentFrame->palette();

  135. p.setColor(QPalette::Window,color);

  136. contentFrame->setPalette(p);

  137. }

  138. void Palette::sl_windowText()

  139. {

  140. QStringList colorList = QColor::colorNames();

  141. QColor color = QColor(colorList[cbbWindowText->currentIndex()]);

  142. //获取当前调色板

  143. QPalette p = contentFrame->palette();

  144. p.setColor(QPalette::WindowText, color);

  145. contentFrame->setPalette(p);

  146. selectFrame->setPalette(p);

  147.  
  148. }

  149. void Palette::sl_button()

  150. {

  151. QStringList colorList = QColor::colorNames();

  152. QColor color = QColor(colorList[cbbButton->currentIndex()]);

  153. //获取当前调色板

  154. QPalette p = contentFrame->palette();

  155. p.setColor(QPalette::Button, color);

  156. contentFrame->setPalette(p);

  157.  
  158. }

  159. void Palette::sl_buttonText()

  160. {

  161. QStringList colorList = QColor::colorNames();

  162. QColor color = QColor(colorList[cbbButtonText->currentIndex()]);

  163. //获取当前调色板

  164. QPalette p = contentFrame->palette();

  165. p.setColor(QPalette::ButtonText, color);

  166. contentFrame->setPalette(p);

  167.  
  168.  
  169. }

  170. void Palette::sl_base()

  171. {

  172. QStringList colorList = QColor::colorNames();

  173. QColor color = QColor(colorList[cbbBase->currentIndex()]);

  174. //获取当前调色板

  175. QPalette p = contentFrame->palette();

  176. p.setColor(QPalette::Base, color);

  177. contentFrame->setPalette(p);

  178. }

转自:https://blog.csdn.net/m0_37806112/article/details/79899675

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值