QT QPalette调色板

QStringList colorList = QColor::colorNames():获得Qt所有知道名称的颜色名列表,返回的是一个字符串列表colorList;
QString color:新建一个QString对象,为循环遍历做准备;
QPixmap pix(QSize(70, 20)):新建一个QPixmap对象pix作为显示颜色的图标;
pix.fill(QColor(color)):为pix填充当前遍历的颜色;

palette.h

#ifndef PALETTE_H
#define PALETTE_H

#include <QDialog>
#include <QComboBox>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QLineEdit>

class Palette : public QDialog
{
    
    
    Q_OBJECT

public:
    Palette(QWidget *parent = nullptr);
    ~Palette();
    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;
    QComboBox *comboBox1;
    QLabel *label2;
    QLineEdit *lineEdit2;
    QTextEdit *textEdit2;
    QPushButton *OkBtn;
    QPushButton *CancelBtn;

};
#endif // PALETTE_H

palette.cpp

#include "palette.h"
#include <QGridLayout>

Palette::Palette(QWidget *parent)
    : QDialog(parent)
{
    
    
    createCtrlFrame();
    createContentFrame();
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(ctrlFrame);
    mainLayout->addWidget(contentFrame);
}

Palette::~Palette()
{
    
    
}

void Palette::createCtrlFrame()      // 完成窗体左半部分颜色选择区的创建
{
    
    
    ctrlFrame = new QFrame();
    windowLabel = new QLabel("QPalette::Window: ");
    windowComboBox = new QComboBox();
    fillColorList(windowComboBox);
    connect(windowComboBox, SIGNAL(activated(int)), this, SLOT(ShowWindow()));

    windowTextLabel = new QLabel("QPalette::WindowText: ");
    windowTextComboBox = new QComboBox();
    fillColorList(windowTextComboBox);
    connect(windowTextComboBox, SIGNAL(activated(int)), this, SLOT(ShowWindowText()));

    buttonLabel = new QLabel("QPalette::Button: ");
    buttonComboBox = new QComboBox();
    fillColorList(buttonComboBox);
    connect(buttonComboBox, SIGNAL(activated(int)), this, SLOT(ShowButton()));

    buttonTextLabel = new QLabel("QPalette::ButtonText: ");
    buttonTextComboBox = new QComboBox();
    fillColorList(buttonTextComboBox);
    connect(buttonTextComboBox, SIGNAL(activated(int)), this, SLOT(ShowButtonText()));

    baseLabel = new QLabel("QPalette::Base: ");
    baseComboBox = new QComboBox();
    fillColorList(baseComboBox);
    connect(baseComboBox, SIGNAL(activated(int)), this, SLOT(ShowBase()));

    QGridLayout *mainLayout = new QGridLayout(ctrlFrame);
    mainLayout->setSpacing(20);
    mainLayout->addWidget(windowLabel, 0, 0);
    mainLayout->addWidget(windowComboBox, 0, 1);
    mainLayout->addWidget(windowTextLabel, 1, 0);
    mainLayout->addWidget(windowTextComboBox, 1, 1);
    mainLayout->addWidget(buttonLabel, 2, 0);
    mainLayout->addWidget(buttonComboBox, 2, 1);
    mainLayout->addWidget(buttonTextLabel, 3, 0);
    mainLayout->addWidget(buttonTextComboBox, 3, 1);
    mainLayout->addWidget(baseLabel, 4, 0);
    mainLayout->addWidget(baseComboBox, 4, 1);

}

void Palette::createContentFrame()  // 完成窗体右半部分的创建
{
    
    
    contentFrame = new QFrame();
    label1 = new QLabel("请选择一个值: ");
    comboBox1 = new QComboBox();

    label2 = new QLabel("请输入字符串: ");
    lineEdit2 = new QLineEdit();

    textEdit2 = new QTextEdit();

    QGridLayout *TopLayout = new QGridLayout();
    TopLayout->addWidget(label1, 0, 0);
    TopLayout->addWidget(comboBox1, 0, 1);
    TopLayout->addWidget(label2, 1, 0);
    TopLayout->addWidget(lineEdit2, 1, 1);
    TopLayout->addWidget(textEdit2, 2, 0, 1,2);

    OkBtn = new QPushButton("确认");
    CancelBtn = new QPushButton("取消");

    QHBoxLayout *BottomLayout = new QHBoxLayout();
    BottomLayout->addStretch(1);
    BottomLayout->addWidget(OkBtn);
    BottomLayout->addWidget(CancelBtn);

    QVBoxLayout *mainLayout = new QVBoxLayout(contentFrame);
    mainLayout->addLayout(TopLayout);
    mainLayout->addLayout(BottomLayout);
}

void Palette::ShowWindow()
{
    
    
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox->currentIndex()]);

    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Window, color);
    contentFrame->setPalette(p);
    contentFrame->update();
}

void Palette::ShowWindowText()
{
    
    
    QStringList colorList = QColor::colorNames();
    QColor color = colorList[windowTextComboBox->currentIndex()];

    QPalette p = contentFrame->palette();
    p.setColor(QPalette::WindowText, color);
    contentFrame->setPalette(p);
}

void Palette::ShowButton()
{
    
    
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[buttonComboBox->currentIndex()]);

    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Button, color);
    contentFrame->setPalette(p);
    contentFrame->update();
}

void Palette::ShowButtonText()
{
    
    
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[buttonTextComboBox->currentIndex()]);

    QPalette p = contentFrame->palette();
    p.setColor(QPalette::ButtonText, color);
    contentFrame->setPalette(p);
}

void Palette::ShowBase()
{
    
    
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[baseComboBox->currentIndex()]);

    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Base, color);
    contentFrame->setPalette(p);
}

void Palette::fillColorList(QComboBox *comboBox) // 完成向颜色下拉列表框中插入颜色的工作
{
    
    
    QStringList colorList = QColor::colorNames();
    QString color;
    foreach (color, colorList) {
    
    
        QPixmap pix(QSize(70, 20));
        pix.fill(QColor(color));
        comboBox->addItem(QIcon(pix), nullptr);
        comboBox->setIconSize(QSize(70, 20));
        comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    }
}

main.cpp

#include "palette.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    Palette w;
    w.show();
    return a.exec();
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u013420428/article/details/109720892
今日推荐