Qt之QColorDialog

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
public slots:
    void showColor();
};

#endif // WIDGET_H

widget.cpp:

#include "widget.h"
#include<QColor>
#include<QColorDialog>
#include<QPushButton>
#include<QPalette> //调色板
#include<QLabel>
#include<QHBoxLayout>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->resize(600,480);
    QPalette qpalette=this->palette();
    QHBoxLayout *s=new QHBoxLayout(this);
    qpalette.color( QPalette::WindowText);
    qpalette.setColor(QPalette::WindowText,QColor(255,0,0));
    qpalette.setColor(QPalette::Background,QColor("#0f8Fb0"));
    this->setPalette(qpalette);
    QPushButton *qp=new QPushButton("打开",this);
    QLabel* ql=new QLabel("打开",this);
    s->addWidget(qp);
    s->addWidget(ql);
   connect(qp,SIGNAL(clicked()),this,SLOT(showColor()));
}
void Widget::showColor()
{
        QColor qc=QColorDialog::getColor(QColor("#0023ff"),this,"dfsd");
        if(qc.isValid())
        {
        QPalette temp=this->palette();
        temp.setColor(QPalette::Background,qc);
            this->setPalette(temp);
        }
}
Widget::~Widget()
{

}

main.cpp:

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

效果图:

原文地址:https://www.cnblogs.com/SunShine-gzw/p/13266835.html