QT QPushButton 通过stylesheet 设置样式

本文详细介绍如何使用QT的样式表(QSS)来定制按钮的外观,包括背景颜色、文字颜色、边框样式及圆角效果,适用于创建美观且统一的用户界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.QT QPushButton 通过stylesheet 设置样式

ui->pushButton->setStyleSheet("QPushButton{background-color:rgba(255,178,0,100%);\
                                            color: white;   border-radius: 10px;  border: 2px groove gray; border-style: outset;}" // 按键本色
		"QPushButton:hover{background-color:white; color: black;}"  // 鼠标停放时的色彩
		"QPushButton:pressed{background-color:rgb(85, 170, 255); border-style: inset; }"   // 鼠标按下的色彩

2.Qt creator中使用stylesheet方式将按钮设置成圆角按钮,代码如下:
setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;");

 

3. https://blog.youkuaiyun.com/caoshangpa/article/details/78549835

4.QT官方例子

http://doc.qt.io/archives/qt-4.8/stylesheet-examples.html

 

 

Qt 中,你可以通过样式表 (QSS - Qt Style Sheets) 来设置 `QPushButton` 的悬停颜色。以下是详细步骤: --- ### 设置按钮的悬停颜色 Qt 提供了一种类似 CSS 样式的方式来自定义控件外观,这就是 QSS(Qt Style Sheet)。为了给 `QPushButton` 添加悬停效果,我们可以利用伪状态选择器 `:hover`。 #### 示例代码: ```cpp #include <QApplication> #include <QPushButton> int main(int argc, char *argv[]) { QApplication app(argc, argv); QPushButton button("Hover Me"); // 使用样式设置悬停时的颜色 button.setStyleSheet( "QPushButton { " "background-color: white; " // 默认背景色 "color: black;" // 默认文字颜色 "} " "QPushButton:hover { " "background-color: lightblue; " // 鼠标悬浮时背景变为浅蓝色 "color: red;" // 文字颜色变为红色 "}" ); button.resize(200, 50); button.show(); return app.exec(); } ``` --- ### 解释样式的含义 1. **默认状态下**: ```css QPushButton { background-color: white; color: black; } ``` 当鼠标未移动到按钮上时,背景为白色,字体为黑色。 2. **悬停状态下**: ```css QPushButton:hover { background-color: lightblue; color: red; } ``` 当鼠标悬停在按钮上方时,背景会变成浅蓝色,字体颜色变红。 --- ### 注意事项 - **性能优化**:频繁更改样式可能会导致重绘开销较大,特别是在复杂界面中应谨慎使用动态效果。 - **跨平台一致性**:尽管大多数现代系统都支持这种技术,但在某些特殊平台上仍需测试是否一致显示预期结果。 - **禁用按钮的状态处理**:如果你希望对不可用(`disabled`)按钮也定制风格,则可以用`:disabled`伪状态进一步扩展规则。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值