Search code examples
pythonpysidemayaqabstractbutton

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


I am trying to store string data within a QAbstractButton.text().

Why?

I want to display the short name in the text() itself, but be able to call the long name via the text() "comment" through code.

You are able to write "comments" within QT Designer, but I have been unable to replicate this in Python. Looking at the code in notepad, it appears the "comment" text is created within the text string itself:

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

What I currently have in python is:

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

How can I also set and get the comment part of this text? Any advice would be appreciated!


Solution

  • If you want to add extra data to a widget why not just subclass it and create your own?

    class MyCustomButton(QtWidgets.QPushButton):
    
        def __init__(self, parent=None):
            super(MyCustomButton, self).__init__(parent)
    
            self.my_variable = None
    

    Now you can continue using MyCustomButton just like a normal button, and also add whatever you like to my_variable.