Qt: 调色板QPalette类用法详解

来源:互联网 发布:2010年网络流行歌曲 编辑:程序博客网 时间:2024/06/06 03:44

可以通过qt designer的中的pallette属性预览效果  


1.1.3刷成什么样QBrush 

画刷QBrush 也是个功能强大的东东,鉴于你可能不怎么使用,你只知道能设置颜色就行。

[cpp] view plain copy
 print?
  1. //注意要先调用setAutoFillBackground  
  2. textEdit->setAutoFillBackground(true);  
  3. QPalette palette;  
  4. //设置QTextEdit文字颜色  
  5. palette.setBrush(QPalette::Active, QPalette::Text, QBrush(Qt::yellow));  
  6. //设置QTextEdit背景色  
  7. palette.setBrush(QPalette::Active, QPalette::Base, QBrush(Qt::red));  
  8. textEdit->setPalette(palette);  

1.2setColor 
void QPalette::setColor ( ColorGroup group, ColorRole role, const QColor & color );
虽然setColor参数和setBrush差不多,只是最后一个参数更简洁,直接设置好颜色就行。


上述两种方式大同小异,除去group这一参数歪,其余两个参数一模一样,其中第二个参数代表颜色角色!

        其主要通过枚举变量QPalette::ColorRole来定义,常用的枚举值有:

    QPalete::Window,通常指窗口部件的背景色;

    QPalette:WindowText,通常指窗口不见的前景色;

    QPalette::Base,指文本输入窗口部件(比如QtextEdit,QLinedit等)的背景色.

    QPalette::Text,与QPalette::Base一块使用,指文本输入窗口部件的前景色;

    QPalette::Button,指按钮窗口部件的背景色;

    QPalette::ButtonText,指按钮窗口部件的前景色.



 调色板QPalette类用法详解(附实例、源码)

标签: Qt调色板QPalette类用法详解实例
 1511人阅读 评论(0) 收藏 举报
 分类:

  • 在实际的应用中,经常需要对某个控件的颜色外观,如背景、前景色等,进行设置。

  • Qt中提供的调色板QPalette类就是专门用于管理控件的外观显示。QPalette类相当于对话框或控件的调色板,管理着控件和窗体的所有颜色。

  • 每个窗体和控件都包含一个QPalette对象,在显示时,对其做相应的设置即可。

QPalette有两个基本的概念:一个是ColorGroup;一个是ColorRole。


ColorGroup有三种不同的状态:

  • Active:激活状态(获得焦点状态)

  • Disabled:禁用状态(未获得焦点状态)

  • Inactive:未激活状态(不可用状态)

通常情况下:在大多数风格,活跃的和不活跃的看起来一样。

原版英文描述如下:

The color groups:

The Active group is used for the window that has keyboard focus.
The Inactive group is used for other windows.
The Disabled group is used for widgets (not windows) that are disabled for some reason.
Both active and inactive windows can contain disabled widgets. (Disabled widgets are often called inaccessible or grayed out.)


ColorRole:


设置控件颜色的方法是先调用QWidget::palette()获取当前面板,修改它为自定义的值后再通过方法QWidget::setPalette设置为新修改的面板。代码如下所示

[cpp] view plain copy
 print?
  1. QPalette palette = widget->palette();    
  2. palette.setColor(QPalette::Window, Qt::lightGray);  //改变控件背景色    
  3. palette.setColor(QPalette::WindowText, Qt::blue);   //改变控件字体颜色    
  4. ...    
  5. widget->setPalette(palette);   

常用的设置颜色方法如下:
(1) void QPalette::setBrush ( ColorRole role, const QBrush & brush )
改变所有组下指定角色role的画刷颜色值。

(2) void QPalette::setBrush ( ColorGroup group, ColorRole role, const QBrush & brush )
改变指定组group下的指定角色role的画刷颜色值。

(3) void QPalette::setColor ( ColorRole role, const QColor & color )
改变所有组下指定角色role的颜色值。

(4) void QPalette::setColor ( ColorGroup group, ColorRole role, const QColor & color )
改变指定组group下指定角色role的颜色值。


注意:在以上代码之前,必须先调用函数 setAutoFillBackground(true),设置窗体运行自动填充。


实例:

内容:利用QPalette改变控件颜色

效果如下:



步骤:

1、新建Qt GUI应用,项目名称为“Palette” (可以自定义,不过尽量见名知义),基类选择“QDialog”,类名为“Palette”,取消创建界面按钮。

一直单击下一步,完成创建工程。项目工程结构如下:



2、打开“Palette.h”文件,添加如下代码:

