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 ...
随机推荐
- lua闭包函数
function createCountdownTimer(second) local ms = second * local function countDown() ms = ms - retur ...
- java MD5加密的工具类
import java.security.MessageDigest; /** * MD5加密工具类 * @author zwq */ public class MD5Util { /** * MD5 ...
- angular中的ng-click指令案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- https://coderwall.com/p/7smjkq/multiple-ssh-keys-for-different-accounts-on-github-or-gitlab
Multiple SSH keys for different accounts on Github or Gitlab SSH GIT GITLAB GITHUB Sometimes you nee ...
- SqlServer与MySql语法比较
1.复制表(包括表结构.表数据) SqlServer: Select * into user_copy from user MySql: CREATE TABLE user_copy LIKE use ...
- 接口测试及Postman工具
首先,什么是接口呢? 接口一般来说有两种,一种是程序内部的接口,一种是系统对外的接口.系统对外的接口:比如你要从别的网站或服务器上获取资源或信息,别人肯定不会把数据库共享给你,他只能给你提供一个他们写 ...
- DB2解决死锁
方法一.查看db2diag.log文件 找到DeadLock or Lock timeout搜索 死锁或锁超时信息db2 force application(句柄ID)直接结束进程即可. 方法二.DB ...
- python中if语句的使用
1.对体重标准的判断 #coding:utf-8 height=170weight=65#weight=height-105if weight<height-105: print '您偏瘦!注意 ...
- Editing a Book 搜索 + meet in the middle
我们可以发现最多只会进行5次操作. 由此我们从双向跑dfs,用一个unordered_map来保存状态,枚举一下两边的深度即可. 如果4次仍然不可行,则只有可能是5次.所以正反最多只需要搜2层 cod ...
- 关闭浏览器 清除session
捕获关闭浏览器的事件 关于关闭IE清空session的总结 Session过期会清楚session 还可以手动清除session实现关闭浏览器时清除session的方法