2

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!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Adam Sirrelle
  • 357
  • 8
  • 18
  • There is no way to do this using the `comment` field in qt designer. It is [only meant to be used by translators](https://tsdgeos.blogspot.com/2011/05/qt-designer-texts-disambiguation-vs.html), and cannot be accessed programmatically. If you want to add extra properties to qt designer widgets, use [widget promotion](https://stackoverflow.com/a/42076247/984421). – ekhumoro Jul 08 '19 at 10:45

2 Answers2

1

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.

Green Cell
  • 4,677
  • 2
  • 18
  • 49
  • That's awesome! Sorry for the late reply, I got pulled onto something else. Ironically a task that involved creating object based meta subclassing (something I'd never done up to that point). I haven't had a chance to test this implementation yet, but I'll give the answer to you as this is most likely the way I should be resolving this issue :p Thanks! – Adam Sirrelle Jul 26 '19 at 02:34
0

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
Adam Sirrelle
  • 357
  • 8
  • 18