[cpp] view plain copy
 print?
  1. #ifndef PALETTE_H  
  2. #define PALETTE_H  
  3.   
  4. #include <QDialog>  
  5. #include <QComboBox>  
  6. #include <QLabel>  
  7. #include <QTextEdit>  
  8. #include <QPushButton>  
  9. #include <QLineEdit>  
  10.   
  11. class Palette : public QDialog  
  12. {  
  13.     Q_OBJECT  
  14.   
  15. public:  
  16.     Palette(QWidget *parent = 0);  
  17.     ~Palette();  
  18.   
  19.     void createCtrlFrame();  
  20.     void createContentFrame();  
  21.     void fillColorList(QComboBox *);  
  22.   
  23. private slots:  
  24.     void showWindow();  
  25.     void showWindowText();  
  26.     void showButton();  
  27.     void showButtonText();  
  28.     void showBase();  
  29.   
  30. private:  
  31.     QFrame *ctrlFrame;  
  32.     QLabel *windowLabel;  
  33.     QComboBox *windowComboBox;  
  34.     QLabel *windowTextLabel;  
  35.     QComboBox *windowTextComboBox;  
  36.     QLabel *buttonLabel;  
  37.     QComboBox *buttonComboBox;  
  38.     QLabel *buttonTextLabel;  
  39.     QComboBox *buttonTextComboBox;  
  40.     QLabel *baseLabel;  
  41.     QComboBox *baseComboBox;  
  42.     QFrame *contentFrame;  
  43.     QLabel *label1;  
  44.     QComboBox *comboBox1;  
  45.     QLabel *label2;  
  46.     QLineEdit *lineEdit2;  
  47.     QTextEdit *textEdit;  
  48.     QPushButton *okBtn;  
  49.     QPushButton *cancelBtn;  
  50. };  
  51.   
  52. #endif // PALETTE_H  

3、打开“Palette.cpp”文件,添加如下代码:

