QT对话框设计——利用QPalette改变控件颜色

转载地址:http://www.cnblogs.com/hanzhaoxin/archive/2012/11/18/2775918.html

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

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

void QPalette::setColor ( ColorRole role, const QColor & color );
void QPalette::setColor ( ColorGroup groupColorRole role, const QColor & color );
void QPalette::setBrush ( ColorRole role, const QBrush & brush );
void QPalette::setBrush ( ColorGroup groupColorRole 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::Button button背景颜色。这个背景颜色能是不同于window作为一些风格,要求一个不同的背景颜色作为button
QPalette::ButtonText 一个前景颜色被用来作为button颜色.
QPalette::BrightText 一个text颜色是很不同于windowText,很好的对比与dark。典型的被用来为text,需要被画,在text或者windowText将给差的对比,就像在按下的button。注意text颜色能被用来为事情,而不只是单词;text颜色通常被用来为text,但是他是相当普通的使用text颜色角色为行,图标,等等。

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

void setAutoFillBackground ( bool enabled );


image

maindlg.h

  1. #ifndef MAINDLG_H  
  2. #define MAINDLG_H  
  3.   
  4. #include <QtGui/QDialog>  
  5. #include <QFrame>  
  6. #include <QComboBox>  
  7.   
  8.   
  9. class MainDlg : public QDialog  
  10. {  
  11.     Q_OBJECT  
  12.   
  13. public:  
  14.     MainDlg(QWidget *parent = 0, Qt::WFlags flags = 0);  
  15.     ~MainDlg();  
  16.   
  17.     void createCtrlFrame();  
  18.     void createContentFrame();  
  19.     void fillColorList(QComboBox *);  
  20.   
  21. public slots:  
  22.     void sl_window();  
  23.     void sl_windowText();  
  24.     void sl_button();  
  25.     void sl_buttonText();  
  26.     void sl_base();  
  27.   
  28. private:  
  29.     QFrame *ctrlFrame;  //颜色选择面板  
  30.     QFrame *contentFrame;   //具体显示面板  
  31.     QComboBox *cbbWindow;  
  32.     QComboBox *cbbWindowText;  
  33.     QComboBox *cbbButton;  
  34.     QComboBox *cbbButtonText;  
  35.     QComboBox * cbbBase;  
  36.       
  37. };  
  38.   
  39. #endif // MAINDLG_H  

maindlg.cpp

  1. #include "maindlg.h"  
  2. #include <QPalette>  
  3. #include <QHBoxLayout>  
  4. #include <QVBoxLayout>  
  5. #include <QGridLayout>  
  6. #include <QStringList>  
  7. #include <QColor>  
  8. #include <QPixmap>  
  9. #include <QSpinBox>  
  10. #include <QTextEdit>  
  11. #include <QLineEdit>  
  12. #include <QPushButton>  
  13. #include <QLabel>  
  14.   
  15. MainDlg::MainDlg(QWidget *parent, Qt::WFlags flags)  
  16.     : QDialog(parent, flags)  
  17. {  
  18.     createCtrlFrame();  
  19.     createContentFrame();  
  20.   
  21.     QHBoxLayout *mainLayout = new QHBoxLayout(this);  
  22.     mainLayout->addWidget(ctrlFrame);  
  23.     mainLayout->addWidget(contentFrame);  
  24.     mainLayout->setMargin(10);  
  25.     mainLayout->setSpacing(5);  
  26.     mainLayout->setSizeConstraint(QLayout::SetFixedSize);  
  27. }  
  28.   
  29. MainDlg::~MainDlg()  
  30. {  
  31.   
  32. }  
  33.   
  34. void MainDlg::fillColorList(QComboBox *cbb)  
  35. {  
  36.     QStringList colorNameList = QColor::colorNames();  
  37.   
  38.     QString colorName;  
  39.     foreach(colorName,colorNameList)  
  40.     {  
  41.         QPixmap pix_color(70,20);  
  42.         pix_color.fill(QColor(colorName));  
  43.   
  44.         cbb->addItem(QIcon(pix_color),NULL);  
  45.         cbb->setIconSize(QSize(70,20));  
  46.         cbb->setSizeAdjustPolicy(QComboBox::AdjustToContents);   //设置下拉列表的尺寸符合内容的大小  
  47.     }  
  48. }  
  49.   
  50. void MainDlg::createCtrlFrame()  
  51. {  
  52.     ctrlFrame = new QFrame;  
  53.   
  54.     QLabel *labWindow = new QLabel(tr("QPalette::Window:"));  
  55.     QLabel *labWindowText = new QLabel(tr("QPalette::WindowText:"));  
  56.     QLabel *labButton = new QLabel(tr("QPalette::Button:"));  
  57.     QLabel *labButtonText = new QLabel(tr("QPalette::ButtonText:"));  
  58.     QLabel *labBase = new QLabel(tr("QPalette::Base:"));  
  59.   
  60.     cbbWindow = new QComboBox;  
  61.     fillColorList(cbbWindow);  
  62.     connect(cbbWindow,SIGNAL(activated(int)),this,SLOT(sl_window()));  
  63.     cbbWindowText = new QComboBox;  
  64.     fillColorList(cbbWindowText);  
  65.     connect(cbbWindowText,SIGNAL(activated(int)),this,SLOT(sl_windowText()));  
  66.     cbbButton = new QComboBox;  
  67.     fillColorList(cbbButton);  
  68.     connect(cbbButton,SIGNAL(activated(int)),this,SLOT(sl_button()));  
  69.     cbbButtonText = new QComboBox;  
  70.     fillColorList(cbbButtonText);  
  71.     connect(cbbButtonText,SIGNAL(activated(int)),this,SLOT(sl_buttonText()));  
  72.     cbbBase = new QComboBox;  
  73.     fillColorList(cbbBase);  
  74.     connect(cbbBase,SIGNAL(activated(int)),this,SLOT(sl_base()));  
  75.       
  76.     int col_lab = 0;  
  77.     int col_cbb = 1;  
  78.     QGridLayout *ctrlLayout = new QGridLayout(ctrlFrame);  
  79.     ctrlLayout->addWidget(labWindow,0,col_lab);  
  80.     ctrlLayout->addWidget(labWindowText,1,col_lab);  
  81.     ctrlLayout->addWidget(labButton,2,col_lab);  
  82.     ctrlLayout->addWidget(labButtonText,3,col_lab);  
  83.     ctrlLayout->addWidget(labBase,4,col_lab);  
  84.     ctrlLayout->addWidget(cbbWindow,0,col_cbb);  
  85.     ctrlLayout->addWidget(cbbWindowText,1,col_cbb);  
  86.     ctrlLayout->addWidget(cbbButton,2,col_cbb);  
  87.     ctrlLayout->addWidget(cbbButtonText,3,col_cbb);  
  88.     ctrlLayout->addWidget(cbbBase,4,col_cbb);  
  89.     ctrlLayout->setMargin(5);  
  90.     ctrlLayout->setSpacing(5);  
  91. }  
  92.   
  93. void MainDlg::createContentFrame()  
  94. {  
  95.     contentFrame = new QFrame;  
  96.       
  97.     QLabel *labValue = new QLabel(tr("Please select one of the values:"));  
  98.     QSpinBox *spbValue = new QSpinBox;  
  99.     QHBoxLayout *valueLayout = new QHBoxLayout;  
  100.     valueLayout->addWidget(labValue);  
  101.     valueLayout->addWidget(spbValue);  
  102.     valueLayout->setSpacing(5);  
  103.   
  104.     QLabel *labString = new QLabel(tr("Please input a string"));  
  105.     QLineEdit *edtString = new QLineEdit;  
  106.     QHBoxLayout *stringLayout = new QHBoxLayout;  
  107.     stringLayout->addWidget(labString);  
  108.     stringLayout->addWidget(edtString);  
  109.     stringLayout->setSpacing(5);  
  110.   
  111.     QTextEdit *edtHelloQt = new QTextEdit(tr("Hello Qt!"));  
  112.       
  113.     QPushButton *btnOk = new QPushButton(tr("OK"));  
  114.     QPushButton *btnCancel =new QPushButton(tr("Cancel"));  
  115.     QHBoxLayout *buttonLayout = new QHBoxLayout;  
  116.     buttonLayout->addStretch(1);  
  117.     buttonLayout->addWidget(btnOk);  
  118.     buttonLayout->addWidget(btnCancel);  
  119.     buttonLayout->setSpacing(5);  
  120.   
  121.     QVBoxLayout *contentLayout = new QVBoxLayout(contentFrame);  
  122.     contentLayout->addLayout(valueLayout);  
  123.     contentLayout->addLayout(stringLayout);  
  124.     contentLayout->addWidget(edtHelloQt);  
  125.     contentLayout->addLayout(buttonLayout);  
  126.     contentLayout->setMargin(5);  
  127.     contentLayout->setSpacing(5);  
  128.   
  129.     btnOk->setAutoFillBackground(true);  
  130.     btnCancel->setAutoFillBackground(true);  
  131.     contentFrame->setAutoFillBackground(true);  
  132. }  
  133.   
  134. void MainDlg::sl_window()  
  135. {  
  136.     QStringList colorList = QColor::colorNames();  
  137.     QColor color = QColor(colorList[cbbWindow->currentIndex()]);  
  138.     QPalette p = contentFrame->palette();  
  139.     p.setColor(QPalette::Window,color);  
  140.     contentFrame->setPalette(p);  
  141. }  
  142.   
  143. void MainDlg::sl_windowText()  
  144. {  
  145.     QStringList colorList = QColor::colorNames();  
  146.     QColor color = QColor(colorList[cbbWindowText->currentIndex()]);  
  147.     QPalette p = contentFrame->palette();  
  148.     p.setColor(QPalette::WindowText,color);  
  149.     contentFrame->setPalette(p);  
  150. }  
  151.   
  152. void MainDlg::sl_button()  
  153. {  
  154.     QStringList colorNameList = QColor::colorNames();  
  155.     QColor m_color = QColor(colorNameList[cbbButton->currentIndex()]);  
  156.     QPalette p = contentFrame->palette();  
  157.     p.setColor(QPalette::Button,m_color);  
  158.     contentFrame->setPalette(p);  
  159. }  
  160.   
  161. void MainDlg::sl_buttonText()  
  162. {  
  163.     QStringList colorNameList = QColor::colorNames();  
  164.     QColor m_color = QColor(colorNameList[cbbButtonText->currentIndex()]);  
  165.     QPalette p = contentFrame->palette();  
  166.     p.setColor(QPalette::ButtonText,m_color);  
  167.     contentFrame->setPalette(p);  
  168. }  
  169.   
  170.   
  171. void MainDlg::sl_base()  
  172. {  
  173.     QStringList colorNameList = QColor::colorNames();  
  174.     QColor m_color = QColor(colorNameList[cbbBase->currentIndex()]);  
  175.     QPalette p = contentFrame->palette();  
  176.     p.setColor(QPalette::Base,m_color);  
  177.     contentFrame->setPalette(p);  
  178. }  

猜你喜欢

转载自blog.csdn.net/wang15061955806/article/details/73023658