Maya中的浮动UI,并且Button单击了connect,但是事件不起作用

问题描述 投票:0回答:1

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

[之后,我将窗口浮动并尝试单击,但是它不起作用。我想知道为什么它不起作用,以及如何解决它。

这是我的脚本:

谢谢!

  1. 开始脚本
import cfx_engine
reload(cfx_engine)
cfx_engine.start()
  1. 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'


  1. 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')



  1. 窗口图像

window image

python maya pyside2
1个回答
0
投票

如果将连接放入__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 __

中编写连接函数
© www.soinside.com 2019 - 2024. All rights reserved.