无法从Pyside2/Qt 5中的操作悬停插槽设置状态栏。

huangapple go评论57阅读模式
英文:

Can't set status bar from action hover slot in Pyside2/Qt 5

问题

我想在菜单中悬停在操作上时,在状态栏中显示工具提示。

由于我尚未找到显示工具提示的方法,所以我正在尝试这个。

然而,我也无法从悬停时的槽/事件/回调中设置状态栏。一切都被调用了,但似乎没有效果,除了清除状态栏。

这是一个最小的工作示例:

英文:

I want to show a tool tip in the status bar when hovering an action that is in a menu.

I am trying this since I could not find a way yet to show a tool tip when hovering.

However, I am not able to set the status bar from the hovering slot/event/callback either. Everything is being called, but it seems to have no effect, other than clearing the status bar.

Here is a minimum working example:

from PySide2.QtWidgets import QApplication, QMainWindow, QAction, QStatusBar

app = QApplication([])

# Create a main window
main_window = QMainWindow()

# Create an action
action = QAction("My Action", main_window)

# Add the action to the main window's menu bar
main_window.menuBar().addAction(action)

# Create a status bar
status_bar = QStatusBar(main_window)
status_bar.showMessage("Initialized.")
main_window.setStatusBar(status_bar)


# Define a function to update the status bar with tooltips
def update_status_bar():
    if action.isEnabled() and action.toolTip():
        status_bar.showMessage(action.toolTip())
    else:
        status_bar.clearMessage()


# Connect the hover events to the status bar update function
action.hovered.connect(update_status_bar)

# Show the main window
main_window.show()

# Run the application event loop
app.exec_()

答案1

得分: 1

问题是由于“hovered”信号具有优先级,然后在触发状态提示事件之前发射该信号。

由于操作的状态提示为空,结果是即使通过“update_status_bar”设置状态消息,它也会立即被空状态提示清除。

简单的解决方法是明确设置状态提示:

action = QAction("My Action", main_window, statusTip="My Action")

# 或者:
action = QAction("My Action", main_window)
action.setStatusTip(action.text())
英文:

The problem is caused by the fact that the hovered signal has precedence, and then it is emitted before the status tip event is triggered.

Since the action's status tip is empty, the result is that, even if the status message is set by update_status_bar, it will instantly be cleared by the empty status tip.

The simple solution is to explicitly set the status tip:

action = QAction("My Action", main_window, statusTip="My Action")

# alternatively:
action = QAction("My Action", main_window)
action.setStatusTip(action.text())

huangapple
  • 本文由 发表于 2023年5月25日 11:48:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定