Search code examples
pythonmayapyside2

Replicating Maya QLineEdits with the color labels


I am trying to create/ replicate a QLineEdit widget with the ones that we see in Maya (where it has a color label on the left hand side that denotes if it is keyed or not)

I am able to replicate it to a certain extent, however having some issues at the same time:

  • See attached, between my icon and the left outer-most of the QLineEdit, there is a small gap
  • The icon size is not rectangular-ish as I would have expected despite the actual image is scaled in 10x16 pixels.

Here is a quick code sample of my QLineEdit:

my_icon = QtGui.QPixmap('full_keyed.png')
self.ui.positionXLineEdit.addAction(my_icon, QtWidgets.QLineEdit.LeadingPosition)

And so, is there a better for me to attain what I had wanted to achieve as seen in Maya?

enter image description here


Solution

  • By using widgetHierarchy.py you can find out that those "keyed" fields in the ChannelBox are part of a QTableView, and are therefore QStyledItemDelegates.

    I'm afraid I don't have personal experience working with those, but after a little digging it looks like those QStyledItemDelegates are what you want to model your controls after. Take a look at what prints out when querying the QStyledItemDelegates itemData():

    {0: u'Translate X', 2: u'Translate X', 3: u'Translate X', 6: <PySide2.QtGui.QFont( "smallPlainLabelFont,12,-1,5,50,0,0,0,0,0" )  at 0x7f8d2070ab48>, 7: 66, 8: <PySide2.QtGui.QBrush(QColor(ARGB 1, 0.266667, 0.266667, 0.266667),SolidPattern)  at 0x7f8d2070ab00>, 9: <PySide2.QtGui.QBrush(QColor(ARGB 1, 0.733333, 0.733333, 0.733333),SolidPattern)  at 0x7f8d2070ac68>}
    {0: u'0 ', 2: u'0 ', 3: u'0 \n\nKeyed On Frame', 6: <PySide2.QtGui.QFont( "smallPlainLabelFont,12,-1,5,50,0,0,0,0,0" )  at 0x7f8d2070ac68>, 7: 65, 8: <PySide2.QtGui.QBrush(QColor(ARGB 1, 0.803922, 0.152941, 0.160784),SolidPattern)  at 0x7f8d2070ab48>, 9: <PySide2.QtGui.QBrush(QColor(ARGB 1, 0, 0, 0.00784314),SolidPattern)  at 0x7f8d2070a3f8>}
    

    ChannelBox Exmaple

    The first line represents the itemData for the "Translate X" label and the second line fills out the actual input field AND the "keyed" marker.