Search code examples
pythonpython-3.xpyqtpyqt5qcombobox

Python PyQt Change layout of combobox


Is there a way (or maybe a different widget i can use (i couldn't find any) so that i can make my combobox look something like the combobox in the picture ?

The picture shows what i want put with font. I would like the options to be my list instedt of the font.enter image description here

I would like to see all the option (if there are to many to be able to scroll) and select one.

Here is my code so far:

from PyQt5 import QtCore, QtWidgets
import sys
from PyQt5 import QtGui, QtWidgets, QtPrintSupport


class Thired(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Thired, self).__init__(parent)

        self.bb = QtWidgets.QComboBox(self)
        self.bb.setGeometry(QtCore.QRect(212, 50, 400, 25))
        self.bb.setEditable(True)

        bpas = ['a','b','c']

        self.bb.addItem("")
        self.bb.setItemText(0, "")
        for bpa in bpas:
            self.bb.addItem(bpa)

        self.bb.move(50, 200)
        self.bb.activated[str].connect(self.style_choice)

    def font_choice(self):
        font, valid = QtWidgets.QFontDialog.getFont()
        if valid:
            self.styleChoice.setFont(font)

    def style_choice(self, text):
        self.styleChoice.setText(text)
        QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create(text))

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    gui = Thired()
    gui.show()
    app.exec_()

Edit:

I would like that when the whindow opens that all my option are always beeing shown. So that i do not have to press the dropdown arror. In different words: If the window is beeing opened there is a list of bpas, i want to be able to select from the list one of the bpa/option and send a singal that i choose this bpa.

To explain myself a little more (this is not been shown anywhere in the code): bpas = ['a','b','c'] are projects, and I want the user to seleced one of them and after selecting it the programm will load its conneced database. With dropdown it works, but i dont like the way dorpdown looks like with a lot of options :)


Solution

  • You can use a QListWidget with a QLineEdit:

    from PyQt5 import QtCore, QtWidgets
    import string
    
    class Thired(QtWidgets.QDialog):
        def __init__(self, parent=None):
            super(Thired, self).__init__(parent)
    
            self.line_edit = QtWidgets.QLineEdit()
            self.list_widget = QtWidgets.QListWidget()
            options = list(string.ascii_letters)
            self.list_widget.addItems(options)
            self.list_widget.itemClicked.connect(self.on_itemClicked)
    
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(self.line_edit)
            lay.addWidget(self.list_widget)
            self.resize(640, 480)
    
        @QtCore.pyqtSlot(QtWidgets.QListWidgetItem)
        def on_itemClicked(self, item):
            self.line_edit.setText(item.text())
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        gui = Thired()
        gui.show()
        sys.exit(app.exec_())
    

    enter image description here