Search code examples
pythonqtpyqtqt-designer

Arranging pyqt combobox in toolbar


I have made a toolbar in qt designer with a few buttons. I have found some answers on stack that say you can not add a combobox in qt designer. With this I found an example of adding it manually. The method was:

self.combo=QtGui.QComboBox(self.toolBar)
self.combo=insertItems(1,["One","Two","Three"])

However, this puts the combobox all the way on the left ontop of my other buttons. How do I add this to the end? I read the doc that says the QComboBox is QStandardItemModel, which either takes self or a parent. I have tried giving extra arguments like some sort of index but the error says it only takes one argument. How can I specify which location the combobox will go?

Thanks

enter image description here


Solution

  • You added QComboBox as a child of QToolbar. It doesn't belong to any layout, so it doesn't take space in toolbar's layout. You need to use QToolbar::addWidget or QToolbar::insertWidget instead.

    self.combo=QtGui.QComboBox()
    toolBar.addWidget(self.combo)
    self.combo.insertItems(1,["One","Two","Three"])
    

    Note that I've replaced = to . in the last line. It should have been typing error.