gpt4 book ai didi

python - 如何在我的类中继承QtWidgets.QWidget并使我的类类型-QtWidgets.QWidget?

转载 作者:行者123 更新时间:2023-12-01 01:05:02 27 4
gpt4 key购买 nike

我正在尝试在 Maya 中编写一些代码,如果这会在下面的脚本示例中造成任何困惑,我很抱歉。问题是我想制作我的自定义 PyQt 类,稍后将在我的布局中用作 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()

最佳答案

当您创建一个类时,您可以选择从另一个对象继承,在本例中可能是一个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% 自己完成,这可能很有趣,但也有很多额外的工作。

关于python - 如何在我的类中继承QtWidgets.QWidget并使我的类类型-QtWidgets.QWidget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55440770/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com