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

393 Views Asked by At

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!

2

There are 2 best solutions below

1
On BEST ANSWER

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.

0
On

I have found that every object contains a variable for windowTitle. If this isn't the main window, the window title is generally left blank, therefore I can store data here.

Granted, this probably isn't the cleanest approach, but it'll serve for now.

Green Cell's subclassing is most likely the best way to resolve this issue. However, I am mainly building the UI using Qt Designer, and want to primarily keep any edits within that wrapper.

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