Reputation: 6147
I've written a script here in python which consists of a class I made called 'Profile'. Each profile has a 'Name' and list of 'Plugin Names'
I need help getting the list to populate the Ui. When the ui is initiated I want the dropdownlist to be populated with the 'Names' of each profile. 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()
Upvotes: 0
Views: 77
Reputation: 8999
You had a few problems. Your MainWindow never got shown. You were defining the Profile class inside your Example class (instead of on it's own). 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. 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_())
Upvotes: 2