PDA

View Full Version : Dynamically change QComboBox delegate values ?



enter
17th March 2014, 14:23
Hi,

I'd like to know if it's possible to dynamically change a QComboBox delegate items list ?

If I set a delegate for Column 2 on a QTreeView this way :


self.view.setItemDelegateForColumn(2, ComboBoxDelegate(self, []))

How can I access and change values for all the ComboBox of column 2 ?

Ideally I would like to use a signal to do that, that way when another widget is populated I can dynamically change the ComboBox delegate content.

Thanks for your help

anda_skoa
17th March 2014, 15:31
Well, if "self" has a signal that indicates the new combo box values, then the delegate can connect all comboboxes that it creates to that signal.
With a combobox subclass that understands that signal, obviously.

Or you use a separate but shared model for the combobox content, then you only need to update that model's contents

Cheers,
_

enter
19th March 2014, 07:07
Well, the thing I don't know is : how do I connect a signal to all my ComboBox delegates ?

Imagine I have a ComboBoxDelegate class with a update_itemslist slot :



class ComboBoxDelegate(QItemDelegate):
def __init__(self, owner, itemslist):
QItemDelegate.__init__(self, owner)
self.itemslist = itemslist

@pyqtSlot(list)
def update_itemslist(self, itemslist):
editor.addItems(itemslist)


I would like to emit a signal from my middleware_ui_arbiter class defined below :



class middleware_ui_arbiter(QWidget):

# PyQT signals need to be declared outside __init__
signal = pyqtSignal(str)

def __init__(self, parent=None):
super(middleware_ui_arbiter, self).__init__(parent)

# Widget definition
self.view = QTreeView()

# Model definition
self.model = QStandardItemModel(0,ARBITER_NB_COLUMNS)

# Set delegates
self.view.setItemDelegateForColumn(1, ComboBoxDelegate(self, ["In", "Out"]))
self.view.setItemDelegateForColumn(2, ComboBoxDelegate(self, []))


Is there a way to connect a signal to, let's say, all the ComboBoxDelegate of column 2 ?

I imagine a call like this :



self.<something>.connect(self.<something>.update_itemslist)


The thing is I don't know what <something> is in the code beneath.

ChrisW67
19th March 2014, 07:49
There is only one delegate per column, so the question "Is there a way to connect a signal to, let's say, all the ComboBoxDelegate of column 2 ? " does not make sense.

When the delegate is asked for an editor you create a combo box and populate it with whatever items should be in the list at that time. If you want something external to send the current list items by signal then you give the delegate class a slot to receive the data and store it in the delegate object until it is required for an editor. Alternatively, give the delegate a pointer (or whatever the Python equivalent is) to another object that it can query to get the current items when required.

anda_skoa
19th March 2014, 08:43
Since your itemlist is probably just a list of strings, maybe try this:

- create an instance of QStringListModel
- pass this to the delegate
- let the delegate set this on each combobox editor it creates
- when you want to change the itemlist, set it as a new content on the string list model
- the model should notify all comboboxes it is currently set on

Cheers,
_

enter
19th March 2014, 12:05
Since your itemlist is probably just a list of strings, maybe try this:

- create an instance of QStringListModel
- pass this to the delegate
- let the delegate set this on each combobox editor it creates
- when you want to change the itemlist, set it as a new content on the string list model
- the model should notify all comboboxes it is currently set on

Cheers,
_

Thanks for the idea ! That's a brilliant idea, it worked for me.