dialog.h

 #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 = );
~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

dialog.cpp

 #include "dialog.h"
#include <QHBoxLayout>
#include <QGridLayout>
#include <QPalette>
#include <QBoxLayout> 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("控件背景色: "));
windowComboBox = new QComboBox;
fillColorList(windowComboBox);
//控件与时间绑定
connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(showWindow())); //窗口前景色
windowTextLabel = new QLabel(tr("窗口字体颜色: "));
windowTextComboBox = new QComboBox;
fillColorList(windowTextComboBox);
connect(windowTextComboBox,SIGNAL(activated(int)),this,SLOT(showWindowText())); //窗口按钮的颜色
buttonLabel = new QLabel(tr("窗口按钮的颜色: "));
buttonComboBox = new QComboBox;
fillColorList(buttonComboBox);
connect(buttonComboBox,SIGNAL(activated(int)),this,SLOT(showButton())); //窗口按钮上面文本的颜色
buttonTextLabel = new QLabel(tr("窗口按钮上面文本的颜色: "));
buttonTextComboBox = new QComboBox;
fillColorList(buttonTextComboBox);
connect(buttonTextComboBox,SIGNAL(activated(int)),this,SLOT(showButtonText())); //编辑框背景色
baseLabel = new QLabel(tr("编辑框背景色: "));
baseComboBox = new QComboBox;
fillColorList(baseComboBox);
connect(baseComboBox,SIGNAL(activated(int)),this,SLOT(showBase())); //创建网格布局,框架是ctrFrame
QGridLayout *mainLayout = new QGridLayout(ctrlFrame);
//设置间距
mainLayout->setSpacing();
mainLayout->addWidget(windowLabel,,);
mainLayout->addWidget(windowComboBox,,); mainLayout->addWidget(windowTextLabel,,);
mainLayout->addWidget(windowTextComboBox,,); mainLayout->addWidget(buttonLabel,,);
mainLayout->addWidget(buttonComboBox,,); mainLayout->addWidget(buttonTextLabel,,);
mainLayout->addWidget(buttonTextComboBox,,); mainLayout->addWidget(baseLabel,,);
mainLayout->addWidget(baseComboBox,,); } //创建右边布局
void Palette::createContentFrame()
{
contentFrame = new QFrame;
label1 = new QLabel(tr("请选择一个值:"));
comboBox1 = new QComboBox; label2 = new QLabel(tr("请输入字符串: "));
lineEdit2 = new QLineEdit; textEdit = new QTextEdit; //创建网格布局
QGridLayout *topLayout = new QGridLayout;
topLayout->addWidget(label1,,);
topLayout->addWidget(comboBox1,,);
topLayout->addWidget(label2,,);
topLayout->addWidget(lineEdit2,,);
topLayout->addWidget(textEdit,,,,); okBtn = new QPushButton(tr("确认"));
cancelBtn = new QPushButton(tr("取消")); //创建水平布局
QHBoxLayout *bottomLayout = new QHBoxLayout;
//设置伸缩性
bottomLayout->addStretch();
bottomLayout->addWidget(okBtn);
bottomLayout->addWidget(cancelBtn); //创建垂直布局,把两个布局加入,框架是contentFrame
QVBoxLayout *mainlayout = new QVBoxLayout(contentFrame);
mainlayout->addLayout(topLayout);
mainlayout->addLayout(bottomLayout); //允许自动填充
okBtn->setAutoFillBackground(true);
cancelBtn->setAutoFillBackground(true);
//设置可以填充
contentFrame->setAutoFillBackground(true);
} //用于控制背景颜色的显示
void Palette::showWindow()
{
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[windowComboBox->currentIndex()]); QPalette p = contentFrame->palette();
p.setColor(QPalette::Window, color);
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::showButtonText()
{
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[buttonTextComboBox->currentIndex()]); QPalette p = contentFrame->palette();
p.setColor(QPalette::ButtonText , color);
contentFrame->setPalette(p);
} //edit背景颜色设置
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();
QString color; foreach (color, colorList)
{
QPixmap pix(QSize(,));
pix.fill(QColor(color));
comboBox->addItem(QIcon(pix), NULL);
comboBox->setIconSize(QSize(,));
comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
}
} //按钮颜色设置
void Palette::showButton()
{
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[buttonComboBox->currentIndex()]); //contentFrame->setAutoFillBackground(true); QPalette p = contentFrame->palette();
p.setColor(QPalette::Button , color);
contentFrame->setPalette(p); contentFrame->update(); } Palette::~Palette()
{ }

