当前位置:网站首页>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 .
边栏推荐
- 125kHz wake-up function 2.4GHz single transmitter chip-si24r2h
- What constitutes the smart charging pile system?
- Windows 安装 MySQL 5.7详细步骤
- NFC two-way communication 13.56MHz contactless reader chip -- si512 replaces pn512
- Collation of ml.net related resources
- Back up Google or other browser plug-ins
- Simplefoc+platformio stepping on the path of the pit
- STM32 printf problem summary semihosting microlib understanding
- pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
- Inclination sensor is used for long-term monitoring of communication tower and high-voltage tower
猜你喜欢

Stm32ff030 replaces domestic MCU dp32g030

分段分页以及段页结合

Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT

110道 MySQL面试题及答案 (持续更新)

ROS tutorial (Xavier)

Second week of postgraduate freshman training: convolutional neural network foundation

ROS common instructions

【学术相关】为什么很多国内学者的AI的论文复现不了?

Application of explosion-proof inclination sensor in safe operation of LNG

The first week of postgraduate freshman training: deep learning and pytorch Foundation
随机推荐
网络安全之安全基线
Ws2812b color lamp driver based on f407zgt6
What is the working principle of the noise sensor?
Inclination sensor accuracy calibration test
110道 MySQL面试题及答案 (持续更新)
[beauty of software engineering - column notes] 29 | automated testing: how to kill bugs in the cradle?
DC motor speed regulation system based on 51 single chip microcomputer (use of L298)
Collation of ml.net related resources
Crawl expression bag
New energy shared charging pile management and operation platform
PostgreSQL手动创建HikariDataSource解决报错Cannot commit when autoCommit is enabled
leetcode hot 100(刷题篇9)(301/45/517/407/offer62/MST08.14/)
Alibaba political commissar system - Chapter 4: political commissars are built on companies
sql判断语句的编写
Beautiful girls
[beauty of software engineering - column notes] 27 | what is the core competitiveness of software engineers? (top)
Hal learning notes - Advanced timer of 7 timer
[beauty of software engineering - column notes] 26 | continuous delivery: how to release new versions to the production environment at any time?
AES 双向加密解密工具
Lora opens a new era of Internet of things -asr6500s, asr6501/6502, asr6505, asr6601