Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用类中的项目填充python qt小部件_Python_Qt - Fatal编程技术网

用类中的项目填充python qt小部件

用类中的项目填充python qt小部件,python,qt,Python,Qt,我在这里用python编写了一个脚本,它由我创建的名为“Profile”的类组成。每个配置文件都有一个“名称”和“插件名称”列表 我需要帮助获取列表以填充Ui。启动ui时,我希望dropdownlist中填充每个配置文件的“名称”。然后,当选择“概要文件”时,列表框将填充相应的插件名称。我已经注释掉了配置文件,因为我不知道如何正确地让它们工作 希望这是清楚的解释 import sys, os from PyQt4 import QtCore, QtGui class Example(Q

我在这里用python编写了一个脚本,它由我创建的名为“Profile”的类组成。每个配置文件都有一个“名称”和“插件名称”列表

我需要帮助获取列表以填充Ui。启动ui时,我希望dropdownlist中填充每个配置文件的“名称”。然后,当选择“概要文件”时,列表框将填充相应的插件名称。我已经注释掉了配置文件,因为我不知道如何正确地让它们工作

希望这是清楚的解释

import sys, os
from PyQt4 import QtCore, QtGui    

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    class Profile(object):
        def __init__(self, name, plugins):
            self.name = name
            self.plugins = plugins

    def initUI(self):

        # UI CONTORLS
        uiProfiles = QtGui.QComboBox(self)
        uiPluginList = QtGui.QListWidget(self)
        uiLaunch = QtGui.QPushButton("Launch")

        # STYLING
        uiLaunch.setToolTip('This is a <b>QPushButton</b> widget')
        uiLaunch.resize(uiLaunch.sizeHint())
        uiLaunch.setMinimumHeight(30)

        # UI LAYOUT
        grid = QtGui.QGridLayout()
        grid.setSpacing(10)
        grid.addWidget(uiProfiles, 1, 0)
        grid.addWidget(uiPluginList, 2, 0)
        grid.addWidget(uiLaunch, 3, 0)

        self.setLayout(grid) 
        self.setGeometry(300, 500, 600, 200)
        self.setWindowTitle('3ds Max Launcher')
        self.resize(400,150)
        self.show()


        # profiles = [
        #   Profile(name="3ds Max Workstation", plugins=["FumeFX", "Afterworks", "Multiscatter"]),
        #   Profile(name="3ds Max All Plugins", plugins=["FumeFX"]),
        #   Profile(name="3ds Max Lite", plugins=["default 3ds max"]),
        # ]

        # for p in profiles:
        #   uiProfiles.addItem(p.name)


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
导入系统,操作系统 从PyQt4导入QtCore、QtGui 类示例(QtGui.QWidget): 定义初始化(自): super(例如,self)。\uuuuu init\uuuuuuuu() self.initUI() 类配置文件(对象): 定义初始化(self、名称、插件): self.name=名称 self.plugins=插件 def initUI(self): #用户界面控件 uiProfiles=QtGui.QComboBox(自) uiPluginList=QtGui.QListWidget(self) uiLaunch=QtGui.QPushButton(“启动”) #造型 setToolTip('这是一个QPushButton小部件') uiLaunch.resize(uiLaunch.sizeHint()) uiLaunch.setMinimumHeight(30) #用户界面布局 grid=QtGui.QGridLayout() 网格设置间距(10) addWidget(uiProfiles,1,0) addWidget(uiPluginList,2,0) addWidget(uiLaunch,3,0) self.setLayout(网格) self.setGeometry(300500600200) self.setWindowTitle(“3ds Max启动器”) 自我调整大小(400150) self.show() #配置文件=[ #配置文件(name=“3ds Max Workstation”,插件=[“FumeFX”,“售后服务”,“Multiscatter”]), #配置文件(name=“3ds Max所有插件”,Plugins=[“FumeFX”]), #配置文件(name=“3ds Max Lite”,插件=[“默认3ds Max”]), # ] #对于配置文件中的p: #uiProfiles.addItem(p.name) def main(): app=QtGui.QApplication(sys.argv) ex=示例() sys.exit(app.exec_()) 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': main()
您遇到了一些问题。你的主窗口从未显示过。您是在示例类中定义Profile类(而不是自己定义)。当用户更改配置文件列表时,您也没有执行某些操作的事件函数

我把个人资料的名字放到一个文件夹里。这意味着对模型中名称的任何更改都将自动更新小部件。你不必这样做,但在更大的项目中,这样做更容易,实际上也不难

我还将一个函数连接到组合框值更改时发生的事件。您需要创建另一个事件函数,并将其连接到启动按钮事件

import sys, os
from PyQt4 import QtCore, QtGui    


class Profile(object):
    def __init__(self, name, plugins):
        self.name = name
        self.plugins = plugins


class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):

        self.profiles = [Profile(name="3ds Max Workstation", plugins=["FumeFX", "Afterworks", "Multiscatter"]),
                    Profile(name="3ds Max All Plugins", plugins=["FumeFX"]),
                    Profile(name="3ds Max Lite", plugins=["default 3ds max"])]

        profile_names = [p.name for p in self.profiles]

        # make a model to store the profiles data in
        # changes to data will automatically appear in the widget
        self.uiProfilesModel = QtGui.QStringListModel()
        self.uiProfilesModel.setStringList(profile_names)

        # UI CONTORLS
        self.uiProfiles = QtGui.QComboBox(self)
        self.uiPluginList = QtGui.QListWidget(self)
        self.uiLaunch = QtGui.QPushButton("Launch")

        # associate the model to the widget
        self.uiProfiles.setModel(self.uiProfilesModel)

        # connect signals
        self.uiProfiles.currentIndexChanged.connect(self.on_select_profile)

        # STYLING
        self.uiLaunch.setToolTip('This is a <b>QPushButton</b> widget')
        self.uiLaunch.resize(self.uiLaunch.sizeHint())
        self.uiLaunch.setMinimumHeight(30)

        # UI LAYOUT
        grid = QtGui.QGridLayout()
        grid.setSpacing(10)
        grid.addWidget(self.uiProfiles, 1, 0)
        grid.addWidget(self.uiPluginList, 2, 0)
        grid.addWidget(self.uiLaunch, 3, 0)

        self.setLayout(grid) 
        self.setGeometry(300, 500, 600, 200)
        self.setWindowTitle('3ds Max Launcher')
        self.resize(400,150)
        self.show()

        # run once to fill in list
        self.on_select_profile(0)

    def on_select_profile(self, index):

        # clear list
        self.uiPluginList.clear()

        # populate list        
        for plugin in self.profiles[index].plugins:
            self.uiPluginList.addItem(plugin)


