Maya 中的浮动 UI,并且按钮单击连接,但事件不起作用

Posted

技术标签:

【中文标题】Maya 中的浮动 UI,并且按钮单击连接,但事件不起作用【英文标题】:Floating UI in Maya, and Button clicked connect, but the event not working 【发布时间】:2019-12-11 09:11:23 【问题描述】:

就我而言,我尝试在 Maya 中浮动 QMainWindow。 在那个窗口中,有三个按钮,我将事件连接到我的函数。

之后,我浮动窗口并尝试单击,但它不起作用。我想知道它不工作的原因,以及如何解决它。

这是我的脚本:

谢谢!

    启动脚本
import cfx_engine
reload(cfx_engine)
cfx_engine.start()
    cfx_engine 类
import PySide2.QtCore as QtCore
import PySide2.QtGui as QtGuiOrig
import PySide2.QtWidgets as QtGui
import maya.mel as mel
import cfx_main_view
reload(cfx_main_view)
import cfx_window_adaptor
reload(cfx_window_adaptor)

import cfx_pathInfo_util
reload(cfx_pathInfo_util)

import scn
import maya.cmds as cmds

class CFXController():
    _object_name = 'CFX_WINDOW'
    _ui = None
    _parent_window = None
    # _main_parent_window= None
    def __init__(self):

        # check to see if the UI exists, if so delete it
        if cmds.window(self._object_name, exists=True):
            cmds.deleteUI(self._object_name, wnd=True)

        self._parent_window = cfx_window_adaptor._maya_main_window()
        self._main_parent_window = QtGui.QMainWindow(self._parent_window)

        self._ui = cfx_main_view.Ui_MainWindow()
        self._ui.setupUi(self._main_parent_window, self._object_name)
        # self.set_link()



    def start_(self):
        self._main_parent_window.show()



    def set_link(self):
        print 'set link'
        self._ui.cloth_cache_bake_btn.clicked.connect(self.bake_ncache)


    def bake_ncache(self):
        print 'bake nCache'


    cfx_main_view 类
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 
#
# Created: Tue Dec 10 12:48:51 2019
#      by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost!
try:
    from PyQt4 import QtCore, QtGui
