简体   繁体   English

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

[英]populate python qt widget with items from class

I've written a script here in python which consists of a class I made called 'Profile'. 我在这里用python编写了一个脚本,该脚本包含我创建的名为“ Profile”的类。 Each profile has a 'Name' and list of 'Plugin Names' 每个配置文件都有一个“名称”和“插件名称”列表

I need help getting the list to populate the Ui. 我需要帮助以获取列表来填充Ui。 When the ui is initiated I want the dropdownlist to be populated with the 'Names' of each profile. 启动ui时,我希望在下拉列表中填充每个配置文件的“名称”。 Then as the 'Profile' is selected, the listbox be populate with the appropriate plugin names. 然后,在选择“配置文件”时,将使用适当的插件名称填充列表框。 I've commented out the profiles as I wasn't sure how to properly get them working. 我不确定个人资料的工作方式,因此已将个人资料注释掉。

Hope that is clear explaining. 希望那是清楚的解释。

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()

You had a few problems. 您遇到了一些问题。 Your MainWindow never got shown. 您的MainWindow从未显示。 You were defining the Profile class inside your Example class (instead of on it's own). 您是在Example类中定义Profile类(而不是单独定义)。 You also had no event function that did something when the user changed the profile list. 当用户更改配置文件列表时,您也没有事件功能可以执行任何操作。

I made put the profile names into a QStringListModel . 我将配置文件名称放入QStringListModel中 This means that any changes to the names in the model will automatically update the widget. 这意味着对模型中名称的任何更改将自动更新小部件。 You don't have to do it this way, but it's easier in larger projects and not really any harder to do. 您不必这样做,但是在大型项目中它更容易,而且实际上也没有任何困难。

I also connected a function to the event that occurs when the value of the combo box is changed. 我还将一个函数连接到更改组合框的值时发生的事件。 You will need to make another event function and connect it to a launch button event as well. 您将需要执行另一个事件功能,并将其连接到启动按钮事件。

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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM