Issue
from PySide2 import QtGui,QtCore,QtWidgets
from PySide2.QtGui import*
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from shiboken2 import wrapInstance
import maya.OpenMayaUI as mui
import sys
class ui(QWidget):
def __init__(self,parent):
super(ui,self).__init__(parent)
self.resize(300,500)
self.mainWindow = QtWidgets.QMainWindow(parent)
self.setupUI(self.mainWindow)
self.setFocus()
def setupUI(self,mainWindow):
mymainWindow = QWidget(mainWindow)
mymainWindow.resize(300,500)
def mousePressEvent(self,e):
print 'sdfasdf'
if e.button()==Qt.RightButton:
print "Clickkkk"
def Show(self):
self.mainWindow.show()
class app():
def __init__(self):
self.ptr = mui.MQtUtil.mainWindow()
self.ptr = wrapInstance(long(self.ptr),QtWidgets.QWidget)
self.ui = ui(self.ptr)
def runApp(self):
self.ui.Show()
self.ui.setFocus()
tt = app()
tt.runApp()
Here is the code I'm testing on. After using wrapInstance
the mouseEvent
are no longer working.
But if I didn't wrap it it's work
not working
class app():
def __init__(self):
self.ptr = mui.MQtUtil.mainWindow()
self.ptr = wrapInstance(long(self.ptr),QtWidgets.QWidget)
self.ui = ui(self.ptr)
def runApp(self):
self.ui.Show()
self.ui.setFocus()
working
I Also change some parent structure in UI class
class app():
def __init__(self):
self.ui = ui()
def runApp(self):
self.ui.Show()
Can anyone explain why the MouseEvent
won't work after I wrap it? And how to make it work?
Solution
The crux of the problem is this: self.ui.Show()
. This runs your custom method, which in turn runs this self.mainWindow.show()
. This causes self.mainWindow
to show, but you subclassed mousePressEvent
for ui
, not self.mainWindow
! So it's not running the event because you're clicking on the wrong widget.
Instead, since ui
is a QWidget
, call self.ui.show()
. You may also have to put self.setWindowFlags(QtCore.Qt.Window)
in ui
's constructor. With this the mouse event runs as expected when the user clicks on it.
Some side notes:
I doubt that you actually want to create a QMainWindow
in a QWidget
. It just strikes as odd. Consider sub-classing a QMainWindow
instead as it should be the 'top' widget.
Also try to avoid importing modules like from PySide2.QtCore import *
, and import them like this instead from PySide2 import QtCore
. It's bad practice, pollutes your module's scope, and makes the code much more unreadable/un-maintainable since it's hard to traceback where these variables come from.
Oh, and for the love of god, use some vertical white-spacing :)
Answered By - Green Cell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.