QPalette调色板、框架色彩填充

QPalette调色板

QFrame 框填充颜色

参考书籍:
Book_LuWenZhou
参考代码:
QFrame效果

    penColorLabel =new QLabel(tr("画笔颜色:"));      //画笔颜色选择控件
    penColorFrame =new QFrame; //形状、框架。  填充为选择的画笔颜色
    penColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    penColorFrame->setAutoFillBackground(true);
    penColorFrame->setPalette(QPalette(Qt::blue)); //画刷
    penColorBtn =new QPushButton(tr("更改"));
    connect(penColorBtn,SIGNAL(clicked()),this,SLOT(ShowPenColor()));

void MainWidget::ShowPenColor()
{
    //static_cast < type-id > ( expression )   该运算符把expression转换为type-id类型
//    QColor color = QColorDialog::getColor(static_cast<int>(Qt::blue));
    QColor color = QColorDialog::getColor((Qt::blue));
    penColorFrame->setPalette(QPalette(color));   //设置Frame框为选择的颜色
    //更新画笔
    int value = penWidthSpinBox->value();
    Qt::PenStyle style = Qt::PenStyle(penStyleComboBox->itemData(
            penStyleComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenCapStyle cap = Qt::PenCapStyle(penCapComboBox->itemData(
            penCapComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
    paintArea->setPen(QPen(color,value,style,cap,join));
}

QToolButton工具按钮填充颜色

在这里插入图片描述
参考代码:

    colorBtn =new QToolButton;                  //创建颜色选择控件
    QPixmap pixmap(20,20);
    pixmap.fill(Qt::black);
    colorBtn->setIcon(QIcon(pixmap));
    connect(colorBtn,SIGNAL(clicked()),this,SLOT(ShowColor()));

void MainWindow::ShowColor()
{
    QColor color = QColorDialog::getColor(static_cast<int> (Qt::black), this);
    //使用标准颜色对话框QColorDialog获得一个颜色值
    if(color.isValid())  //选中了颜色
    {
       //将新选择的颜色传给绘制区,用于改变画笔的颜色值
        drawWidget->setColor(color);
        QPixmap p(20,20);
        p.fill(color);
        colorBtn->setIcon(QIcon(p));		//更新颜色选择按钮上的颜色显示
    }
}

QComboBox下拉选项框填充颜色

效果
参考代码:

#include "palette.h"
#include <QHBoxLayout>
#include <QGridLayout>
Palette::Palette(QWidget *parent)
    : QDialog(parent)
{
    createCtrlFrame();
    createContentFrame();
    QHBoxLayout *mainLayout =new QHBoxLayout(this);
    mainLayout->addWidget(ctrlFrame);
    mainLayout->addWidget(contentFrame);
}

void Palette::createCtrlFrame()
{
    windowLabel =new QLabel(tr("QPalette::Window: "));  //背景色
    windowComboBox =new QComboBox;			//创建一个QComboBox对象
    fillColorList(windowComboBox);			//(a)  向下拉列表框中插入各种不同的颜色选项
    connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindow()));//(b)

    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;                  //颜色选择面板
    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()
{
    label1 =new QLabel(tr("请选择一个值:"));
    comboBox1 =new QComboBox;

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

    textEdit =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(textEdit,2,0,1,2);  //控件对象,行,列,占用的行数,占用的列数,对齐方式(默认为0)

    OkBtn =new QPushButton(tr("确认"));
    CancelBtn =new QPushButton(tr("取消"));
    QHBoxLayout *BottomLayout =new QHBoxLayout;
    BottomLayout->addStretch(1);
    BottomLayout->addWidget(OkBtn);
    BottomLayout->addWidget(CancelBtn);

    contentFrame =new QFrame;                	// 设置显示面板
    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();		//(a)
    p.setColor(QPalette::Window,color);			//(b)  设置窗体的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);
}

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();	//(a)  获得QT所有内置名称的颜色名列表。
    QString color;									//(b)
    foreach(color,colorList)						//对颜色名列表进行遍历
    {
        QPixmap pix(QSize(70,20));					//(c)
        pix.fill(QColor(color));                    //为pix填充当前遍历的颜色
        comboBox->addItem(QIcon(pix),NULL);         //(d)  插入图标icon,名称设为NULL,即不显示颜色的名称
        comboBox->setIconSize(QSize(70,20));		//(e)  设置图标的尺寸,默认是方形,将其设为和pix尺寸相同的长方形
        comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
        //(f) 设置下拉列表框的尺寸调整策略为:符合内容的大小。
    }
}

Palette::~Palette()
{

}

``
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值