Search code examples
pythonpython-3.xpyqtpyqt5qcombobox

Can I change the direction of the qt Qcombobox?


I hope the drop-down list is always below the selection window. However, it seems that the window is automatically adjusted. The drop-down list goes up and is truncated. I'm wondering how you can sort underneath without automatically sorting the list.

This is the situation now:

ex) ------------------               -------------------
    |    A        | ↓ |     ->      |         A         |
    -------------------             --------------------
                                    |         B         |
                                    ---------------------
                                    |         C         |
                                    --------------------
    C click ->
                                     -------------------
                                     |        A         |
                                     --------------------
                                     |        B         |
    -------------------              --------------------
    |    C         | ↓ |     ->      |        C         |
    -------------------              --------------------

But I want:

   ----------------------      ->    -------------------
  |           C   |  ↓  |            |        A        |
  -----------------------            -------------------
                                     |        B        |
                                      ------------------
                                     |        C        |
                                     -------------------

Help me please.


Solution

  • You have to move the popup after it is displayed, for this you must move the internal QFrame that is the parent of the view() as I show below:

    import sys
    
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    class ComboBox(QComboBox):
        def showPopup(self):
            QComboBox.showPopup(self)
            self.view().parent().move(self.mapToGlobal(QPoint()))
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = QMainWindow()
        cw = QWidget()
        lay = QVBoxLayout(cw)
    
        view = ComboBox()
        view.addItems(["A", "B", "C"])
    
        lay.addWidget(view)
        lay.addWidget(QTableWidget())
        w.setCentralWidget(cw)
        w.show()
        sys.exit(app.exec_())