Qt之對話框設計——利用QPalette改變控件顏色


QPalette類相當於對話框或控件的調色板,它管理着控件或窗體的所有顏色信息,每個窗體或控件都包含一個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::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

#ifndef MAINDLG_H
#define MAINDLG_H

#include <QtGui/QDialog>
#include <QFrame>
#include <QComboBox>


class MainDlg : public QDialog
{
	Q_OBJECT

public:
	MainDlg(QWidget *parent = 0, Qt::WFlags flags = 0);
	~MainDlg();

	void createCtrlFrame();
	void createContentFrame();
	void fillColorList(QComboBox *);

public slots:
	void sl_window();
	void sl_windowText();
	void sl_button();
	void sl_buttonText();
	void sl_base();

private:
	QFrame *ctrlFrame;	//顏色選擇面板
	QFrame *contentFrame;	//具體顯示面板
	QComboBox *cbbWindow;
	QComboBox *cbbWindowText;
	QComboBox *cbbButton;
	QComboBox *cbbButtonText;
	QComboBox * cbbBase;
	
};

#endif // MAINDLG_H

maindlg.cpp

#include "maindlg.h"
#include <QPalette>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QStringList>
#include <QColor>
#include <QPixmap>
#include <QSpinBox>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>

MainDlg::MainDlg(QWidget *parent, Qt::WFlags flags)
	: QDialog(parent, flags)
{
	createCtrlFrame();
	createContentFrame();

	QHBoxLayout *mainLayout = new QHBoxLayout(this);
	mainLayout->addWidget(ctrlFrame);
	mainLayout->addWidget(contentFrame);
	mainLayout->setMargin(10);
	mainLayout->setSpacing(5);
	mainLayout->setSizeConstraint(QLayout::SetFixedSize);
}

MainDlg::~MainDlg()
{

}

void MainDlg::fillColorList(QComboBox *cbb)
{
	QStringList colorNameList = QColor::colorNames();

	QString colorName;
	foreach(colorName,colorNameList)
	{
		QPixmap pix_color(70,20);
		pix_color.fill(QColor(colorName));

		cbb->addItem(QIcon(pix_color),NULL);
		cbb->setIconSize(QSize(70,20));
		cbb->setSizeAdjustPolicy(QComboBox::AdjustToContents);	//設置下拉列表的尺寸符合內容的大小
	}
}

void MainDlg::createCtrlFrame()
{
	ctrlFrame = new QFrame;

	QLabel *labWindow = new QLabel(tr("QPalette::Window:"));
	QLabel *labWindowText = new QLabel(tr("QPalette::WindowText:"));
	QLabel *labButton = new QLabel(tr("QPalette::Button:"));
	QLabel *labButtonText = new QLabel(tr("QPalette::ButtonText:"));
	QLabel *labBase = new QLabel(tr("QPalette::Base:"));

	cbbWindow = new QComboBox;
	fillColorList(cbbWindow);
	connect(cbbWindow,SIGNAL(activated(int)),this,SLOT(sl_window()));
	cbbWindowText = new QComboBox;
	fillColorList(cbbWindowText);
	connect(cbbWindowText,SIGNAL(activated(int)),this,SLOT(sl_windowText()));
	cbbButton = new QComboBox;
	fillColorList(cbbButton);
	connect(cbbButton,SIGNAL(activated(int)),this,SLOT(sl_button()));
	cbbButtonText = new QComboBox;
	fillColorList(cbbButtonText);
	connect(cbbButtonText,SIGNAL(activated(int)),this,SLOT(sl_buttonText()));
	cbbBase = new QComboBox;
	fillColorList(cbbBase);
	connect(cbbBase,SIGNAL(activated(int)),this,SLOT(sl_base()));
	
	int col_lab = 0;
	int col_cbb = 1;
	QGridLayout *ctrlLayout = new QGridLayout(ctrlFrame);
	ctrlLayout->addWidget(labWindow,0,col_lab);
	ctrlLayout->addWidget(labWindowText,1,col_lab);
	ctrlLayout->addWidget(labButton,2,col_lab);
	ctrlLayout->addWidget(labButtonText,3,col_lab);
	ctrlLayout->addWidget(labBase,4,col_lab);
	ctrlLayout->addWidget(cbbWindow,0,col_cbb);
	ctrlLayout->addWidget(cbbWindowText,1,col_cbb);
	ctrlLayout->addWidget(cbbButton,2,col_cbb);
	ctrlLayout->addWidget(cbbButtonText,3,col_cbb);
	ctrlLayout->addWidget(cbbBase,4,col_cbb);
	ctrlLayout->setMargin(5);
	ctrlLayout->setSpacing(5);
}

