如何使用 setText 设置和获取 "comment" 文本?

How to Set and Get "comment" text using setText?

我正在尝试将字符串数据存储在 QAbstractButton.text() 中。

为什么?

我想在 text() 本身中显示短名称,但能够通过代码通过 text() "comment" 调用长名称。

您可以在 QT Designer 中编写 "comments",但我无法在 Python 中复制它。 查看记事本中的代码,似乎 "comment" 文本是在文本字符串本身中创建的:

<property name="text">
  <string extracomment="toast">Select object and click here</string>

我目前在python中的是:

Xsl = cmds.ls(sl=1)[0]
Xbutton.setText(Xsl)

如何设置和获取此文本的评论部分? 如有任何建议,我们将不胜感激!

如果您想向小部件添加额外数据,为什么不将其子类化并创建您自己的小部件?

class MyCustomButton(QtWidgets.QPushButton):

    def __init__(self, parent=None):
        super(MyCustomButton, self).__init__(parent)

        self.my_variable = None

现在您可以像使用普通按钮一样继续使用 MyCustomButton,还可以将您喜欢的任何内容添加到 my_variable

我发现每个 object 都包含一个 windowTitle 的变量。如果这不是主要的 window,window 标题通常留空,因此我可以在此处存储数据。

当然,这可能不是最干净的方法,但现在可以使用。

Green Cell 的子类化很可能是解决此问题的最佳方法。但是,我主要是使用 Qt Designer 构建 UI,并希望主要将所有编辑保留在该包装器中。

def store_selected_node_on_button(self):
    """
    Changes the text of a given button to store an object's name
    As the button isn't a window, I can set the window title to store the long name of the selected object.        
    :return: None
    """
    button = self.sender()
    sl = cmds.ls(sl=1, long=True)
    if not sl:
        button.setText("Select object and click here")
        button.setWindowTitle("")
    else:
        button.setText(sl[0].split("|")[-1])
        button.setWindowTitle(sl[0])

    return