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

时间:2014-11-26 21:42:01

标签: python qt

我在python中编写了一个脚本,其中包含一个名为' Profile'的类。每个个人资料都有一个名称'和'插件名称列表'

我需要帮助获取列表来填充Ui。当启动ui时,我希望下拉列表中包含“' Names'每个档案。然后作为'简介'如果选中,列表框将填充相应的插件名称。我已经评论了这些个人资料,因为我不确定如何正确地让他们工作。

希望这是明确的解释。

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

1 个答案:

答案 0 :(得分:2)

你遇到了一些问题。您的MainWindow从未上映过。您在Example类中定义了Profile类(而不是它自己的类)。当用户更改配置文件列表时,您也没有执行某些操作的事件功能。

我将个人资料名称放入QStringListModel。这意味着对模型中名称的任何更改都将自动更新窗口小部件。你不必这样做,但在大型项目中它更容易,而且实际上并不困难。

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

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