当前位置:网站首页>Qpalette learning notes
Qpalette learning notes
2022-07-29 08:20:00 【cloud_ yq】
QPalette Class is equivalent to the palette of dialog boxes or controls , It manages all the color information of the control or form , Each form or control contains a QPalette object , When displaying, follow its QPalette Object to describe the color of each part in each state .
QPalette Class has two basic data types : One is ColorGroup, Used to distinguish the color information of space or form in different states ; The other is ColorRole, Color information used to distinguish different components of controls or forms . The specific explanation is as follows :
ColorGroup:
QPalette::Disabled | Unavailable status |
QPalette::Active | Active state ( Focus of attention ) |
QPalette::Inactive | Inactive state ( No focus ) |
ColorRole:
QPalette::Window | A regular background color |
QPalette::Background | This value is obsolete , Use window Instead of |
QPalette::WindowText | A general foreground color ( text color ) |
QPalette::Foreground | This value is obsolete , Use windowText Instead of . |
QPalette::Base | Mainly use the composition input widget (QTextEdit、QListView etc. ) Background color , It can also be used for other , Such as combobox Drop down lists and toolbar. |
QPalette::Text | And QPalette::Base Corresponding , Mainly use the composition input widget (QTextEdit、QListView etc. ) The color of the text |
QPalette::AlternateBase | Used as the background color for alternating view lines ( When the view has the row background color alternation attribute set setAlternatingRowColors(true)), And QPalette::Base Distinguish |
QPalette::ToolTipBase | Used as a background color for QToolTip and QWhatsThis. Tool tip use QPalette Inactive color groups , Because the tool tip is not an active window . |
QPalette::ToolTipText | Used as a foreground color for QToolTip and QWhatsThis. Tool tip use QPalette Inactive color groups , Because the tool tip is not an active window . |
QPalette::Button | button The background color . This background color can be different from window As some styles , Require a different background color as button |
QPalette::ButtonText | A foreground color is used as button Color . |
QPalette::BrightText | One text The color is very different from windowText, Good contrast with dark. Typically used for text, Need to be painted , stay text perhaps windowText Will give a poor comparison , It's like pressing button. Be careful text Color can be used for things , Not just words ;text Color is usually used for text, But it is quite common to use text Color role is line , Icon , wait . |
QPalette Mainly through the following four functions to achieve the color setting of controls or forms :
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 );
among ,setBrush Can be set by QBrush To set the background image of the control or form . in addition , It should be noted that in setting dialog boxes and controls (QPushButton、QFrame etc. ) You need to use
void setAutoFillBackground ( bool enabled ); Set its background to fill .
Next, an example is given (Qt Create a QWidget project ):
//Wdiget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QComboBox>
#include <QFrame>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
// Member variables
QFrame *sFrame;
QFrame *cFrame;
QComboBox *cbWin;
QComboBox *cbWinText;
QComboBox *cbBtn;
QComboBox *cbBtnText;
QComboBox *cbBase;
QComboBox *cbText;
QComboBox *cbAlter;
// Member functions
void seletFrame();
void contentFrame();
void fillComboBox(QComboBox *);
private slots:
void cbWin_s();
void cbWinText_s();
void cbBtn_s();
void cbBtnText_s();
void cbBase_s();
void cbText_s();
void cbAlter_s();
};
#endif // WIDGET_H
//Widget.cpp
#include "widget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QTextEdit>
#include <QStringList>
#include <QListView>
#include <QStringListModel>
#include <QLabel>
#include <QGroupBox>
#include <QPixmap>
#include <QIcon>
#include <QSizePolicy>
#include <QDebug>
#include <QLineEdit>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
seletFrame();
contentFrame();
QHBoxLayout *gHblayout = new QHBoxLayout(this);
gHblayout->addWidget(sFrame);
gHblayout->addWidget(cFrame);
gHblayout->setMargin(10);
gHblayout->setSpacing(5);
gHblayout->setSizeConstraint(QLayout::SetFixedSize);
}
Widget::~Widget()
{
}
void Widget::seletFrame()
{
sFrame = new QFrame;
QLabel *label1 = new QLabel("Qt::Window");
cbWin = new QComboBox;
fillComboBox(cbWin);
connect(cbWin,SIGNAL(activated(int)),this,SLOT(cbWin_s()));
QLabel *label2 = new QLabel("Qt::WindowText");
cbWinText = new QComboBox;
fillComboBox(cbWinText);
connect(cbWinText,SIGNAL(activated(int)),this,SLOT(cbWinText_s()));
//connect(cbWinText,&QComboBox::activated,this,&Widget::cbWinText_s());
QGridLayout *s1Layout = new QGridLayout;
s1Layout->addWidget(label1,0,0);
s1Layout->addWidget(cbWin,0,1);
s1Layout->addWidget(label2,1,0);
s1Layout->addWidget(cbWinText,1,1);
s1Layout->setSizeConstraint(QLayout::SetFixedSize);
QGroupBox *s1GroupBox = new QGroupBox("Window corlor");
s1GroupBox->setLayout(s1Layout);
QLabel *label3 = new QLabel("Qt::Button");
cbBtn = new QComboBox;
fillComboBox(cbBtn);
connect(cbBtn,SIGNAL(activated(int)),this,SLOT(cbBtn_s()));
QLabel *label4 = new QLabel("Qt::ButtonText");
cbBtnText = new QComboBox;
fillComboBox(cbBtnText);
connect(cbBtnText,SIGNAL(activated(int)),this,SLOT(cbBtnText_s()));
QLabel *label5 = new QLabel("Qt::Base");
cbBase = new QComboBox;
fillComboBox(cbBase);
connect(cbBase,SIGNAL(activated(int)),this,SLOT(cbBase_s()));
QLabel *label6 = new QLabel("Qt::Text");
cbText = new QComboBox;
fillComboBox(cbText);
connect(cbText,SIGNAL(activated(int)),this,SLOT(cbText_s()));
QLabel *label7 = new QLabel("Qt::AlternateBase");
cbAlter = new QComboBox;
fillComboBox(cbAlter);
connect(cbAlter,SIGNAL(activated(int)),this,SLOT(cbAlter_s()));
QGridLayout *sgLayout =new QGridLayout(sFrame);
sgLayout->addWidget(s1GroupBox,0,0,1,2); // merge cell , from (0,0) Greg started , occupy 1 That's ok 2 Column
sgLayout->addWidget(label3,1,0);
sgLayout->addWidget(cbBtn,1,1);
sgLayout->addWidget(label4,2,0);
sgLayout->addWidget(cbBtnText,2,1);
sgLayout->addWidget(label5,3,0);
sgLayout->addWidget(cbBase,3,1);
sgLayout->addWidget(label6,4,0);
sgLayout->addWidget(cbText,4,1);
sgLayout->addWidget(label7,5,0);
sgLayout->addWidget(cbAlter,5,1);
sgLayout->setMargin(5);
}
void Widget::contentFrame()
{
cFrame = new QFrame();
cFrame->setLineWidth(2);
cFrame->setFrameStyle(QFrame::Box|QFrame::Raised);
QLabel *labString = new QLabel("please input a string");
QLineEdit *edtString = new QLineEdit;
QHBoxLayout *stringWidget = new QHBoxLayout;
stringWidget->addWidget(labString);
stringWidget->addWidget(edtString);
stringWidget->setSpacing(5);
QListView *cListV = new QListView;
QStringList cstrL;
cstrL << " The man is determined to leave the countryside "<<" Learn not to be famous, swear not to return "<<" Castle Peak is full of bones "<<" Why do you need to return the clothes ";
QStringListModel *cListM = new QStringListModel(cstrL);
cListV->setModel(cListM);
cListV->setAlternatingRowColors(true);
QTextEdit *cTextE = new QTextEdit(" wine is glowing in the luminous jade cups ");
cTextE->append(" If you want to drink pipa, hurry up immediately ");
cTextE->append(" Don't laugh when you are drunk on the battlefield ");
cTextE->append(" In ancient times, several people fought back ");
QPushButton *okBtn = new QPushButton(" confirm ");
QPushButton *cancalBtn = new QPushButton(" Cancel ");
QHBoxLayout *cHLayout = new QHBoxLayout;
cHLayout->addStretch(1);
cHLayout->addWidget(okBtn);
cHLayout->addWidget(cancalBtn);
QVBoxLayout *cVLayout = new QVBoxLayout(cFrame);
cVLayout->addLayout(stringWidget);
cVLayout->addWidget(cListV);
cVLayout->addWidget(cTextE);
cVLayout->addLayout(cHLayout);
cVLayout->setMargin(15);
cVLayout->setSpacing(5);
okBtn->setAutoFillBackground(true);
cancalBtn->setAutoFillBackground(true);
cFrame->setAutoFillBackground(true);
}
void Widget::fillComboBox(QComboBox *c)
{
QStringList colorList = QColor::colorNames();
QString colorName;
foreach(colorName,colorList)
{
QPixmap s_color = QPixmap(70,20);
s_color.fill(QColor(colorName));
//QIcon s_color = QIcon(colorName);
c->addItem(QIcon(s_color),NULL);
c->setIconSize(QSize(70,20));
c->setSizeAdjustPolicy(QComboBox::AdjustToContents);
}
}
void Widget::cbWin_s()
{
qDebug()<<" Get into cbWin_s() Slot function ";
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::Window,QColor(colors[cbWin->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbWinText_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::WindowText,QColor(colors[cbWinText->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbBtn_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::Button,QColor(colors[cbBtn->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbBtnText_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::ButtonText,QColor(colors[cbBtnText->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbBase_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::Base,QColor(colors[cbBase->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbText_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::Text,QColor(colors[cbText->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbAlter_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::AlternateBase,QColor(colors[cbAlter->currentIndex()]));
cFrame->setPalette(p);
}
Result chart :
The essence of this article is https://www.cnblogs.com/hanzhaoxin/archive/2012/11/18/2775918.html Reprint of the article
According to your own learning and understanding , For your own convenience , Made some adjustments .
边栏推荐
- Node: file write data (readfile, WriteFile), two modes: overwrite and increment
- Unicode private use areas
- Windows 安装 MySQL 5.7详细步骤
- Tle5012b+stm32f103c8t6 (bluepill) reading angle data
- (Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
- [beauty of software engineering - column notes] 28 | what is the core competitiveness of software engineers? (next)
- Unity multiplayer online framework mirror learning record (I)
- node:文件写入数据(readFile、writeFile),覆盖与增量两种模式
- Unity shader learning (VI) achieving radar scanning effect
- Network Security Learning chapter
猜你喜欢
[beauty of software engineering - column notes] 22 | how to do a good job in technology selection for the project?
Markdown concise grammar manual
BiSeNet v2
Ws2812b color lamp driver based on f407zgt6
亚马逊测评自养号是什么,卖家应该怎么做?
Random lottery turntable wechat applet project source code
Tb6600+stm32f407 test
Week 2: convolutional neural network basics
[beauty of software engineering - column notes] "one question and one answer" issue 3 | 18 common software development problem-solving strategies
Tle5012b+stm32f103c8t6 (bluepill) reading angle data
随机推荐
Crawl expression bag
Lora opens a new era of Internet of things -asr6500s, asr6501/6502, asr6505, asr6601
Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT
Collation of ml.net related resources
Dp1332e multi protocol highly integrated contactless read-write chip
torch.nn.functional.one_ hot()
Cs5340 domestic alternative dp5340 multi bit audio a/d converter
Usage of torch.tensor.to
Alibaba political commissar system - Chapter III, Alibaba political commissar and cultural docking
深度学习(1):银行客户流失预测
AES bidirectional encryption and decryption tool
STM32 detection signal frequency
Huawei wireless device configuration uses WDS technology to deploy WLAN services
110 MySQL interview questions and answers (continuously updated)
产品推广的渠道和策略,化妆品品牌推广方法及步骤
ML.NET相关资源整理
Reading papers on false news detection (4): a novel self-learning semi supervised deep learning network to detect fake news on
Unity Shader学习(六)实现雷达扫描效果
[beauty of software engineering - column notes] "one question and one answer" issue 2 | 30 common software development problem-solving strategies
为了速率创建线程池,启动核心线程