Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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
    {
       color:#ccc;
       background-color:#ba75ff;
       border:none;
       selection-background-color:#8000ff;
       selection-color: #ccc; 
       height: 30px;
    }
    
    
    

  • Lifetime Qt Champion

    Hi,

    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)")
            if(qry.exec()):
                model.setQuery(qry);
                self.comboBoxAddr.setModel(model)
                
            qry1 = QtSql.QSqlQuery()
            cell1 = []
            qry1.prepare ("SELECT * FROM plcValue");
            if( qry1.exec ()):
                while qry1.next() :
                    if str(qry1.value(1)) not in cell1 :
                        cell1.append(str(qry1.value(1)))     
            for index, element in enumerate(cell1):
                if self.comboBoxInput.findText(element) == -1 :
                    self.comboBoxInput.addItem(element)
                    item = self.comboBoxInput.model().item(index, 0)
                    item.setCheckState(QtCore.Qt.Unchecked)
    

    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.data = []
            self.conn = DbConnection
            self.conn.createConnection()
            
            model = QtSql.QSqlQueryModel()
          
            qry = QtSql.QSqlQuery()
          
            qry.prepare("SELECT Address FROM   PlcAddress  WHERE  ID  NOT IN (SELECT plcAddr_id FROM PlcOuts)")
            if(qry.exec()):
                while (qry.next()):
                    self.data.append(qry.value(0))
    
        def rowCount(self, parent):
            return len(self.data)
    
        def data(self, index, role): 
            
            if role ==QtCore.Qt.DisplayRole:
                row = index.row()
                return self.data[row]
            
        def insertRow(self,row):
            
            qry = QtSql.QSqlQuery()
            qry.prepare("insert into PlcAddress(Address) values ('"+row+"')")
            if qry.exec():
                return True
            else:
                return False
            
        def removeRow(self,index):
            
            qry = QtSql.QSqlQuery()
            qry.prepare("delete from PlcAddress where ID = ?")
            qry.addBindValue(index)
            
            if qry.exec():
                return True
            else:
                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. https://doc.qt.io/qt-5/qabstractitemmodel.html#insertRows

    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