2

I'm trying to make a tool window for Maya, in which I can right-click anywhere, and if I click 'add', a rectangle widget shows up at my cursor position.

Now my right-click functionality works. I can also get my cursor position in addPicker() function. But I am having problem with placing newly-created widgets. If I add a layout and add the newly-created widgets to it, they actually show up. However, if I didn't create a layout for those widgets, no matter what position I tested, nothing shows up in my window.

Hopefully someone has some ideas. Thank you all in advance.

A right-click screenshot: A right-click screenshot

class RightClickMenu(QtGui.QMenu):

    def __init__(self, *args, **kwargs):
        super(RightClickMenu, self).__init__(*args)

        self.parentWidget().setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.parentWidget().customContextMenuRequested.connect(self.menuPos)

    def menuPos(self, *args):
        self.exec_(QtGui.QCursor.pos())

class Ui_window(object):
    def setupUi(self, window):
        window.setObjectName("window")
        window.resize(555, 900)
        self.widget_base = QtGui.QWidget()

        self.verticalLayout_window = QtGui.QVBoxLayout(window)
        self.verticalLayout_window.addWidget(self.widget_base)

        self.menu_popup = RightClickMenu(self.widget_base)
        self.menu_popup.setObjectName("popupMenu")
        self.verticalLayout_widget = QtGui.QVBoxLayout(self.widget_base)

        # Action - add picker
        addAction = QtGui.QAction('Add Picker', self.widget_base)
        addAction.setShortcut('Ctrl+A')
        addAction.setStatusTip('Add Picker')
        addAction.triggered.connect(self.addPicker)
        self.menu_popup.addAction(addAction)

        # Action - delete picker
        deleteAction = QtGui.QAction('Delete Picker', self.widget_base)
        deleteAction.setShortcut('Ctrl+D')
        deleteAction.setStatusTip('Delete Picker')
        deleteAction.triggered.connect(self.deletePicker)
        self.menu_popup.addAction(deleteAction)

    def addPicker(self):
        cursorPos = QtGui.QCursor.pos()
        localPos = self.widget_base.mapFromGlobal(cursorPos)
######################################################################
        # how??? below doesn't work.
        self.pushButton = QtGui.QPushButton(self.widget_base)
        self.pushButton.setGeometry(QtCore.QRect(220, 50, 75, 23))
        self.pushButton.setObjectName("pushButton")

    def deletePicker(self):
        print 'delete'



def run():
    import sys
    try:
        Ui_window.close()
    except:
        pass

    pickerWindow = QtGui.QDialog()
    ui = Ui_window()
    ui.setupUi(pickerWindow)
    pickerWindow.show()
    pickerWindow.exec_()
Richard Erickson
  • 2,568
  • 8
  • 26
  • 39

1 Answers1

1

Surprising solution (see this question):

self.pushButton.show()
Community
  • 1
  • 1
Alexander Lutsenko
  • 2,130
  • 8
  • 14