我是靠谱客的博主 欣慰唇彩,最近开发中收集的这篇文章主要介绍QPalette QColor QComboBox 自定义调色板,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、效果图
在这里插入图片描述
二、代码

#ifndef PALETTEWIDGET_H
#define PALETTEWIDGET_H

#include <QWidget>

namespace Ui {
class paletteWidget;
}

class paletteWidget : public QWidget
{
    Q_OBJECT

public:
    explicit paletteWidget(QWidget *parent = nullptr);
    ~paletteWidget();

private:
    Ui::paletteWidget *ui;
};

#endif // PALETTEWIDGET_H

#include "palettewidget.h"
#include "ui_palettewidget.h"
#include <QPixmap>
#include <QColor>
#include <QPalette>
#include <QIcon>
#include <QDebug>
paletteWidget::paletteWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::paletteWidget)
{
    ui->setupUi(this);

    ui->frame->setAutoFillBackground(true);
    ui->pushButton->setAutoFillBackground(true);
    ui->pushButton_2->setAutoFillBackground(true);

    QStringList colors = QColor::colorNames();
    foreach(auto color,colors){
        QPixmap item(72,22);
        item.fill(QColor(color));

        ui->comboBox->addItem(QIcon(item),nullptr);
        ui->comboBox->setIconSize(QSize(72,22));
        ui->comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
        connect(ui->comboBox, QOverload<int>::of(&QComboBox::activated),[=](int index){
            QString strColor = colors.at(index);
            QPalette p = ui->frame->palette();
            p.setColor(QPalette::Window,QColor(strColor));
            ui->frame->setPalette(p);
        });


        ui->comboBox_2->addItem(QIcon(item),nullptr);
        ui->comboBox_2->setIconSize(QSize(72,22));
        ui->comboBox_2->setSizeAdjustPolicy(QComboBox::AdjustToContents);
        connect(ui->comboBox_2, QOverload<int>::of(&QComboBox::activated),[=](int index){
            QString strColor = colors.at(index);
            QPalette p = ui->frame->palette();
            p.setColor(QPalette::WindowText,QColor(strColor));
            ui->frame->setPalette(p);
        });

        ui->comboBox_3->addItem(QIcon(item),nullptr);
        ui->comboBox_3->setIconSize(QSize(72,22));
        ui->comboBox_3->setSizeAdjustPolicy(QComboBox::AdjustToContents);
        connect(ui->comboBox_3, QOverload<int>::of(&QComboBox::activated),[=](int index){
            QString strColor = colors.at(index);
            QPalette p = ui->frame->palette();
            p.setColor(QPalette::Button,QColor(strColor));
            ui->pushButton->setPalette(p);
            ui->pushButton_2->setPalette(p);
        });

        ui->comboBox_4->addItem(QIcon(item),nullptr);
        ui->comboBox_4->setIconSize(QSize(72,22));
        ui->comboBox_4->setSizeAdjustPolicy(QComboBox::AdjustToContents);
        connect(ui->comboBox_4, QOverload<int>::of(&QComboBox::activated),[=](int index){
            QString strColor = colors.at(index);
            QPalette p = ui->frame->palette();
            p.setColor(QPalette::ButtonText,QColor(strColor));
            ui->pushButton->setPalette(p);
            ui->pushButton_2->setPalette(p);
        });

        ui->comboBox_5->addItem(QIcon(item),nullptr);
        ui->comboBox_5->setIconSize(QSize(72,22));
        ui->comboBox_5->setSizeAdjustPolicy(QComboBox::AdjustToContents);
        connect(ui->comboBox_5, QOverload<int>::of(&QComboBox::activated),[=](int index){
            QString strColor = colors.at(index);
            QPalette p = ui->frame->palette();
            p.setColor(QPalette::Base,QColor(strColor));
            ui->frame->setPalette(p);
        });
    }

    show();
}

paletteWidget::~paletteWidget()
{
    delete ui;
}

最后

以上就是欣慰唇彩为你收集整理的QPalette QColor QComboBox 自定义调色板的全部内容,希望文章能够帮你解决QPalette QColor QComboBox 自定义调色板所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部