Qt QWidget
成员函数
move(int x, int y);
move(const QPoint &);
setGeometry(int x, int y, int w, int h);
setMimiMumSize(int w, int h);
setMaxiMumSize(int w, int h);
setFont(const QFont &);
QPalette(调色板)
管理控件外观显示。
ColorGroup
- QPalette::Active,获得焦点状态
- QPalette::Inactive,未获得焦点状态
- QPalette::Disable,不可用状态
不可行时用qss代替。
setStyleSheet("background-color: rgb(170, 0, 255);");
//首先调用,设置控件自动填充背景
setAutoFillBackground(true);
//单一颜色填充
void QPalette::setColor(ColorGroup group, ColorRole role, const QColor &color);
void QPalette::setColor(ColorRole role, const QColor &color);
//非单一颜色(比如图片)填充
void QPalette::setBrush(ColorGroup group, ColorRole role, const QBrush &brush);
void QPalette::setColor(ColorRole role, const QBrush &brush);
Palette.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Palette
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp \
palette.cpp
HEADERS += palette.h
main.cpp
#include "palette.h"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Palette w;
w.show();
return a.exec();
}
palette.h
#ifndef PALETTE_H
#define PALETTE_H
#include <QDialog>
#include <QComboBox>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QGridLayout>
class Palette : public QDialog {
Q_OBJECT
public:
explicit Palette(QWidget *parent = 0);
void fillColorList(QComboBox *);//完成向颜色下拉列表框中插入颜色的工作
private slots:
void ShowWindow();
void ShowWindowText();
void ShowButton();
void ShowButtonText();
void ShowBase();
private:
QLabel *windowLabel;
QComboBox *windowComboBox;
QLabel *windowTextLabel;
QComboBox *windowTextComboBox;
QLabel *buttonLabel;
QComboBox *buttonComboBox;
QLabel *buttonTextLabel;
QComboBox *buttonTextComboBox;
QLabel *baseLabel;
QComboBox *baseComboBox;
QFrame *ctrlFrame; //颜色选择面板
QGridLayout *leftLayout;
QLabel *label1;
QComboBox *comboBox1;
QLabel *label2;
QLineEdit *lineEdit2;
QTextEdit *textEdit;
QGridLayout *TopLayout;
QPushButton *OkBtn;
QPushButton *CancelBtn;
QHBoxLayout *BottomLayout;
QFrame *contentFrame; //具体显示面板
QVBoxLayout *rightLayout;
QHBoxLayout *mainLayout;
};
#endif // PALETTE_H
palette.cpp
#include "palette.h"
#include <QStandardItemModel>
Palette::Palette(QWidget *parent) : QDialog(parent) {
//完成窗体左半部分颜色选择区的创建
windowLabel = new QLabel(tr("QPalette::Window: "));
windowComboBox = new QComboBox;
fillColorList(windowComboBox);
connect(windowComboBox, SIGNAL(activated(int)), this, SLOT(ShowWindow()));
windowTextLabel = new QLabel(tr("QPalette::WindowText: "));
windowTextComboBox = new QComboBox;
fillColorList(windowTextComboBox);
connect(windowTextComboBox, SIGNAL(activated(int)), this, SLOT(ShowWindowText()));
buttonLabel = new QLabel(tr("QPalette::Button: "));
buttonComboBox = new QComboBox;
fillColorList(buttonComboBox);
connect(buttonComboBox, SIGNAL(activated(int)), this, SLOT(ShowButton()));
buttonTextLabel = new QLabel(tr("QPalette::ButtonText: "));
buttonTextComboBox = new QComboBox;
fillColorList(buttonTextComboBox);
connect(buttonTextComboBox, SIGNAL(activated(int)), this, SLOT(ShowButtonText()));
baseLabel = new QLabel(tr("QPalette::Base: "));
baseComboBox = new QComboBox;
fillColorList(baseComboBox);
connect(baseComboBox, SIGNAL(activated(int)), this, SLOT(ShowBase()));
ctrlFrame = new QFrame; //颜色选择面板
leftLayout = new QGridLayout(ctrlFrame);
leftLayout->setSpacing(20);
leftLayout->addWidget(windowLabel, 0, 0);
leftLayout->addWidget(windowComboBox, 0, 1);
leftLayout->addWidget(windowTextLabel, 1, 0);
leftLayout->addWidget(windowTextComboBox, 1, 1);
leftLayout->addWidget(buttonLabel, 2, 0);
leftLayout->addWidget(buttonComboBox, 2, 1);
leftLayout->addWidget(buttonTextLabel, 3, 0);
leftLayout->addWidget(buttonTextComboBox, 3, 1);
leftLayout->addWidget(baseLabel, 4, 0);
leftLayout->addWidget(baseComboBox, 4, 1);
//完成窗体右半部分的创建
label1 = new QLabel(tr("请选择一个值:"));
comboBox1 = new QComboBox;
//comboBox1->setStyleSheet("background-color: red;");
//comboBox1->setStyleSheet("QComboBox::drop-down {background: red;}");
/*
QPalette pal = comboBox1->palette();
pal.setColor(QPalette::Base, QColor("red"));
comboBox1->setPalette(pal);
*/
comboBox1->addItems({"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"});
QStandardItemModel *pItemModel = qobject_cast<QStandardItemModel*>(comboBox1->model());
pItemModel->item(0)->setForeground(QColor(255, 0, 0)); //修改某项文本颜色
pItemModel->item(0)->setBackground(QColor(0, 0, 255)); //修改某项背景颜色
label2 = new QLabel(tr("请输入字符串:"));
lineEdit2 = new QLineEdit;
//lineEdit2->setAutoFillBackground(true);//使用QPlatte失效
lineEdit2->setStyleSheet("background-color: rgba(0, 255, 255, 255);");
textEdit = new QTextEdit;
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(textEdit,2, 0, 1, 2);
OkBtn = new QPushButton(tr("确认"));
OkBtn->setAutoFillBackground(true); //使用QPlatte失效
CancelBtn = new QPushButton(tr("取消"));
CancelBtn->setStyleSheet("background-color: rgb(170, 0, 255);");
BottomLayout = new QHBoxLayout;
BottomLayout->addStretch(1);
BottomLayout->addWidget(OkBtn);
BottomLayout->addWidget(CancelBtn);
contentFrame = new QFrame; //具体显示面板
contentFrame->setAutoFillBackground(true);
rightLayout = new QVBoxLayout(contentFrame);
rightLayout->addLayout(TopLayout);
rightLayout->addLayout(BottomLayout);
mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(ctrlFrame);
mainLayout->addWidget(contentFrame);
setWindowFlags(Qt::WindowCloseButtonHint);
}
void Palette::ShowWindow() {
//获得当前选择的颜色值
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[windowComboBox->currentIndex()]);
QPalette p = contentFrame->palette();
p.setColor(QPalette::Window, color);
//把修改后的调色板信息应用到contentFrame窗体中,更新显示
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);
contentFrame->update();
}
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) {
foreach(QString color, QColor::colorNames()) { //对颜色名列表进行遍历
QPixmap pix(QSize(70, 20));
pix.fill(QColor(color)); //为pix填充当前遍历的颜色
comboBox->addItem(QIcon(pix), NULL);
comboBox->setIconSize(QSize(70, 20));
comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
}
}