qt中当鼠标移到按钮上如何改变颜色

发布网友

我来回答

2个回答

热心网友

loginButton->setStyleSheet(tr("QPushButton{background-color:white;}QPushButton:hover{background-color:cyan;}QPushButton:pressed{background-color:red;}"));
调用Qbutton的setStyleSheet类,上面的函数实现鼠标移到按钮前是白色,移到按钮后是蓝色,按下后是红色。当然如果不想换掉按下后的颜色,直接loginButton->setStyleSheet(tr("QPushButton{background-color:white;}QPushButton:hover{background-color:cyan;}"));就行。

热心网友

首先要响应鼠标事件,然后通过 Style Sheets 改变按钮的外观

1. 从QPushButton派生一个自己的按钮类
2.在新类的构造函数中 调用 mouseTracking(true);以获取鼠标移动事件。
3. 重载 mouseMoveEvent(),在该函数中通过StyleSheet改变外观
如: setStyleSheet("background: yellow”);

Qt Style Sheets的使用请参考 Assistance中的内容 和 Style Sheet Example

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com