26.QT颜色与布局

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颜色与布局的更多相关文章
- (Android UI)Android应用程序中资源:图片、字符串、颜色、布局等
Android系统设计采用代码和布局分离的设计模式,因此在设计Android应用程序时需要遵循该设计模式. “把非代码资源(如图片和字符串常量)和代码分离开来始终是一种很好的做法.”---<An ...
- qt坐标系统与布局的简单入门
qt坐标系统 qt坐标系统比較简单 ); 上面的代码把button显示为父窗体的20,20处宽度为100,高度为100 接下去是布局 qt里面布局须要增加<QLayout.h>这个头 ...
- QT设置centralWidget布局
QT设置centralWidget布局 设置之前是这样的,这时候即使设置了控件的布局,实际上控件大小还是不会跟这变,因为centralWidget没有设置布局. 需要在没有控件的空白区域,点击右键在布 ...
- Qt中的布局浅析与弹簧的使用,以及Qt居中的两种方法
1. 布局 为什么要布局: 布局之后窗口的排列是有序的 布局之后窗口的大小发生变化, 控件的大小也会对应变化 如果不对控件布局, 窗口显示出来之后有些控件的看不到的 布局是可以嵌套使用 常用的布局方式 ...
- (一)Qt界面设计布局
Qt提供四种布局: 这种布局生成的格局比较单一,这时候需要另外两个填充控件,来生成整行或整列的格式. 注意:使用Spacers控件时,必须要放在layouts中的布局中,否则无法保存. 示例: 1.往 ...
- Qt入门(6)——Qt的界面布局
Qt提供四种布局: VBoxLayout:垂直布局 HBoxLayout:水平布局 GridLayout:二维布局. FormLayout: 窗体布局
- Qt颜色下拉框
上周为了用Qt写一个类似颜色下拉框的东西,查阅了网上的多数相关资料,依然没有我想要的.终于在周四的时候下定决心重写QCombobox类来实现功能,现在把它贴出来,望看到的人,批评指正.废话不多说,先上 ...
- qt——常用的布局方法
布局相关对象及简介 窗体上的所有的控件必须有一个合适的尺寸和位置.Qt提供了一些类负责排列窗体上的控件,主要有:QHBoxLayout,QVBoxLayout,QGridLayout,QStackLa ...
- QT的组件布局
在QT的IDE下,编写一个自定义布局. #include<QApplication> #include<QWidget> #include<QSpinBox> #i ...
随机推荐
- [转]C++内存管理
[导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不 ...
- 互联网的大数据神话——NoSQL
本文摘抄于:<纵横大数据--云计算数据基础设施> 何小朝著 Chapter5. NewSQL--关系数据库联邦/联合 5.4.2 互联网的神话 对强一致性的要求放松,是因为 互联网的分布 ...
- matlab中的@函数
原文链接:http://blog.sina.com.cn/s/blog_5e73a8fc0100t9yg.html 这是个函数句柄 @(x,y) 表示未知数是x和y punct - Funct ...
- 00--Linux常用命令大全
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- (转) RabbitMQ学习之远程过程调用(RPC)(java)
http://blog.csdn.net/zhu_tianwei/article/details/40887885 在一般使用RabbitMQ做RPC很容易.客户端发送一个请求消息然后服务器回复一个响 ...
- C# 遍历对象下的 属性
foreach (System.Reflection.PropertyInfo p in users.GetType().GetProperties()) { var xx = p.Name; var ...
- paramiko模块学习笔记
SSHClient 基于用户名密码连接 import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ss ...
- 图片无损放大工具PhotoZoom如何进行打印设置
我们使用PhotoZoom对照片进行无失真放大后,想将照片给打印出来需要设置一些常规参数时.那么这些参数我们该从哪里设置,怎么设置呢? PhotoZoom下载:pan.baidu.com/s/1cXb ...
- 写shell工具类,一个常用实例
简述: 当我们常用到某些指令时,我们就需要将这个命令进行封装.封装的设计和扩展,因人而异.但为了每个人都能够了解到这个命令,常需要写出这个类的help. 关键字: 函数.getopts 函数 通过自定 ...
- alsa文章
http://blog.csdn.net/azloong/article/details/6140824 http://blog.csdn.net/tianshuai1111/article/deta ...