1. 功能简介:
QPalette是Qt中的调色板类,它提供的setColor()函数可改变控件的颜色,其原型为:
void QPalette::setColor(ColorRole acr, const QColor &acolor)
其中, ColorRole是个枚举,指的是颜色主题,QPalette::Window是指背景色,QPalette::WindowText指的是前景色, QPalette::Button指的是按钮的底色...
2. 实例
新建一个Qt Widgets Application工程, 基类为QDialog或QMainWindow,取消"创建界面"复选框
2.1 mainwindow.h 代码如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QComboBox>
#include <QTextEdit>
#include <QPushButton>
#include <QLineEdit>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void createCtrlFrame();
void createContentFrame();
void fillColorList(QComboBox*);
private slots:
void ShowWindow();
void ShowWindowText();
void ShowButton();
void ShowButtonText();
void ShowBase();
private:
QFrame* ctrlFrame;
QLabel* windowLabel;
QComboBox* windowComboBox;
QLabel* windowTextLabel;
QComboBox* windowTextComboBox;
QLabel* buttonLabel;
QComboBox* buttonComboBox;
QLabel* buttonTextLabel;
QComboBox* buttonTextComboBox;
QLabel* baseLabel;
QComboBox* baseComboBox;
QFrame* contentFrame;
QLabel* label1;

该博客介绍了如何在Qt中利用QPalette类的setColor函数改变控件颜色,通过QComboBox展示颜色选择,并根据用户选择更新QFrame的背景色、前景色、按钮底色和按钮文字色。示例代码详细展示了实现过程。
最低0.47元/天 解锁文章
2371





