qt icon状态

在qt designer中我们可以看到,一些控件允许设置图标,然后在它下面会出现很多项目
在这里插入图片描述
对这些不同项目的解释见 enum QIcon::Mode

ConstantValueDescription
QIcon::Normal0Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available. 控件可用时,如果没有检测到交互事件(如鼠标事件),显示该图标
QIcon::Disabled1Display the pixmap when the functionality represented by the icon is not available.控件禁用时,显示该图标
QIcon::Active2Display the pixmap when the functionality represented by the icon is available and the user is interacting with the icon, for example, moving the mouse over it or clicking it. 控件可用时,并且检测到交互事件(如鼠标事件),显示该图标
QIcon::Selected3Display the pixmap when the item represented by the icon is selected. 控件被选中时,显示该图标

QIcon::State

ConstantValueDescription
QIcon::Off1Display the pixmap when the widget is in an “off” state. 开关控件处于off状态(check)时显示该图标
QIcon::On0Display the pixmap when the widget is in an “on” state. 开关控件处于on状态(uncheck)时显示该图标

通过QIcon::addPixmapQIcon::addFile设置不同情况下的图标

### 实现 Qt 状态栏高亮显示效果 为了实现在 Qt 中的状态栏高亮显示效果,可以采用多种方法来达到这一目标。一种常见的方式是通过自定义 `QStatusBar` 或者在其上添加带有特定样式的部件。 #### 方法一:使用 QSS 设置状态栏整体样式 可以通过设置整个应用程序或窗口的样式表 (QSS),从而改变状态栏的颜色和其他属性: ```css QStatusBar { background-color: #f0f0f0; } ``` 这种方法适用于想要统一修改整个应用中的所有状态栏的情况[^1]。 #### 方法二:创建带样式的 QLabel 并将其添加到状态栏 如果只需要针对某个具体的消息或者部分区域进行高亮,则可以考虑向状态栏中插入一个具有特殊样式的 `QLabel` 控件作为临时消息提示器: ```cpp // 创建一个新的标签用于展示信息并设置其初始外观 QLabel *label = new QLabel(this); label->setStyleSheet("background-color: yellow; padding: 5px;"); label->setText(tr("This is a highlighted message")); // 将该标签添加至状态栏内指定位置 statusBar()->addPermanentWidget(label); // 定义定时器自动移除这条消息(可选) QTimer::singleShot(3000, label, SLOT(deleteLater())); ``` 这段代码展示了如何动态地往状态栏里面增加一个短暂存在的、带有背景色变化的通知条目[^2]。 #### 方法三:利用 QWidgetAction 自定义复杂交互行为 对于更复杂的场景,比如希望某些条件下触发不同的视觉反馈模式,还可以借助于 `QWidgetAction` 来构建更加灵活多样的解决方案。这种方式允许我们为每一个动作关联独立的小部件,并且能够方便地管理它们之间的相互作用关系[^3]: ```cpp // 构建一个包含图标的按钮组件 QPushButton *highlightButton = new QPushButton(); highlightButton->setIcon(QIcon(":/icons/highlight_icon.png")); highlightButton->setFlat(true); // 不绘制默认边框以便更好地融入界面设计 // 使用 QWidgetAction 包装上述按钮实例 QWidgetAction *action = new QWidgetAction(this); action->setDefaultWidget(highlightButton); // 把这个 action 添加进状态栏之中 statusBar()->addAction(action); connect(highlightButton, SIGNAL(clicked()), this, SLOT(onHighlightClicked())); ``` 以上三种方式分别对应了不同程度的需求范围——全局性的主题调整、局部化的即时通知以及高度定制化的行为逻辑处理;根据实际应用场景的不同可以选择最合适的一种来进行开发实践。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值