1

I make a simple window and added the menu and toolbar. And I got the strange behavior of the function connected to action. Here's the code:

import os
import sys
import sip
import maya.OpenMayaUI as mui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import uic

#----------------------------------------------------------------------
def getMayaWindow():
    ptr = mui.MQtUtil.mainWindow()
    return sip.wrapinstance(long(ptr), QObject)

#----------------------------------------------------------------------
class MainForm(QMainWindow):
    def __init__(self):
        super(MainForm, self).__init__(getMayaWindow())
        self.setGeometry(50,50,600,600)

        mdiArea = QMdiArea()
        self.setCentralWidget(mdiArea)

        self.testAction = QAction(QIcon('ico.png'), '&Test', self)        
        self.testAction.triggered.connect(self.aaaaa)

        self.menubar = self.menuBar()
        fileMenu = self.menubar.addMenu('&File')
        fileMenu.addAction(self.testAction)

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(self.testAction)

        self.statusBar()

    def aaaaa(self):
        print('test')

#----------------------------------------------------------------------
# window
def cacheWnd():
    wnd = MainForm()
    wnd.show()

cacheWnd()

when you press the menu or the toolbar icon - the associated function is not called. but it is necessary to make a global function outside the class and make a call without self how everything starts to work immediately.

i correct line:

self.testAction.triggered.connect(aaaaa)

and drag the function outside the class:

def aaaaa():
    print('test')

and all at once started, is called up and running...

Why it does not work until the function is a member of a class?

Massimo
  • 836
  • 3
  • 17
  • 38
  • If `aaaaa` is not a class method then `self.testAction.triggered.connect(self.aaaaa)` will not find or connect to it. Is that what you mean? – 101 Jul 22 '15 at 23:23
  • no! if aaaaa is a member of class - its not worked... – Massimo Jul 24 '15 at 06:58
  • the problem occurs only when the script runs in maya. if you make it an external application - just everything starts to work. – Massimo Jul 24 '15 at 09:29
  • if you try to run any other function of QMainWindow - then everything works fine. eg: self.testAction.triggered.connect(lambda: self.setWindowState(Qt.WindowMinimized)) – Massimo Jul 24 '15 at 09:31

0 Answers0