Important: Please read the Qt Code of Conduct -

How to avoid overlaping QComboBox items

  • Overlaping

    I use a general css file for all the widget theme.

    QComboBox {
    	color: #ffffff;
    	background-color: #d2a6ff;
    	selection-background-color: #8000ff;
    	outline: 0;
    	font-size: 13pt; font-family: Vazir;
    QComboBox QAbstractItemView
       selection-color: #ccc; 
       height: 30px;

  • Lifetime Qt Champion


    Do you mean the white text on the right ?

  • @SGaist

    My ComboBox items are checkable. I happened to have this rare behavior on other comboBoxes.

    When I catch evenFilter mouse click on one of them it goes away. But on comboBox which is checkable the problem stays.

  • @SGaist

    I'm really sorry if I couldn't find a way to show the picture properly.

  • Lifetime Qt Champion

    In that's case, can you provide a minimal compilable code sample that uses that style sheet and triggers your issue ?

  • @SGaist

    I made a dialog with two comboBoxes and assign the css file but there isn't any errors.
    My actual program consist a centeral tab with in other dialog classes placed there.When i run the dialog class by itself there is no errors. But when i run the main program and use it as a tab errors raise.
    Also there is another issue when i add items to database on another tabs when the comboBoxes tab is open doesn't get updated.
    i wrote piece of code which is eventFilter on mouse click on combos and calling a method for refreshing the model of combos. Is there any way to refresh the form without clicking ?

  • Banned

    Okay first off if you have never read about MVC methodology it seems you should. The actual calls to the database should not take place within your GUI as they seem to by your description.

    You should have an antonymous class (Model or Data Source Class) that handles and encapsulates all direct database communication -- then you have a Controller/Handler that is used to take requests from the View (GUI) and sends them to the database -- now in Python-PyQt this object can be your QMainWindow if you have sub-classed your CenterWidget as your View (GUI) then is actually your CenterWidget and not your QMainWindow -- otherwise you need an Controller/Handler layer that instantiates your View and Model classes and all it does is handle the communication between them.

  • @Denni-0

    Hello. Thanks for response. I know about MVC since i study Django a little but i don't know how to implement that in pyqt and sqlite. here is my update comboBox code i have to call the method each time to update the view please give me a practical example with code .

        def updCombos(self):
            model = QtSql.QSqlQueryModel()
            qry = QtSql.QSqlQuery()
            qry.prepare("SELECT Address FROM   PlcAddress  WHERE  ID  NOT IN (SELECT plcAddr_id FROM PlcOuts)")
            qry1 = QtSql.QSqlQuery()
            cell1 = []
            qry1.prepare ("SELECT * FROM plcValue");
            if( qry1.exec ()):
                while :
                    if str(qry1.value(1)) not in cell1 :
            for index, element in enumerate(cell1):
                if self.comboBoxInput.findText(element) == -1 :
                    item = self.comboBoxInput.model().item(index, 0)

    So this method is just for showing as you said i have to make a universal model for inserting and selecting and bind it to view how could i implement that i already google search and bump into QAbstractTableModel. I'm really confused but i know the MVC is right way to deal with database here is database insert from other class :

    qry = QtSql.QSqlQuery() 
    qry.prepare("insert into PlcAddress(Address) values ('"+self.lineEditAddr.text()+"')")

  • I made a separate class and yet again the view does'nt get updated after insertion on another form:

    import DbConnection
    from PyQt5 import QtCore,QtSql
    class ComboModel(QtCore.QAbstractListModel):
        def __init__(self, parent=None, *args):
            super(ComboModel, self).__init__()
            # The data would normally be read from a database (not using the Qt SQL classes)
   = []
            self.conn = DbConnection
            model = QtSql.QSqlQueryModel()
            qry = QtSql.QSqlQuery()
            qry.prepare("SELECT Address FROM   PlcAddress  WHERE  ID  NOT IN (SELECT plcAddr_id FROM PlcOuts)")
                while (
        def rowCount(self, parent):
            return len(
        def data(self, index, role): 
            if role ==QtCore.Qt.DisplayRole:
                row = index.row()
        def insertRow(self,row):
            qry = QtSql.QSqlQuery()
            qry.prepare("insert into PlcAddress(Address) values ('"+row+"')")
            if qry.exec():
                return True
                return False
        def removeRow(self,index):
            qry = QtSql.QSqlQuery()
            qry.prepare("delete from PlcAddress where ID = ?")
            if qry.exec():
                return True
                return False

  • @behruz-montazeri

    1. Your data() method has no route to return anything if role is not DisplayRole. Suggest you address.

    2. Your insertRow() & deleteRow() methods fail to notify the outside world (i.e. the view) when it is inserting/deleting rows. This is why the view does not update. If you override a method please read the documentation, e.g.

    In either case, you will need to call beginInsertRows() and endInsertRows() to notify other components that the model has changed.

    This has nothing to do with whether the view is on another form, which is what you implied.

  • @JonB

    I have no idea how to use it please give me some practical example . I guess my class skeleton is completely wrong.

  • Banned

    @behruz-montazeri if you want to learn how to do MVC within python-pyqt shoot me a message on Discord at DenniO#8137 and I will dive into that with you as well as share some example code as that can be a rather extensive discourse.

Log in to reply