[cpp] view plain copy
 print?
  1. #include "palette.h"  
  2. #include <QBoxLayout>  
  3. Palette::Palette(QWidget *parent)  
  4.     : QDialog(parent)  
  5. {  
  6.     createCtrlFrame();  
  7.     createContentFrame();  
  8.     QHBoxLayout *mainLayout = new QHBoxLayout(this);  
  9.     mainLayout->addWidget(ctrlFrame);  
  10.     mainLayout->addWidget(contentFrame);  
  11. }  
  12.   
  13. void Palette::createCtrlFrame()  
  14. {  
  15.     ctrlFrame = new QFrame; //窗口背景色  
  16.     windowLabel = new QLabel(tr("QPalette::window: "));  
  17.     windowComboBox = new QComboBox;  
  18.     fillColorList(windowComboBox);  
  19.     connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(showWindow()));  
  20.   
  21.     windowTextLabel = new QLabel(tr("QPalette::WindowText: ")); //窗口前景色  
  22.     windowTextComboBox = new QComboBox;  
  23.     fillColorList(windowTextComboBox);  
  24.     connect(windowTextComboBox,SIGNAL(activated(int)),this,SLOT(showWindowText()));  
  25.   
  26.     buttonLabel = new QLabel(tr("QPalette::Button: ")); //窗口按钮的颜色  
  27.     buttonComboBox = new QComboBox;  
  28.     fillColorList(buttonComboBox);  
  29.     connect(buttonComboBox,SIGNAL(activated(int)),this,SLOT(showButton()));  
  30.   
  31.     buttonTextLabel = new QLabel(tr("QPalette::ButtonText: ")); //窗口按钮上面文本的颜色  
  32.     buttonTextComboBox = new QComboBox;  
  33.     fillColorList(buttonTextComboBox);  
  34.     connect(buttonTextComboBox,SIGNAL(activated(int)),this,SLOT(showButtonText()));  
  35.   
  36.     baseLabel = new QLabel(tr("QPalette::Base: "));  
  37.     baseComboBox = new QComboBox;  
  38.     fillColorList(baseComboBox);  
  39.     connect(baseComboBox,SIGNAL(activated(int)),this,SLOT(showBase()));  
  40.   
  41.     QGridLayout *mainLayout = new QGridLayout(ctrlFrame);  
  42.     mainLayout->setSpacing(20);  
  43.     mainLayout->addWidget(windowLabel,0,0);  
  44.     mainLayout->addWidget(windowComboBox,0,1);  
  45.   
  46.     mainLayout->addWidget(windowTextLabel,1,0);  
  47.     mainLayout->addWidget(windowTextComboBox,1,1);  
  48.   
  49.     mainLayout->addWidget(buttonLabel,2,0);  
  50.     mainLayout->addWidget(buttonComboBox,2,1);  
  51.   
  52.     mainLayout->addWidget(buttonTextLabel,3,0);  
  53.     mainLayout->addWidget(buttonTextComboBox,3,1);  
  54.   
  55.     mainLayout->addWidget(baseLabel,4,0);  
  56.     mainLayout->addWidget(baseComboBox,4,1);  
  57. }  
  58.   
  59. void Palette::createContentFrame()  
  60. {  
  61.     contentFrame = new QFrame;  
  62.     label1 = new QLabel(tr("请选择一个值:"));  
  63.     comboBox1 = new QComboBox;  
  64.   
  65.     label2 = new QLabel(tr("请输入字符串: "));  
  66.     lineEdit2 = new QLineEdit;  
  67.   
  68.     textEdit = new QTextEdit;  
  69.   
  70.     QGridLayout *topLayout = new QGridLayout;  
  71.     topLayout->addWidget(label1,0,0);  
  72.     topLayout->addWidget(comboBox1,0,1);  
  73.     topLayout->addWidget(label2,1,0);  
  74.     topLayout->addWidget(lineEdit2,1,1);  
  75.     topLayout->addWidget(textEdit,2,0,1,2);  
  76.   
  77.     okBtn = new QPushButton(tr("确认"));  
  78.     cancelBtn = new QPushButton(tr("取消"));  
  79.   
  80.     QHBoxLayout *bottomLayout = new QHBoxLayout;  
  81.     bottomLayout->addStretch(1);  
  82.     bottomLayout->addWidget(okBtn);  
  83.     bottomLayout->addWidget(cancelBtn);  
  84.   
  85.     QVBoxLayout *mainlayout = new QVBoxLayout(contentFrame);  
  86.     mainlayout->addLayout(topLayout);  
  87.     mainlayout->addLayout(bottomLayout);  
  88.   
  89.     okBtn->setAutoFillBackground(true);     //允许自动填充  
  90.     cancelBtn->setAutoFillBackground(true);  
  91.     contentFrame->setAutoFillBackground(true);  
  92. }  
  93.   
  94. void Palette::showWindow()  //用于控制背景颜色的显示  
  95. {  
  96.     QStringList colorList = QColor::colorNames();  
  97.     QColor color = QColor(colorList[windowComboBox->currentIndex()]);  
  98.   
  99.    // contentFrame->setAutoFillBackground(true);  
  100.   
  101.     QPalette p = contentFrame->palette();  
  102.     p.setColor(QPalette::Window, color);  
  103.     contentFrame->setPalette(p);  
  104.   
  105.     contentFrame->update();  
  106. }  
  107.   
  108. void Palette::showWindowText()  //对窗体的前景色进行设置  
  109. {  
  110.     QStringList colorList = QColor::colorNames();  
  111.     QColor color = colorList[windowTextComboBox->currentIndex()];  
  112.   
  113.     QPalette p = contentFrame->palette();  
  114.     p.setColor(QPalette::WindowText, color);  
  115.     contentFrame->setPalette(p);  
  116. }  
  117.   
  118. void Palette::showButtonText()  
  119. {  
  120.     QStringList colorList = QColor::colorNames();  
  121.     QColor color = QColor(colorList[buttonTextComboBox->currentIndex()]);  
  122.   
  123.     QPalette p = contentFrame->palette();  
  124.     p.setColor(QPalette::ButtonText , color);  
  125.     contentFrame->setPalette(p);  
  126. }  
  127.   
  128. void Palette::showBase()  
  129. {  
  130.     QStringList colorList = QColor::colorNames();  
  131.     QColor color = QColor(colorList[baseComboBox->currentIndex()]);  
  132.   
  133.     QPalette p = contentFrame->palette();  
  134.     p.setColor(QPalette::Base , color);  
  135.     contentFrame->setPalette(p);  
  136. }  
  137.   
  138. void Palette::fillColorList(QComboBox *comboBox)  
  139. {  
  140.     QStringList colorList = QColor::colorNames();  
  141.     QString color;  
  142.   
  143.     foreach (color, colorList)  
  144.     {  
  145.         QPixmap pix(QSize(70,20));  
  146.         pix.fill(QColor(color));  
  147.         comboBox->addItem(QIcon(pix), NULL);  
  148.         comboBox->setIconSize(QSize(70,20));  
  149.         comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);  
  150.     }  
  151. }  
  152.   
  153.   
  154. void Palette::showButton()  
  155. {  
  156.     QStringList colorList = QColor::colorNames();  
  157.     QColor color = QColor(colorList[buttonComboBox->currentIndex()]);  
  158.   
  159.     //contentFrame->setAutoFillBackground(true);  
  160.   
  161.     QPalette p = contentFrame->palette();  
  162.     p.setColor(QPalette::Button , color);  
  163.     contentFrame->setPalette(p);  
  164.   
  165.     contentFrame->update();  
  166.   
  167. }  
  168.   
  169. Palette::~Palette()  
  170. {  
  171.   
  172. }  


4、运行项目,效果如前所示。