[Pyside] Maya Style Popup menu cursor issue

Hello! I’m trying to do right click popup menus on buttons in maya so users know a button has a rigth click menu available.

Unfortunately when i set the custom cursor, I can’t seem to get it to allign with the original curser.

hotX, and hotY here seem to do nothing as far as I can see. When I move the cursor over the button it moves a little down and to the right no matter what values i provide here, whereas using the cmds.popupMenu on a button, the cursor image swap is seamless.

button.setCursor(QtGui.QCursor(':/rmbMenu.png', hotX=6, hotY=4))

full exmaple:


from PySide2 import QtCore, QtGui, QtWidgets

class MainWindow(QtWidgets.QDialog):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setWindowTitle("MainWindow")

        mainLayout = QtWidgets.QVBoxLayout()
        self.setLayout(mainLayout)

        self.btn = QtWidgets.QPushButton('Right Click Me!')
        mainLayout.addWidget(self.btn)

        self.btn.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.btn.customContextMenuRequested.connect(self.showPopup)
        self.btn.setCursor(QtGui.QCursor(':/rmbMenu.png', hotX=6, hotY=4))

        self.popupMenu = QtWidgets.QMenu()
        self.PBSaveFileCB = self.popupMenu.addAction("Click")

    def showPopup(self,position):
         self.popupMenu.exec_(self.btn.mapToGlobal(position))

def showUI():
    ui = MainWindow()
    ui.show()
    return ui

ui = showUI()

I can’t help with why hotX/hotY seems to do nothing, but as for why PySide and cmds buttons seem different is that the default cursors are different in each case. If you make a regular cmds UI and watch carefully as the cursor enters that UI, you will see the standard windows cursor changes slightly into a Maya-specific cursor image (leftArrow.png I think) - which doesn’t happen in your PySide example UI (the standard windows cursor remains throughout).

leftArrow.png and rmbMenu.png both have the same hotpoint offset (approx 11,8 from image top-left) which is presumably different to the standard windows cursor image.

Try setting the cursor to leftArrow.png for your whole UI and I think you won’t see this weird offset “pop” when changing to the rmb cursor icon.

Whether that helps you here or not I don’t know, but at least it explains the discrepancy you’re seeing.

1 Like

Thanks Nathan (big fan of googling stuff and ending up on your website btw) - This does indeed fix it popping, but does so but adding the problem of the hotspot not being on the cursor tip to both cursors, so but you can click the button while not technically being on it and also have your pointer on the button at the bottom edge but not be able to click it. Dunno why setting the hotspot just doesnt want to work.

There is clearly something weird going on in QCursor with hotX and hotY I don’t understand, because if I run your example code but modify the mainLayout part of __init__ to this:

mainLayout = QtWidgets.QVBoxLayout()
self.setLayout(mainLayout)
self.setCursor(QtGui.QCursor(':/leftArrow.png', hotX=11, hotY=8))

then also modify your button rmb cursor line to this (same hot values):

self.btn.setCursor(QtGui.QCursor(':/rmbMenu.png', hotX=11, hotY=8))

Then it doesn’t exhibit any popping, and the cursor hotspot almost exactly lines up correctly to the tip of the arrow where you would expect (x seems perfect, y is maybe 2 pixels off?).

However, what’s confusing me is:

  • this only works correctly the first time I run it after a cold Maya restart,
  • subsequent runs after changing either/all hot values don’t seem to affect it at all or work as expected
  • if I modify the hotY value to “correct” the 2 pixel offset (and restart Maya) the cursor starts popping weirdly again :face_with_raised_eyebrow:

I’m totally bemused as to what’s going on here, unless it’s just Maya dynamically fiddling with its own cursor values under the hood, which kinda seems likely I guess.

Yes i encountered some very strange behaviour when changing values and rerunning, and honestly, i just told myself i must be an idiot, somewhat glad it’s not just me, shame that I see little way forward though, thanks for looking into it.