我正在尝试在Maya中编写一些代码,如果在下面的脚本示例中造成任何混淆,我感到抱歉。 问题是我想创建自己的自定义PyQt类,该类以后将在我的Layout中用作Widget,但是我对此事缺乏知识,导致错误
# RuntimeError: '__init__' method of object's base class (test) not called. #
我读到的关于该错误的主题很少,人们建议使用super()进行修复。 我试图放入“ super()”,然后出现此错误:
# TypeError: PySide2.QtCore.QObject isn't a direct base class of test #
有人可以帮我清除这个东西吗?
from PySide2 import QtGui, QtCore, QtWidgets
from shiboken2 import wrapInstance
import maya.OpenMaya as om
import maya.OpenMayaUI as omui
import maya.cmds as cmds
import os, functools
def getMayaWindow():
pointer = omui.MQtUtil.mainWindow()
if pointer is not None:
return wrapInstance(long(pointer), QtWidgets.QWidget)
class testUi():
def __init__(self):
self.window = 'vl_test'
self.title = 'Test Remastered'
self.size = (1000, 650)
def create(self):
if cmds.window(self.window, exists=True):
cmds.deleteUI(self.window, window=True)
self.parentWindow = getMayaWindow()
self.mainWindow = QtWidgets.QMainWindow(self.parentWindow)
self.mainWindow.setObjectName(self.window)
self.mainWindow.setWindowTitle(self.title)
self.mainWidget = QtWidgets.QWidget()
self.mainWindow.setCentralWidget(self.mainWidget)
self.mainLayout = QtWidgets.QFormLayout(self.mainWidget)
testik = test(self)
self.mainLayout.addWidget(testik)
self.mainButton = QtWidgets.QPushButton()
self.mainLayout.addWidget(self.mainButton)
self.mainButton.clicked.connect(partial(self.load_selected))
class test(QtWidgets.QWidget):
def __init__(self, parent=None, maya_transform_nodes=[]):
#super(QtWidgets.QWidget, self ).__init__(parent)
qWid = QtWidgets.QWidget()
self.setMinW = qWid.setMinimumWidth(300)
self.setMinH = qWid.setMinimumHeight(300)
self.sss = qWid.setStyleSheet("border:1px solid rgb(0, 255, 0); ")
self.items = []
self.transform_move = QtGui.QTransform()
self.transform_scale = QtGui.QTransform()
self.prev_mouse_pos = QtCore.QPoint(0, 0)
self.color = QtGui.QColor(0, 255, 50, 50)
self.clicked_color = QtGui.QColor(0, 255, 50, 150)
self.right_clicked_color = QtGui.QColor(255, 0, 0, 150)
v = testUi()
v.create()
答案 0 :(得分:2)
创建类时,可以选择从另一个对象继承,在这种情况下,可能是QDialog
,因为它是工具的主窗口。但是,仅继承它是不够的,您必须调用QDialog
的构造函数才能使其正确初始化,并且可以使用super
进行此操作。
使用super
,您可以选择从继承自您的类的对象中运行方法。看起来像这样:super(YOUR_CLASS_NAME, INSTANCE).METHOD_TO_RUN(PARAMETERS_TO_PASS)
。因此,更具体地说,要运行__init__
,它将是:super(testUi, self).__init__(parent)
,它将对其进行修复。
这可能是一个糟糕的解释,因此请使用Google super
以获得更好的示例。
我对您的脚本进行了一些编辑以简化该脚本,并在其中留下了一些评论:
from PySide2 import QtGui, QtCore, QtWidgets
from shiboken2 import wrapInstance
import maya.OpenMaya as om
import maya.OpenMayaUI as omui
import maya.cmds as cmds
import os, functools
def getMayaWindow():
pointer = omui.MQtUtil.mainWindow()
if pointer is not None:
return wrapInstance(long(pointer), QtWidgets.QWidget)
class testUi(QtWidgets.QDialog): # Need to inherit from a `QObject`
def __init__(self, parent=None): # It's typical to include the `parent` parameter and pass that through `super`.
# If nothing was passed for parent, default to Maya's main window.
if parent is None:
parent = getMayaWindow()
super(testUi, self).__init__(parent) # Call `super` with the class's name, testUi, and its instance, self.
self.window = 'vl_test'
self.title = 'Test Remastered'
self.size = (1000, 650)
self.create() # Just automatically call create in the instance's constructor.
def create(self):
if cmds.window(self.window, exists=True):
cmds.deleteUI(self.window, window=True)
self.setWindowTitle(self.title)
self.resize(QtCore.QSize(*self.size))
self.testik = test(self) # Add `self.` so it belongs to this instance.
self.mainButton = QtWidgets.QPushButton()
self.mainLayout = QtWidgets.QVBoxLayout() # QVBoxLayout seems to give a better result.
self.mainLayout.addWidget(self.testik)
self.mainLayout.addWidget(self.mainButton)
self.setLayout(self.mainLayout) # Don't forget to make this widget actually set to this layout and use it.
class test(QtWidgets.QFrame):
def __init__(self, parent=None, maya_transform_nodes=[]):
super(test, self).__init__(parent) # Call `super` with the class's name, test, and its instance, self.
self.setMinimumWidth(300)
self.setMinimumHeight(300)
self.setStyleSheet("QWidget {border: 1px solid rgb(0, 255, 0)};")
self.items = []
self.transform_move = QtGui.QTransform()
self.transform_scale = QtGui.QTransform()
self.prev_mouse_pos = QtCore.QPoint(0, 0)
self.color = QtGui.QColor(0, 255, 50, 50)
self.clicked_color = QtGui.QColor(0, 255, 50, 150)
self.right_clicked_color = QtGui.QColor(255, 0, 0, 150)
v = testUi()
v.show() # Call show to show it!
我是否可以假设您正在尝试建立概要?如果是,则可能要签出QGraphicsView
以便在窗口中填充项目并处理大多数逻辑。否则,您将必须100%自己做,这可能很有趣,但是还有很多额外的工作。