当前位置:网站首页>QPalette调色板、框架色彩填充
QPalette调色板、框架色彩填充
2022-08-01 17:33:00 【小小工程员】
QPalette调色板
QFrame 框填充颜色
参考书籍:
Book_LuWenZhou
参考代码:
penColorLabel =new QLabel(tr("画笔颜色:")); //画笔颜色选择控件
penColorFrame =new QFrame; //形状、框架。 填充为选择的画笔颜色
penColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);
penColorFrame->setAutoFillBackground(true);
penColorFrame->setPalette(QPalette(Qt::blue)); //画刷
penColorBtn =new QPushButton(tr("更改"));
connect(penColorBtn,SIGNAL(clicked()),this,SLOT(ShowPenColor()));
void MainWidget::ShowPenColor()
{
//static_cast < type-id > ( expression ) 该运算符把expression转换为type-id类型
// QColor color = QColorDialog::getColor(static_cast<int>(Qt::blue));
QColor color = QColorDialog::getColor((Qt::blue));
penColorFrame->setPalette(QPalette(color)); //设置Frame框为选择的颜色
//更新画笔
int value = penWidthSpinBox->value();
Qt::PenStyle style = Qt::PenStyle(penStyleComboBox->itemData(
penStyleComboBox->currentIndex(),Qt::UserRole).toInt());
Qt::PenCapStyle cap = Qt::PenCapStyle(penCapComboBox->itemData(
penCapComboBox->currentIndex(),Qt::UserRole).toInt());
Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
paintArea->setPen(QPen(color,value,style,cap,join));
}
QToolButton工具按钮填充颜色
参考代码:
colorBtn =new QToolButton; //创建颜色选择控件
QPixmap pixmap(20,20);
pixmap.fill(Qt::black);
colorBtn->setIcon(QIcon(pixmap));
connect(colorBtn,SIGNAL(clicked()),this,SLOT(ShowColor()));
void MainWindow::ShowColor()
{
QColor color = QColorDialog::getColor(static_cast<int> (Qt::black), this);
//使用标准颜色对话框QColorDialog获得一个颜色值
if(color.isValid()) //选中了颜色
{
//将新选择的颜色传给绘制区,用于改变画笔的颜色值
drawWidget->setColor(color);
QPixmap p(20,20);
p.fill(color);
colorBtn->setIcon(QIcon(p)); //更新颜色选择按钮上的颜色显示
}
}
QComboBox下拉选项框填充颜色
参考代码:
#include "palette.h"
#include <QHBoxLayout>
#include <QGridLayout>
Palette::Palette(QWidget *parent)
: QDialog(parent)
{
createCtrlFrame();
createContentFrame();
QHBoxLayout *mainLayout =new QHBoxLayout(this);
mainLayout->addWidget(ctrlFrame);
mainLayout->addWidget(contentFrame);
}
void Palette::createCtrlFrame()
{
windowLabel =new QLabel(tr("QPalette::Window: ")); //背景色
windowComboBox =new QComboBox; //创建一个QComboBox对象
fillColorList(windowComboBox); //(a) 向下拉列表框中插入各种不同的颜色选项
connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindow()));//(b)
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()));
ctrlFrame =new QFrame; //颜色选择面板
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()
{
label1 =new QLabel(tr("请选择一个值:"));
comboBox1 =new QComboBox;
label2 =new QLabel(tr("请输入字符串:"));
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); //控件对象,行,列,占用的行数,占用的列数,对齐方式(默认为0)
OkBtn =new QPushButton(tr("确认"));
CancelBtn =new QPushButton(tr("取消"));
QHBoxLayout *BottomLayout =new QHBoxLayout;
BottomLayout->addStretch(1);
BottomLayout->addWidget(OkBtn);
BottomLayout->addWidget(CancelBtn);
contentFrame =new QFrame; // 设置显示面板
QVBoxLayout *mainLayout =new QVBoxLayout(contentFrame); // 垂直布局
mainLayout->addLayout(TopLayout); //加入子布局
mainLayout->addLayout(BottomLayout);
}
void Palette::ShowWindow()
{
//获得当前选择的颜色值
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[windowComboBox->currentIndex()]);
QPalette p = contentFrame->palette(); //(a)
p.setColor(QPalette::Window,color); //(b) 设置窗体的window类颜色主题,颜色值为color
//把修改后的调色板信息应用到contentFrame窗体中,更新显示
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::ShowButton()
{
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);
}
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(); //(a) 获得QT所有内置名称的颜色名列表。
QString color; //(b)
foreach(color,colorList) //对颜色名列表进行遍历
{
QPixmap pix(QSize(70,20)); //(c)
pix.fill(QColor(color)); //为pix填充当前遍历的颜色
comboBox->addItem(QIcon(pix),NULL); //(d) 插入图标icon,名称设为NULL,即不显示颜色的名称
comboBox->setIconSize(QSize(70,20)); //(e) 设置图标的尺寸,默认是方形,将其设为和pix尺寸相同的长方形
comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
//(f) 设置下拉列表框的尺寸调整策略为:符合内容的大小。
}
}
Palette::~Palette()
{
}
``
边栏推荐
猜你喜欢
How can become a good architect necessary skills: painting for all the people praise the system architecture diagram?What is the secret?Quick to open this article and have a look!.
SQL的索引详细介绍
RecSys'22|CARCA: Cross-Attention-Aware Context and Attribute Recommendations
一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近
Ali's official Redis development specification
Flask框架实战
金仓数据库KingbaseES安全指南--6.3. Kerberos身份验证
RecSys'22|CARCA:交叉注意力感知上下文和属性进行推荐
深入分析类加载器
棕榈油罐区数字化转型
随机推荐
MySQL加锁案例分析
Sftp中文件名乱码
实现mnist手写数字识别
今年最火爆的词:商业分析,看这一篇就够了!
DateTime Helper Class for C#
[供应链·案例篇]石油和天然气行业的数字化转型用例
Bugku-Misc-贝斯手
Winform message prompt box helper class
SRM供应商管理系统如何助力口腔护理企业实现采购战略的转型升级
关于Mysql服务无法启动的问题
The anxiety of the post-90s was cured by the vegetable market
MySql 怎么查出符合条件的最新的数据行?
2022 Strong Net Cup CTF---Strong Net Pioneer ASR wp
插入排序 优化插入排序
05 Doris cluster construction
表达式;运算符,算子;取余计算;运算符优先顺序
半自动化爬虫-爬取一个网站的内容及回复
06 redis cluster structures
The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!
金仓数据库 KDTS 迁移工具使用指南(3. 系统部署)