void MainDlg::createContentFrame()
{
	contentFrame = new QFrame;
	
	QLabel *labValue = new QLabel(tr("Please select one of the values:"));
	QSpinBox *spbValue = new QSpinBox;
	QHBoxLayout *valueLayout = new QHBoxLayout;
	valueLayout->addWidget(labValue);
	valueLayout->addWidget(spbValue);
	valueLayout->setSpacing(5);

	QLabel *labString = new QLabel(tr("Please input a string"));
	QLineEdit *edtString = new QLineEdit;
	QHBoxLayout *stringLayout = new QHBoxLayout;
	stringLayout->addWidget(labString);
	stringLayout->addWidget(edtString);
	stringLayout->setSpacing(5);

	QTextEdit *edtHelloQt = new QTextEdit(tr("Hello Qt!"));
	
	QPushButton *btnOk = new QPushButton(tr("OK"));
	QPushButton *btnCancel =new QPushButton(tr("Cancel"));
	QHBoxLayout *buttonLayout = new QHBoxLayout;
	buttonLayout->addStretch(1);
	buttonLayout->addWidget(btnOk);
	buttonLayout->addWidget(btnCancel);
	buttonLayout->setSpacing(5);

	QVBoxLayout *contentLayout = new QVBoxLayout(contentFrame);
	contentLayout->addLayout(valueLayout);
	contentLayout->addLayout(stringLayout);
	contentLayout->addWidget(edtHelloQt);
	contentLayout->addLayout(buttonLayout);
	contentLayout->setMargin(5);
	contentLayout->setSpacing(5);

	btnOk->setAutoFillBackground(true);
	btnCancel->setAutoFillBackground(true);
	contentFrame->setAutoFillBackground(true);
}

void MainDlg::sl_window()
{
	QStringList colorList = QColor::colorNames();
	QColor color = QColor(colorList[cbbWindow->currentIndex()]);
	QPalette p = contentFrame->palette();
	p.setColor(QPalette::Window,color);
	contentFrame->setPalette(p);
}

void MainDlg::sl_windowText()
{
	QStringList colorList = QColor::colorNames();
	QColor color = QColor(colorList[cbbWindowText->currentIndex()]);
	QPalette p = contentFrame->palette();
	p.setColor(QPalette::WindowText,color);
	contentFrame->setPalette(p);
}

void MainDlg::sl_button()
{
	QStringList colorNameList = QColor::colorNames();
	QColor m_color = QColor(colorNameList[cbbButton->currentIndex()]);
	QPalette p = contentFrame->palette();
	p.setColor(QPalette::Button,m_color);
	contentFrame->setPalette(p);
}

void MainDlg::sl_buttonText()
{
	QStringList colorNameList = QColor::colorNames();
	QColor m_color = QColor(colorNameList[cbbButtonText->currentIndex()]);
	QPalette p = contentFrame->palette();
	p.setColor(QPalette::ButtonText,m_color);
	contentFrame->setPalette(p);
}


void MainDlg::sl_base()
{
	QStringList colorNameList = QColor::colorNames();
	QColor m_color = QColor(colorNameList[cbbBase->currentIndex()]);
	QPalette p = contentFrame->palette();
	p.setColor(QPalette::Base,m_color);
	contentFrame->setPalette(p);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM