Dynamically change QComboBox delegate values ?
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 :
Code:
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
Re: Dynamically change QComboBox delegate values ?
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,
_
Re: Dynamically change QComboBox delegate values ?
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 :
Code:
def __init__(self, owner, itemslist):
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 :
Code:
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
# Model definition
# 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 :
Code:
self.<something>.connect(self.<something>.update_itemslist)
The thing is I don't know what <something> is in the code beneath.
Re: Dynamically change QComboBox delegate values ?
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.
Re: Dynamically change QComboBox delegate values ?
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,
_
Re: Dynamically change QComboBox delegate values ?
Quote:
Originally Posted by
anda_skoa
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.