当前位置:网站首页>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 .
边栏推荐
- ML.NET相关资源整理
- Background management system platform of new energy charging pile
- Tensorboard use
- [beauty of software engineering - column notes] 23 | Architect: programmers who don't want to be architects are not good programmers
- 随机抽奖转盘微信小程序项目源码
- Simple calculator wechat applet project source code
- Phy6252 is an ultra-low power Bluetooth wireless communication chip for the Internet of things
- Dp1332e multi protocol highly integrated contactless read-write chip
- Unity multiplayer online framework mirror learning record (I)
- PostgreSQL手动创建HikariDataSource解决报错Cannot commit when autoCommit is enabled
猜你喜欢
阿里巴巴政委体系-第四章、政委建在连队上
ROS tutorial (Xavier)
[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification
Proteus simulation based on msp430f2491 (realize water lamp)
【Transformer】SegFormer:Simple and Efficient Design for Semantic Segmentation with Transformers
华为无线设备配置利用WDS技术部署WLAN业务
STM32 detection signal frequency
Lora opens a new era of Internet of things -asr6500s, asr6501/6502, asr6505, asr6601
Privacy is more secure in the era of digital RMB
110道 MySQL面试题及答案 (持续更新)
随机推荐
Arduinoide + stm32link burning debugging
To create a thread pool for the rate, start the core thread
分段分页以及段页结合
Cv520 domestic replacement of ci521 13.56MHz contactless reader chip
torch.Tensor.to的用法
STM32 serial port garbled
ML.NET相关资源整理
New energy shared charging pile management and operation platform
Unicode private use areas
PostgreSQL manually creates hikaridatasource to solve the error cannot commit when autocommit is enabled
Arduino uno error analysis avrdude: stk500_ recv(): programmer is not responding
Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT
Crawl notes
Back up Google or other browser plug-ins
Unicode私人使用区域(Private Use Areas)
STM32 MDK (keil5) contents mismatch error summary
Proteus simulation based on msp430f2491 (realize water lamp)
Reading papers on false news detection (4): a novel self-learning semi supervised deep learning network to detect fake news on
Ws2812b color lamp driver based on f407zgt6
谷歌浏览器免跨域配置