QT之setStyleSheet设置Button样式

1.首先大致介绍一下setStyleSheet函数的作用:

Qt中设置按钮或QWidget的外观是,可以使用QT Style Sheets来进行设置,非常方便。
可以用setStyleSheet(“font: bold; font-size:20px; color: rgb(241, 70, 62); background-color: green”);来进行设置,其他的样式介绍如下:
font: bold; 是否粗体显示
border-image:""; 用来设定边框的背景图片。
border-radius:5px; 用来设定边框的弧度。可以设定圆角的按钮
border-width: 1px; 边框大小
font-family:""; 来设定字体所属家族,
font-size:20px; 来设定字体大小
font-style:""; 来设定字体样式
font-weight:20px; 来设定字体深浅
background-color: green; 设置背景颜色
background:transparent; 设置背景为透明
color:rgb(241, 70, 62); 设置前景颜色
selection-color:rgb(241, 70, 62); 用来设定选中时候的颜色

可以使用border-top,border-right,border-bottom,border-left分别设定按钮的上下左右边框,
同样有border-left-color, border-left-style, border-left-width.等分别来设定他们的颜色,样式和宽度

2.本次想要实现的效果就是按钮控件点击实现背景图片的来回切换,两种背景图片,在点击按钮控件的时候来回切换,类似与输入法上的中英文切换键一样,下面上代码:
btn_language = new QPushButton();//在头文件中声明
btn_language->setFixedSize(keyboard_btn_width, keyboard_btn_height);
btn_language->setStyleSheet(“QPushButton{background-image:url(:/image/res/EN-CH.png);”
“background-repeat: no-repeat;background-position: center;”
“background-attachment: fixed;background-attachment: fixed;”
“background-attachment: fixed;;background-clip: padding}”);
connect(btn_language, &QPushButton::clicked, this, &CKeyboardForm::btnLanguageClicked);
//设定按钮的背景图片,background-repeat可以设定背景图片的重复规则,这里设定为no-repeat,即不重复。
//background-position用来设定图片的位置,是左(left)还是右(right),还是在中间(center),是上(top)还是底部(bottom)。
//background-attachment用来这定背景图片是否卷动或者和窗口大小相匹配,默认是卷动的。

以上只是实现了按钮控件的背景图片显示,并没有实现切换。关于背景图切换我的思路是在按钮控件绑定的槽函数中进行相关操作,定义一个static flag,每点击一次按钮就就行判断flag的奇偶性,根据不同的情况来进行相关按钮控件的显示。下面给出的参考代码是通过对按钮的输入模式(中英文)进行判断的。
void CKeyboardForm::btnLanguageClicked()
{
if(m_eCurrentMode == InputMode::zh)
{
btn_language->setStyleSheet(“QPushButton{background-image:url(:/image/res/EN-CH.png);”
“background-repeat: no-repeat;background-position: center;”
“background-attachment: fixed;background-attachment: fixed;”
“background-attachment: fixed;;background-clip: padding}”);
m_eCurrentMode = InputMode::en;
}
else if(m_eCurrentMode == InputMode::en)
{
btn_language->setStyleSheet(“QPushButton{background-image:url(:/image/res/CH-EN.png);”
“background-repeat: no-repeat;background-position: center;”
“background-attachment: fixed;background-attachment: fixed;”
“background-attachment: fixed;;background-clip: padding}”);
m_eCurrentMode = InputMode::zh;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值