26.QT颜色与布局的更多相关文章

  1. (Android UI)Android应用程序中资源:图片、字符串、颜色、布局等

    Android系统设计采用代码和布局分离的设计模式,因此在设计Android应用程序时需要遵循该设计模式. “把非代码资源(如图片和字符串常量)和代码分离开来始终是一种很好的做法.”---<An ...

  2. qt坐标系统与布局的简单入门

     qt坐标系统 qt坐标系统比較简单 ); 上面的代码把button显示为父窗体的20,20处宽度为100,高度为100 接下去是布局 qt里面布局须要增加<QLayout.h>这个头 ...

  3. QT设置centralWidget布局

    QT设置centralWidget布局 设置之前是这样的,这时候即使设置了控件的布局,实际上控件大小还是不会跟这变,因为centralWidget没有设置布局. 需要在没有控件的空白区域,点击右键在布 ...

  4. Qt中的布局浅析与弹簧的使用,以及Qt居中的两种方法

    1. 布局 为什么要布局: 布局之后窗口的排列是有序的 布局之后窗口的大小发生变化, 控件的大小也会对应变化 如果不对控件布局, 窗口显示出来之后有些控件的看不到的 布局是可以嵌套使用 常用的布局方式 ...

  5. (一)Qt界面设计布局

    Qt提供四种布局: 这种布局生成的格局比较单一,这时候需要另外两个填充控件,来生成整行或整列的格式. 注意:使用Spacers控件时,必须要放在layouts中的布局中,否则无法保存. 示例: 1.往 ...

  6. Qt入门(6)——Qt的界面布局

    Qt提供四种布局: VBoxLayout:垂直布局 HBoxLayout:水平布局 GridLayout:二维布局. FormLayout: 窗体布局

  7. Qt颜色下拉框

    上周为了用Qt写一个类似颜色下拉框的东西,查阅了网上的多数相关资料,依然没有我想要的.终于在周四的时候下定决心重写QCombobox类来实现功能,现在把它贴出来,望看到的人,批评指正.废话不多说,先上 ...

  8. qt——常用的布局方法

    布局相关对象及简介 窗体上的所有的控件必须有一个合适的尺寸和位置.Qt提供了一些类负责排列窗体上的控件,主要有:QHBoxLayout,QVBoxLayout,QGridLayout,QStackLa ...

  9. QT的组件布局

    在QT的IDE下,编写一个自定义布局. #include<QApplication> #include<QWidget> #include<QSpinBox> #i ...

随机推荐

  1. Fail2ban + firewalld 防护doss攻击

    系统环境:centos7.3 用途:利用fail2ban+Firewalld来防CC攻击和SSH爆破 准备工作: 1.检查Firewalld是否启用 #如果您已经安装iptables建议先关闭 ser ...

  2. 原型模式(Prototype)C++实现

    意图:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 实用性:1.当要实例化的类是在运行时刻指定时. 2.为了避免创建一个与产品类层次平行的工厂类层次时. 3.当一个类的实例只能有几 ...

  3. Vue的前端路由

    vue-router-- 根据不同的地址找到不同的页面                                       (单页面应用:无需频繁的从后台刷新页面) 1,安装路由-->导 ...

  4. 在无任何报错的情况下 pagehelper.startpage分页无效问题

    问题原因:自从spring boot开始使用2.0x版本以上后,很多相应的依赖文件版本开始变化 该版本为spring-boot 1.4.1 <dependency> <groupId ...

  5. 【路飞学城Day170】算法小结

    Evernote Export 算法的思想是能省则省,内存能少则少,时间运行能少尽量少 堆排序的时间复杂度O(nlogn) 堆排序的内置模块heapq 常用函数 heapify(x) heappush ...

  6. UGUI实现打字的效果

    创建打字特效控制的脚本,将该脚本挂载都含有Text组件的对象上面,通过该脚本控制Text文本框的文字,以打字的效果显示文字. 脚本 using UnityEngine; using System.Co ...

  7. nyoj27-水池数目【DFS】

    题目描述: 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上仅标识了此处是否是水池,现在,你的任务来了,请用计算机算出该地图中共有几个水 ...

  8. redi通过哨兵sentinel实现主从切换

    本次实验主要为了让哨兵监听redis主从复制,当主节点关闭后,哨兵会选举一台从节点成为主节点,并且让其他从节点变成新主节点得从节点 本次理论需要三台机器,一主两从,为了方便用一台服务器开启三个实例,一 ...

  9. 搞定PHP面试 - 运算符知识点整理

    一.算术运算符 1. 概览 例子 名称 结果 $a + $b 加法 $a 和 $b 的和. $a - $b 减法 $a 和 $b 的差. $a * $b 乘法 $a 和 $b 的积. $a / $b ...

  10. ongl表达式中得到对象,调用对象方法(OA项目权限显示模块)

    在用户是否拥有某项权限的问题  是这样解决的: 用户登录之后  登录信息是保存在session域中的  通过el表达式可得到登录的对象信息  那么怎样判断用户是否拥有某项权限呢 ?如果没有上图中的判断 ...