Results 1 to 6 of 6

Thread: Dynamically change QComboBox delegate values ?

  1. #1
    Join Date
    Mar 2014
    Posts
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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 :

    Qt Code:
    1. self.view.setItemDelegateForColumn(2, ComboBoxDelegate(self, []))
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Mar 2014
    Posts
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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 :

    Qt Code:
    1. class ComboBoxDelegate(QItemDelegate):
    2. def __init__(self, owner, itemslist):
    3. QItemDelegate.__init__(self, owner)
    4. self.itemslist = itemslist
    5.  
    6. @pyqtSlot(list)
    7. def update_itemslist(self, itemslist):
    8. editor.addItems(itemslist)
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. class middleware_ui_arbiter(QWidget):
    2.  
    3. # PyQT signals need to be declared outside __init__
    4. signal = pyqtSignal(str)
    5.  
    6. def __init__(self, parent=None):
    7. super(middleware_ui_arbiter, self).__init__(parent)
    8.  
    9. # Widget definition
    10. self.view = QTreeView()
    11.  
    12. # Model definition
    13. self.model = QStandardItemModel(0,ARBITER_NB_COLUMNS)
    14.  
    15. # Set delegates
    16. self.view.setItemDelegateForColumn(1, ComboBoxDelegate(self, ["In", "Out"]))
    17. self.view.setItemDelegateForColumn(2, ComboBoxDelegate(self, []))
    To copy to clipboard, switch view to plain text mode 

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

    I imagine a call like this :

    Qt Code:
    1. self.<something>.connect(self.<something>.update_itemslist)
    To copy to clipboard, switch view to plain text mode 

    The thing is I don't know what <something> is in the code beneath.
    Last edited by enter; 19th March 2014 at 07:16.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.
    Last edited by ChrisW67; 19th March 2014 at 08:03.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  6. #6
    Join Date
    Mar 2014
    Posts
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically change QComboBox delegate values ?

    Quote Originally Posted by anda_skoa View Post
    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.

Similar Threads

  1. Replies: 1
    Last Post: 22nd April 2013, 23:42
  2. Replies: 1
    Last Post: 22nd February 2012, 12:32
  3. Replies: 7
    Last Post: 3rd November 2010, 23:18
  4. Item delegate displaying enumerated values
    By mclark in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2010, 16:31
  5. QComboBox values?
    By dbrmik in forum Newbie
    Replies: 8
    Last Post: 9th January 2009, 08:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.