except:
    import PySide2.QtCore as QtCore
    import PySide2.QtGui as QtGuiOrig
    import PySide2.QtWidgets as QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def __init__(self):
        # super(Ui_MainWindow,self).__init__(_parent_window)
        self.style_component = 'background_color': '#333333',
                                'border_color': '#595959',
                                'font_color':'#D9D9D9',
                                'font_color_pressed': '#595959',
                                'button_color': 'rgba(70,70,70,0.5)'
        # self.setupUi(self)
        pass

    def setupUi(self, MainWindow, _object_name):
        MainWindow.setObjectName(_fromUtf8(_object_name))
        MainWindow.resize(576, 371)
        MainWindow.setStyleSheet("background : "+self.style_component['background_color']+";")

        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.verticalLayout_2 = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
        self.cloth_main_vl = QtGui.QVBoxLayout()
        self.cloth_main_vl.setObjectName(_fromUtf8("cloth_main_vl"))

        self.cloth_cache_view_lw = QtGui.QListWidget(self.centralwidget)
        self.cloth_cache_view_lw.setObjectName(_fromUtf8("cloth_cache_view_lw"))
        self.cloth_cache_view_lw.setVerticalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
        self.cloth_cache_view_lw.setHorizontalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
        self.cloth_cache_view_lw.setFocusPolicy(QtCore.Qt.NoFocus)
        self.cloth_cache_view_lw.setStyleSheet(
                                        "QListWidget"+\
                                        "border-style: solid;"+\
                                        "border-width : 0.5px;"+\
                                        "border-color: "+ self.style_component['border_color'] +";"+\
                                        "border-radius: 1px;"+\
                                        "color : "+ self.style_component['font_color'] +";"+\
                                        "background:"+self.style_component['background_color']+";"+\
                                        "font: 12px;"+\
                                        ""+\
                                        "QListWidget::item"+\
                                        "border-bottom: 1px solid #303030;"+\
                                        "color:"+ self.style_component['font_color'] +";"+\
                                        "height : 16.5px;"+\
                                        ""+\
                                        "QListWidget::branch:hover"+\
                                        "background: #363636;"+\
                                        ""+\
                                        "QListWidget::branch:selected"+\
                                        "background: #363636;"+\
                                        ""+\
                                        "QListWidget::item:hover "+\
                                        "color: "+ self.style_component['font_color'] +";"+\
                                        "background: #363636;"+\
                                        ""+\
                                        "QListWidget::item:selected"+\
                                        "background: #595959;"+\
                                        "color:"+ self.style_component['font_color'] +";"+\
                                        ""+\
                                        "QScrollBar:vertical "+\
                                        "width: 10px;"+\
                                        "margin: 20px 0 3px 0;"+\
                                        "border-radius: 5px;"+\
                                      ""+\
                                      "QScrollBar::handle:vertical "+\
                                        "background:" + self.style_component['font_color'] +";"+\
                                        "min-height: 5px;"+\
                                        "width : 10px;"
                                        "border-radius: 5px;"+\
                                      ""+\
                                      "QScrollBar::add-line:vertical "+\
                                        "background: none;"+\
                                        "height: 45px;"+\
                                        "subcontrol-position: bottom;"+\
                                        "subcontrol-origin: margin;"+\
                                      ""+\
                                      "QScrollBar::sub-line:vertical "+\
                                        "background: none;"+\
                                        "height: 45px;"+\
                                        "subcontrol-position: top;"+\
                                        "subcontrol-origin: margin;"+\
                                      ""
                                      )
        self.cloth_main_vl.addWidget(self.cloth_cache_view_lw)

        self.cloth_btn_hl = QtGui.QHBoxLayout()
        self.cloth_btn_hl.setObjectName(_fromUtf8("cloth_btn_hl"))
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.cloth_btn_hl.addItem(spacerItem)

        self.cloth_cache_bake_btn = QtGui.QPushButton(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.cloth_cache_bake_btn.sizePolicy().hasHeightForWidth())
        self.cloth_cache_bake_btn.setSizePolicy(sizePolicy)
        self.cloth_cache_bake_btn.setObjectName(_fromUtf8("cloth_cache_bake_btn"))
        self.cloth_cache_bake_btn.setStyleSheet(
                                                "QPushButton"+\
                                                "font: 13px ;"+\
                                                "border: 1px solid;"+\
                                                "border-color:"+ self.style_component['border_color']+";"+\
                                                "border-radius: 3px;"+\
                                                "background-color:"+self.style_component['button_color']+";"+\
                                                "color:"+ self.style_component['font_color'] +";"+\
                                                ""+\
                                                "QPushButton:pressed"+\
                                                "color:"+ self.style_component['font_color_pressed'] +";"+\
                                                "border-color:" +self.style_component['background_color']+";"+\
                                                "border-radius: 3px;"+\
                                                "background-color:" +self.style_component['font_color']+";"+\
                                                "")
        self.cloth_btn_hl.addWidget(self.cloth_cache_bake_btn)

        self.cloth_select_btn = QtGui.QPushButton(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.cloth_select_btn.sizePolicy().hasHeightForWidth())
        self.cloth_select_btn.setSizePolicy(sizePolicy)
        self.cloth_select_btn.setObjectName(_fromUtf8("cloth_select_btn"))
        self.cloth_select_btn.setStyleSheet(
                                                "QPushButton"+\
                                                "font: 13px ;"+\
                                                "border: 1px solid;"+\
                                                "border-color:"+ self.style_component['border_color']+";"+\
                                                "border-radius: 3px;"+\
                                                "background-color:"+self.style_component['button_color']+";"+\
                                                "color:"+ self.style_component['font_color'] +";"+\
                                                ""+\
                                                "QPushButton:pressed"+\
                                                "color:"+ self.style_component['font_color_pressed'] +";"+\
                                                "border-color:" +self.style_component['background_color']+";"+\
                                                "border-radius: 3px;"+\
                                                "background-color:" +self.style_component['font_color']+";"+\
                                                "")
        self.cloth_btn_hl.addWidget(self.cloth_select_btn)

        self.cloth_abc_pub_btn = QtGui.QPushButton(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.cloth_abc_pub_btn.sizePolicy().hasHeightForWidth())
        self.cloth_abc_pub_btn.setSizePolicy(sizePolicy)
        self.cloth_abc_pub_btn.setObjectName(_fromUtf8("cloth_abc_pub_btn"))
        self.cloth_abc_pub_btn.setStyleSheet(
                                                "QPushButton"+\
                                                "font: 13px ;"+\
                                                "border: 1px solid;"+\
                                                "border-color:"+ self.style_component['border_color']+";"+\
                                                "border-radius: 3px;"+\
                                                "background-color:"+self.style_component['button_color']+";"+\
                                                "color:"+ self.style_component['font_color'] +";"+\
                                                ""+\
                                                "QPushButton:pressed"+\
                                                "color:"+ self.style_component['font_color_pressed'] +";"+\
                                                "border-color:" +self.style_component['background_color']+";"+\
                                                "border-radius: 3px;"+\
                                                "background-color:" +self.style_component['font_color']+";"+\
                                                "")
        self.cloth_btn_hl.addWidget(self.cloth_abc_pub_btn)

        spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.cloth_btn_hl.addItem(spacerItem1)
        self.cloth_btn_hl.setStretch(0, 1)
        self.cloth_btn_hl.setStretch(1, 2)
        self.cloth_btn_hl.setStretch(2, 2)
        self.cloth_btn_hl.setStretch(3, 2)
        self.cloth_btn_hl.setStretch(4, 1)
        self.cloth_main_vl.addLayout(self.cloth_btn_hl)
        self.cloth_main_vl.setStretch(0, 10)
        self.cloth_main_vl.setStretch(1, 1)
        self.verticalLayout_2.addLayout(self.cloth_main_vl)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.set_link()

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.cloth_cache_bake_btn.setText(_translate("MainWindow", "nCache bake", None))
        self.cloth_select_btn.setText(_translate("MainWindow", "Select Cache", None))
        self.cloth_abc_pub_btn.setText(_translate("MainWindow", "Alembic Pub", None))

    def set_link(self):
        print 111
        self.cloth_cache_bake_btn.clicked.connect(self.prinrpo)

    def prinrpo(self):
        self.cloth_cache_bake_btn.setText('a')



    窗口图片

window image

【问题讨论】:

【参考方案1】:

如果你把你的连接放入 __init__,它会正常工作:

def __init__(self):

    # check to see if the UI exists, if so delete it
    if cmds.window(self._object_name, exists=True):
        cmds.deleteUI(self._object_name, wnd=True)

    self._parent_window = maya_main_window()
    self._main_parent_window = QtGui.QMainWindow(self._parent_window)

    self._ui = Ui_MainWindow()
    self._ui.setupUi(self._main_parent_window, self._object_name)

    self._ui.cloth_cache_bake_btn.clicked.connect(self.bake_ncache)

请注意,它仍会从您的 setLink 触发先前的连接并更改文本。我不知道为什么,我总是在 __init__ 中编写连接函数

【讨论】:

以上是关于Maya 中的浮动 UI,并且按钮单击连接,但事件不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Semantic-UI 避免某些按钮单击事件的表单验证触发

Maya 2015-自定义货架按钮缺少图标

从角度 ui 引导模式中单击时如何捕获背景单击事件?

OnClick 触发另一个按钮单击事件

什么时候使用事件和命令为WPF / MVVM

如何在 Swift 中的 UITableViewCell 上添加带有单击事件的按钮?