if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())
导入系统,操作系统 从PyQt4导入QtCore、QtGui 类配置文件(对象): 定义初始化(self、名称、插件): self.name=名称 self.plugins=插件 类示例(QtGui.QWidget): 定义初始化(自): super(例如,self)。\uuuuu init\uuuuuuuu() self.initUI() def initUI(self): self.profiles=[Profile(name=“3ds Max Workstation”,插件=[“FumeFX”,“Afterworks”,“Multiscatter]”), 配置文件(name=“3ds Max所有插件”,Plugins=[“FumeFX”]), 配置文件(name=“3ds Max Lite”,插件=[“默认3ds Max”])] profile_name=[self.profiles中p的p.name] #创建一个模型以存储配置文件数据 #对数据的更改将自动显示在小部件中 self.uiProfilesModel=QtGui.QStringListModel() self.uiProfilesModel.setStringList(配置文件名称) #用户界面控件 self.uiProfiles=QtGui.QComboBox(self) self.uiPluginList=QtGui.QListWidget(self) self.uiLaunch=QtGui.QPushButton(“启动”) #将模型与小部件关联 self.uiProfiles.setModel(self.uiProfilesModel) #连接信号 self.uiProfiles.currentIndexChanged.connect(self.on\u选择\u配置文件) #造型 self.uiLaunch.setToolTip('这是一个QPushButton小部件') self.uiLaunch.resize(self.uiLaunch.sizeHint()) self.uiLaunch.setMinimumHeight(30) #用户界面布局 grid=QtGui.QGridLayout() 网格设置间距(10) grid.addWidget(self.uiProfiles,1,0) addWidget(self.uiPluginList,2,0) addWidget(self.uiLaunch,3,0) self.setLayout(网格) self.setGeometry(300500600200) self.setWindowTitle(“3ds Max启动器”) 自我调整大小(400150) self.show() #运行一次以填写列表 自我。在\u上选择\u配置文件(0) def on_select_profile(自我,索引): #清除列表 self.uiPluginList.clear() #填充列表 对于self.profiles[index]中的插件。插件: self.uiPluginList.addItem(插件) 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': app=QtGui.QApplication(sys.argv) ex=示例() 例如:show() sys.exit(app.exec_())
非常感谢您,这正是我想要的,我感谢您解释事情并确保对代码进行注释。非常感谢。