j wales
j wales

Reputation: 15

How to create combobox with combobox inside using PyQt

For example suppose I have this dictionary - dict = {a: 1,2,3, b: 4,5,6, c: 7,8,9} how can I create combobox for dict with comboboxes for dict's values ?

enter image description here

Upvotes: 0

Views: 2779

Answers (1)

Brendan Abel
Brendan Abel

Reputation: 37509

It sounds like you want a Nested Menu.

enter image description here

One way to do that in Qt is to use a QToolButton with a QMenu

d = {'a': [1,2,3], 'b': [4,5,6], 'c': [7,8,9]}

button = QToolButton()

def callback_factory(k, v):
    return lambda: button.setText('{0}_{1}'.format(k, v))

menu = QMenu()
for k, vals in d.items():
    sub_menu = menu.addMenu(k)
    for v in vals:
        action = sub_menu.addAction(str(v))
        action.triggered.connect(callback_factory(k, v))

button.setMenu(menu)

Upvotes: 3

Related Questions