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 ...
随机推荐
- Java单例模式解析(收藏)
在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容易出现问题.下面就单例设计模式详细的探讨一下. 所谓单例模式,简单来说,就是在整个应用中保证只有一个类的实例存在.就 ...
- pc端和移动端的轮播图实现(只是结构,内容以后慢慢补充)
轮播图 PC端 移动端 原生js的写法 图片顺序 8123456781 设置计时器 当过度完成之后判断index是否到达两边界限,是的话设置位移 手指touchstart时,获取位置,暂停计时器 手指 ...
- (转)50 个 jQuery 小技巧
1. 如何修改jQuery默认编码(例如默认UTF-8改成改GB2312): $.ajaxSetup({ajaxSettings:{ contentType:"application/x-w ...
- Python 实现简单图片验证码登录
朋友说公司要在测试环境做接口测试,登录时需要传入正确的图片的验证码,本着懒省事的原则,推荐他把测试环境的图片验证码写死,我们公司也是这么做的^_^.劝说无果/(ㄒoㄒ)/~~,只能通过 OCR 技术来 ...
- RawURL
Request.RawUrl表示当前页面, Response.Redirect重新打开页面. 意思就是重新打开当前页面. 和下面一样的 string url=Request.RawUrl: Respo ...
- vim 常用操作笔记
跳转最后一行 :$ 或 shift+g 跳转第一行 :1 或 gg 设置自动换行 :set wrap 设置不自动换行 :set nowrap
- PHP 数组 & 字符串处理
1:数组分割为字符串 implode 2:字符串分割为数组 explode() 3:替换字符串 eg: $a = "Hello world" str_replace(“H”,“ ...
- 企业级任务调度框架Quartz(8) 线程在Quartz里的意义(2)
前序: 做为企业里的任务调度框架,出现同一时间点同时运行两个任务,或者两个任务因为开始的执行时间和执行时间的长短,很有可能出现任务并发执行的情况:因为Quartz的实现是采用java编程,那 ...
- NP是什么意思?
举例叙述(转自百度百科,纯为学习笔记) 编辑 在一个周六的晚上,你参加了一个盛大的晚会.由于感到局促不安,你想知道这一大厅中是否有你已经认识的人.你的主人向你提议说,你一定认识那位正在甜点盘附近角落的 ...
- mybatis传入参数类型parameterType详解
前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType属性,用于对应的mapper接口方法接受的参数类型. ( res ...