【Qt5开发及实例】11、QPalette调色板使用

我做的这个不知道为什么有两个功能无法显示,也没有报错,我暂时是想不出什么好办法= =


实现目标

palette.h

/**
* 书本:【Qt5开发及实例】
* 功能:实现调色板的使用QPalette
* 文件:paletee.h
* 时间:2015年1月3日12:21:53
* 作者:cutter_point
*/
#ifndef PALETTE_H
#define PALETTE_H

#include <QDialog>
#include <QComboBox>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QLineEdit>

class Palette : public QDialog
{
  Q_OBJECT

public:
  Palette(QWidget *parent = 0);
  ~Palette();
  //我们把这个界面分为两个部分,左边和右边
  //左边界面的创建
  void createCtrlFrame();
  //右边界面的创建
  void createContentFrame();
  //颜色下拉列表框中输入颜色的工作
  void fillColorList(QComboBox *);

private slots:  //槽函数,背景色,前景色显示
  void ShowWindow();
  void ShowWindowText();
  void ShowButton();
  void ShowButtonText();
  void ShowBase();

private:
  QFrame *ctrlFrame;      //颜色选择板,左板块
  QLabel *windowLabel;
  QComboBox *windowComboBox;
  QLabel *windowTextLabel;
  QComboBox *windowTextComboBox;
  QLabel *buttonLabel;
  QComboBox *buttonComboBox;
  QLabel *buttonTextLabel;
  QComboBox *buttonTextComboBox;
  QLabel *baseLabel;
  QComboBox *baseComboBox;

  QFrame *contentFrame;   //具体显示的面板,也就是右半块
  QLabel *label1;
  QComboBox *comboBox1;
  QLabel *label2;
  QLineEdit *lineEdit2;
  QTextEdit *textEdit;
  QPushButton *OkBtn;
  QPushButton *CancelBtn;



};

#endif // PALETTE_H

palette.cpp

/**
* 书本:【Qt5开发及实例】
* 功能:实现具体的调色板
* 文件:palette.h
* 时间:2015年1月3日12:20:05
* 作者:cutter_point
*/
#include "palette.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QtDebug>

Palette::Palette(QWidget *parent)
  : QDialog(parent)
{//构造函数
  createCtrlFrame();    //构造左边界面
  createContentFrame();   //右边

  QHBoxLayout *mainLayout = new QHBoxLayout(this);
  mainLayout->addWidget(ctrlFrame);   //左边的界面添加进入
  mainLayout->addWidget(contentFrame);    //右边界面添加进入

}

//创建左边的颜色选择区
void Palette::createCtrlFrame()
{
  ctrlFrame = new QFrame;
  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()));

  //网格布局
  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()
{
  contentFrame = new QFrame;    //创建右边界面
//  contentFrame->setFrameStyle(QFrame::Shadow_Mask);
  label1 = new QLabel(tr("please select one value: "));    //请不要吐槽我的English
  comboBox1 = new QComboBox;
  comboBox1->addItem(tr("1"));
  comboBox1->addItem(tr("2"));
  comboBox1->addItem(tr("3"));

  label2 = new QLabel(tr("please input String: "));    //请不要吐槽我的English
  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);   //行,列,占用行, 占用列,还有一个对齐方式没写出来

  OkBtn = new QPushButton(tr("ok"));    //确定
  CancelBtn = new QPushButton(tr("cancel"));    //取消

  QHBoxLayout *BottomLayout = new QHBoxLayout;
  BottomLayout->addStretch(1);    //一个弹簧
  BottomLayout->addWidget(OkBtn);
  BottomLayout->addWidget(CancelBtn);

  //整体垂直布局
  QVBoxLayout *mainLayout = new QVBoxLayout(contentFrame);
  mainLayout->addLayout(TopLayout);
  mainLayout->addLayout(BottomLayout);

}

//显示背景颜色
void Palette::ShowWindow()      //这个不知道为什么显示不出来!!!有的说是windows不支持,我也不太清楚2015年1月3日13:51:44
{
  QStringList colorList = QColor::colorNames();   //得到所有的颜色序列
  QColor color = QColor(colorList[windowComboBox->currentIndex()]);   //取得当前选中的序号,得到颜色
  QPalette p = contentFrame->palette();   //得到右边的调色板
  p.setColor(QPalette::Window, color);   //修改相应位置的颜色
  contentFrame->setPalette(p);     //把调色板的值给应用上去
  contentFrame->update();   //刷新界面,得到显示
  /*
   * QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[cbbWindow->currentIndex()]);
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Window,color);
    contentFrame->setPalette(p);
   * */
}

//显示前景色
void Palette::ShowWindowText()
{
  QStringList colorList = QColor::colorNames();   //得到所有的颜色序列
  QColor color = QColor(colorList[windowTextComboBox->currentIndex()]);   //取得当前选中的序号,得到颜色

  QPalette p = contentFrame->palette();   //得到右边的调色板
  p.setColor(QPalette::WindowText, color);   //修改相应位置的颜色
  contentFrame->setPalette(p);     //把调色板的值给应用上去

//  contentFrame->update();
}

//按钮背景颜色
void Palette::ShowButton()  //这个不知道为什么显示不出来!!!有的说是windows不支持,我也不太清楚2015年1月3日13:51:44
{
  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);     //把调色板的值给应用上去

//  contentFrame->update();
}

//文本框背景色的选择
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);     //把调色板的值给应用上去

//  contentFrame->update();
}

//fillColorList用于插入颜色
void Palette::fillColorList(QComboBox *comboBox)
{
  QStringList colorList = QColor::colorNames();   //得到所有颜色名
  QString color;

  foreach(color, colorList)   //循环取出colorList里面的值
    {
      QPixmap pix(QSize(70, 20));   //这个pix作为显示颜色的图标
      pix.fill(QColor(color));    //为pix填充当前遍历到的颜色
      comboBox->addItem(QIcon(pix), NULL);    //把pix作为图标插入进去
      comboBox->setIconSize(QSize(70, 20));     //把选项设置成和pix一样的大小
      comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);     //设置下拉框的大小符合内容大小

    }

}

Palette::~Palette()
{

}

实现结果:





还有就是我发现上传东西还是有总空间限制的,就是说上传多了,空间大小就不够了,坑爹的是我居然只有60M,我也是呵呵了,这真是金贵,我还不如搞个网盘呢





  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值