调色板运用
效果图:
#ifndef DIALOG_H #define DIALOG_H #include#include #include #include #include #include #include class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = nullptr); ~Dialog(); public: void createCtrlframe(); void createContentframe(); void fillColorList(QComboBox* comboBox); //组合框添加颜色链表 private slots: void ShowWindow(int index); void ShowWindowText(int index); void ShowButton(int index); void ShowButtonText(int index); void Showbase(int index); private: Qframe *m_ctrlframe; QLabel *m_windowLabel; QComboBox *m_windowComboBox; QLabel *m_windowTextLabel; QComboBox *m_windowTextComboBox; QLabel *m_buttonLabel; QComboBox *m_buttonLabelComboBox; QLabel *m_buttonTextLabel; QComboBox *m_buttonTextComboBox; QLabel *m_baseLabel; QComboBox *m_baseComboBox; Qframe *m_contentframe; QLabel *m_label1; QLabel *m_label2; QComboBox *m_comboBox1; QLineEdit *m_lineEdit; QTextEdit *m_textEdit; QPushButton *m_okBtn; QPushButton *m_canceBtn; }; #endif // DIALOG_H
#include "dialog.h" #includeDialog::Dialog(QWidget *parent) : QDialog(parent) { setWindowTitle("QPalette"); setWindowFlags(Qt::WindowCloseButtonHint); createCtrlframe(); createContentframe(); //水平布局 QHBoxLayout *mainLayout = new QHBoxLayout(this); mainLayout->addWidget(m_ctrlframe); mainLayout->addWidget(m_contentframe); } Dialog::~Dialog() { } void Dialog::createCtrlframe() { m_ctrlframe = new Qframe; m_ctrlframe->setframeStyle(Qframe::Sunken | Qframe::Box); //设置风格 m_windowLabel = new QLabel("QPalette::Window:"); m_windowComboBox = new QComboBox; fillColorList(m_windowComboBox); //填充颜色 connect(m_windowComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindow(int))); m_windowTextLabel = new QLabel("QPalette::WindowText:"); m_windowTextComboBox = new QComboBox; fillColorList(m_windowTextComboBox); connect(m_windowTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindowText(int))); m_buttonLabel = new QLabel("QPatte::Button:"); m_buttonLabelComboBox = new QComboBox; fillColorList(m_buttonLabelComboBox); connect(m_buttonLabelComboBox,SIGNAL(activated(int)),this,SLOT(ShowButton(int))); m_buttonTextLabel = new QLabel("QPatte::ButtonText:"); m_buttonTextComboBox = new QComboBox; fillColorList(m_buttonTextComboBox); connect(m_buttonTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowButtonText(int))); m_baseLabel = new QLabel("QPalette::base:"); m_baseComboBox = new QComboBox; fillColorList(m_baseComboBox); connect(m_baseComboBox,SIGNAL(activated(int)),this,SLOT(Showbase(int))); //格线布局 QGridLayout *mainLayout = new QGridLayout(m_ctrlframe); mainLayout->setSpacing(20); //设置间隙 mainLayout->addWidget(m_windowLabel,0,0); mainLayout->addWidget(m_windowComboBox,0,1); mainLayout->addWidget(m_windowTextLabel,1,0); mainLayout->addWidget(m_windowTextComboBox,1,1); mainLayout->addWidget(m_buttonLabel,2,0); mainLayout->addWidget(m_buttonLabelComboBox,2,1); mainLayout->addWidget(m_buttonTextLabel,3,0); mainLayout->addWidget(m_buttonTextComboBox,3,1); mainLayout->addWidget(m_baseLabel,4,0); mainLayout->addWidget(m_baseComboBox,4,1); } void Dialog::createContentframe() { m_contentframe = new Qframe; m_contentframe->setAutoFillBackground(true); m_label1 = new QLabel("请选择一个值"); m_label2 = new QLabel("请输入字符串"); m_comboBox1 = new QComboBox; m_lineEdit = new QLineEdit; m_textEdit = new QTextEdit; m_okBtn = new QPushButton(QString("确认")); m_canceBtn = new QPushButton(QString("取消")); m_okBtn->setAutoFillBackground(true); //背景自动填充 QGridLayout *Toplayout = new QGridLayout; Toplayout->addWidget(m_label1 ,0,0); Toplayout->addWidget(m_comboBox1,0,1); Toplayout->addWidget(m_label2,1,0); Toplayout->addWidget(m_lineEdit,1,1); Toplayout->addWidget(m_textEdit,2,0,1,2); QHBoxLayout *bottomLayout = new QHBoxLayout; bottomLayout->addWidget(m_okBtn); bottomLayout->addWidget(m_canceBtn); //垂直布局 QVBoxLayout *mainLayout = new QVBoxLayout(m_contentframe); mainLayout->addLayout(Toplayout); mainLayout->addLayout(bottomLayout); } void Dialog::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),NULL); comboBox->setIconSize(QSize(70,20)); //根据内容进行适应 comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents); } } void Dialog::ShowWindow(int index) { QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[index]); QPalette p = m_contentframe->palette(); p.setColor(QPalette::Window,color); m_contentframe->setPalette(p); //触发重绘 m_contentframe->update(); } void Dialog::ShowWindowText(int index) { QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[index]); QPalette p = m_contentframe->palette(); p.setColor(QPalette::WindowText,color); m_contentframe->setPalette(p); m_contentframe->update(); } void Dialog::ShowButton(int index) { QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[index]); QPalette p = m_contentframe->palette(); p.setColor(QPalette::Button,color); m_contentframe->setPalette(p); m_contentframe->update(); } void Dialog::ShowButtonText(int index) { QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[index]); QPalette p = m_contentframe->palette(); p.setColor(QPalette::ButtonText,color); m_contentframe->setPalette(p); m_contentframe->update(); } void Dialog::Showbase(int index) { QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[index]); QPalette p = m_contentframe->palette(); p.setColor(QPalette::base,color); m_contentframe->setPalette(p); m_contentframe->update(); }



