【问题标题】:How to get the Maya/Qt GUI component that is calling a function?如何获取调用函数的 Maya/Qt GUI 组件?
【发布时间】:2015-12-06 07:44:53
【问题描述】:

我试图找出一种方法来以某种方式“获取”正在调用函数的 GUI 组件。通过这种方式,我可以进一步将我的代码整合到执行类似任务的组件的可重用部分中。我需要一种在 Maya 的 GUI 命令和 Qt 中执行此操作的方法。我想我正在寻找的是一个通用的 Python 技巧,如“init”、“file”、“ma​​in”等。如果没有通用的 python 方法,也欢迎任何 Maya/Qt 特定技巧。

这是一些任意伪代码,可以更好地解释我在寻找什么:

field1 = floatSlider(changeCommand=myFunction)
field2 = colorSlider(changeCommand=myFunction)

def myFunction(*args):
    get the component that called this function

    if the component is a floatSlider
        get component's value
        do the rest of the stuff

    elif the component is a colorSlider
        get component's color
        do the rest of the stuff

【问题讨论】:

  • 在 Qt 中,有 Signals 和 Slots,并且有一个 QObject.sender() 方法返回调用的 GUI 元素。

标签: python qt user-interface pyside maya


【解决方案1】:

从 Gombat 的评论中扩展而来,这里有一个如何让通用函数与滑块和 spinBox 控件一起使用的示例:

from PySide import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self, parent = None):
        super(Window, self).__init__(parent)

        # Create a slider
        self.floatSlider = QtGui.QSlider()
        self.floatSlider.setObjectName('floatSlider')
        self.floatSlider.valueChanged.connect(self.myFunction)

        # Create a spinbox
        self.colorSpinBox = QtGui.QSpinBox()
        self.colorSpinBox.setObjectName('colorSlider')
        self.colorSpinBox.valueChanged.connect(self.myFunction)

        # Create widget's layout
        mainLayout = QtGui.QHBoxLayout()
        mainLayout.addWidget(self.floatSlider)
        mainLayout.addWidget(self.colorSpinBox)
        self.setLayout(mainLayout)

        # Resize widget and show it
        self.resize(300, 300)
        self.show()

    def myFunction(self):
        # Getting current control calling this function with self.sender()
        # Print out the control's internal name, its type, and its value
        print "{0}: type {1}, value {2}".format( self.sender().objectName(), type( self.sender() ), self.sender().value() )

win = Window()

我不知道您想要什么控件 colorSlider(我认为 PySide 的滑块与 Maya 中的滑块不同,您可能需要自定义它或使用 QColorDialog)。但这应该让您大致了解如何去做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    • 2020-12-03
    • 1970-01-01
    • 2013-04-01
    • 2019-09-12
    • 2012-02-25
    • 1970-01-01
    相关资源
    最近更新 更多