pyqt获取实例小部件工作

时间:2018-02-08 19:52:31

标签: python pyqt4

我正在尝试创建一个gui,让用户在gui中创建预设布局的实例,如下所示。我正在努力的部分是当我点击所选按钮时,它将所选属性添加到最后添加的小部件而不是所选的小部件。我不确定我是否正确分类或者我正在进行的方式是否正确。我添加了一个说明,指出我认为问题所在。有人可以帮我解决这个问题。谢谢!

编辑。 旁注,我在玛雅这样做,但这不是我面临的玛雅问题。

import maya.cmds as mc
from PyQt4 import QtCore,QtGui

dialog = None
class mainWindow(QtGui.QDialog):
    instance_count = 0
    def __init__(self):
        super(mainWindow,self).__init__()
        mainWindow.instance_count+=1
        self.instance = mainWindow.instance_count
        self.setWindowTitle("Create XML File")
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
        self.setMinimumHeight(300)
        self.setFixedWidth(500)
        self.mainWindowLayout = QtGui.QVBoxLayout()
        self.setLayout(self.mainWindowLayout)
        self.layout().setAlignment(QtCore.Qt.AlignTop)

        self.basic_widget = QtGui.QWidget()
        self.basic_widget.setLayout(QtGui.QVBoxLayout())
        self.layout().addWidget(self.basic_widget)
        self.addAttributeButton = QtGui.QPushButton("Add")        
        self.basic_widget.layout().addWidget(self.addAttributeButton)

    def attributeLayout(self):
        self.basic_layout = QtGui.QHBoxLayout()
        self.attribute = QtGui.QLineEdit()
        self.attributeButton = QtGui.QPushButton("Selected")
        self.basic_layout.addWidget(self.attribute)
        self.basic_layout.addWidget(self.attributeButton)
    def addSelectedAttribute(self,instance):
        objectSelected = mc.ls(sl=True)
        channelSelected = mc.channelBox('mainChannelBox', query=True, selectedMainAttributes=True)
        if (len(objectSelected) !=1):
            mc.error('Please select only one object...')
            return
        if (len(channelSelected) !=1):
            mc.error('Please select only one attribute in the channel box...')
            return
        longName = mc.attributeName('{}.{}'.format(objectSelected[0],channelSelected[0]),long=True)
        self.attribute[instance].setText('{}.{}'.format(objectSelected[0],longName)) # this is where I'm struggling

class MainSubWindow(mainWindow):
    new_widget = {}
    def __init__(self):
        super(MainSubWindow,self).__init__()
        self.addAttributeButton.clicked.connect(self.addAttributeLayout)

    def addAttributeLayout(self):       
        self.new_widget[self.instance] = mainWindow()
        self.new_widget[self.instance].attributeLayout()
        self.basic_widget.layout().addLayout(self.new_widget[self.instance].basic_layout)
        self.new_widget[self.instance].attributeButton.clicked.connect(lambda: self.addSelectedAttribute(self.instance)) # and struggling here too

def create():
    global dialog
    if dialog is None:
        dialog = MainSubWindow()
        dialog.show()

def delete():
    global dialog
    if dialog is None:
        return
    dialog.deleteLater()
    dialog = None

1 个答案:

答案 0 :(得分:0)

所以在得到其他地方的一些帮助并挖掘了一下之后,我意识到我完全错了,因为我怀疑。如果有人好奇这就是我修复它的方式

import maya.cmds as mc
from PyQt4 import QtCore,QtGui

dialog = None
class mainWindow(QtGui.QDialog):
    instance_count = 0
    new_widget = {}
    def __init__(self):
        super(mainWindow,self).__init__()        
        self.setWindowTitle("Create XML File")
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
        self.setMinimumHeight(300)
        self.setFixedWidth(500)
        self.mainWindowLayout = QtGui.QVBoxLayout()
        self.setLayout(self.mainWindowLayout)
        self.layout().setAlignment(QtCore.Qt.AlignTop)

        self.basic_widget = QtGui.QWidget()
        self.basic_widget.setLayout(QtGui.QVBoxLayout())
        self.layout().addWidget(self.basic_widget)
        self.addAttributeButton = QtGui.QPushButton("Add")        
        self.basic_widget.layout().addWidget(self.addAttributeButton)
        self.addAttributeButton.clicked.connect(self.addAttributeLayout)

    def addAttributeLayout(self):       
        self.new_widget[self.instance_count] = MainSubWindow()
        self.new_widget[self.instance_count].attributeLayout()
        self.basic_widget.layout().addLayout(self.new_widget[self.instance_count].basic_layout)
        self.instance_count+=1

class MainSubWindow(mainWindow):
    instance_count = 0
    def __init__(self):
        super(MainSubWindow,self).__init__()

    def attributeLayout(self):
        self.basic_layout = QtGui.QHBoxLayout()
        self.attr = QtGui.QLineEdit()
        self.attributeButton = QtGui.QPushButton("Selected")
        self.basic_layout.addWidget(self.attr)
        self.basic_layout.addWidget(self.attributeButton)
        self.attributeButton.clicked.connect(self.addSelectedAttribute) # and/or struggling here too

    def addSelectedAttribute(self):
        objectSelected = mc.ls(sl=True)
        channelSelected = mc.channelBox('mainChannelBox', query=True, selectedMainAttributes=True)
        if (len(objectSelected) !=1):
            mc.error('Please select only one object...')
            return
        if (len(channelSelected) !=1):
            mc.error('Please select only one attribute in the channel box...')
            return
        longName = mc.attributeName('{}.{}'.format(objectSelected[0],channelSelected[0]),long=True)
        self.attr.setText('{}.{}'.format(objectSelected[0],longName)) # this is where I'm struggling






def create():
    global dialog
    if dialog is None:
        dialog = MainSubWindow()
        dialog.show()

def delete():
    global dialog
    if dialog is None:
        return
    dialog.deleteLater()
    dialog = None