当前位置:网站首页>QPalette palette, frame color fill
QPalette palette, frame color fill
2022-08-01 17:38:00 【little engineer】
QPalette调色板
QFrame Box fill color
参考书籍:
Book_LuWenZhou
参考代码:
penColorLabel =new QLabel(tr("画笔颜色:")); //画笔颜色选择控件
penColorFrame =new QFrame; //形状、框架. Fill with the selected brush color
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)); //设置FrameThe box is the selected color
//Update brush
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));
}
QToolButtonTool button fill color
参考代码:
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);
//Use the standard color dialogQColorDialog获得一个颜色值
if(color.isValid()) //Color is selected
{
//将新选择的颜色传给绘制区,用于改变画笔的颜色值
drawWidget->setColor(color);
QPixmap p(20,20);
p.fill(color);
colorBtn->setIcon(QIcon(p)); //更新颜色选择按钮上的颜色显示
}
}
QComboBoxDrop-down option box fill color
参考代码:
#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) Insert various color options into the drop-down list box
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; //Color selection panel
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("Please select a value:"));
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); //控件对象,行,列,The number of rows occupied,The number of columns occupied,对齐方式(默认为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); //Add sub-layout
mainLayout->addLayout(BottomLayout);
}
void Palette::ShowWindow()
{
//Get the currently selected color value
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[windowComboBox->currentIndex()]);
QPalette p = contentFrame->palette(); //(a)
p.setColor(QPalette::Window,color); //(b) 设置窗体的windowClass color theme,颜色值为color
//Apply the modified palette information to 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) 获得QTList of color names for all built-in names.
QString color; //(b)
foreach(color,colorList) //Iterate over the list of color names
{
QPixmap pix(QSize(70,20)); //(c)
pix.fill(QColor(color)); //为pixFills the current traversal with the color
comboBox->addItem(QIcon(pix),NULL); //(d) 插入图标icon,名称设为NULL,i.e. the name of the color is not displayed
comboBox->setIconSize(QSize(70,20)); //(e) Set the size of the icon,默认是方形,将其设为和pixRectangles of the same size
comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
//(f) Sets the drop-down list box's resizing policy to :Fits the size of the content.
}
}
Palette::~Palette()
{
}
``
边栏推荐
猜你喜欢
随机推荐
中信证券是国内十大券商吗?怎么开户安全?
关于2022年深圳市福田区支持高端服务业发展项目的申报通知
SQL函数 TO_CHAR(一)
Ali's official Redis development specification
tooltip 控件
B011 - 基于51的多功能指纹智能锁
2022年MySQL最新面试题
B005 – 基于STC8的单片机智能路灯控制系统
参观首钢园
GRUB2的零日漏洞补丁现已推出
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!.
ROS2支持技术:DDS简述
Topology零部件拆解3D可视化解决方案
02 es cluster construction
SQL函数 TO_CHAR(二)
关于LocalDateTime的全局返回时间带“T“的时间格式处理
创造建材数字转型新视界,中建材如何多边赋能集团业务快速发展
【TDP加码福利】COS用户实践征文月,等你来投稿!!!
存储日报-数据湖架构权威指南(使用 Iceberg 和 MinIO)
金仓数据库 MySQL 至 KingbaseES 迁移最佳实践(2. 概述)