0

On Windows: When I select any item from a non editable comboBox drop down, everytime drop down opens downwards because this is how QT natively implements it on windows.

On Linux: When I select first item from the dropdown, it opens downward, but if I select any other item and then open drop down again, then it doesn't expand downward exactly, some entries are upward and some downward, because of native theme of Linux.

Any easy way to make it similar to windows so that whole dropdown opens downward?

1 Answers1

0

This behavior does not depend on the OS but on the QStyle that is used (for example I use a style provided by KDE plasma that has the behavior you want but if I use the fusion style then I get the behavior you don't want). So considering the above, a possible solution is to modify the position of the popup a moment after it is displayed.

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class ComboBox(QtWidgets.QComboBox):
    def showPopup(self):
        super().showPopup()
        container = self.view().parentWidget()
        gp = self.mapToGlobal(self.rect().bottomLeft())
        container.move(gp)


def main():
    app = QtWidgets.QApplication(sys.argv)
    combo = ComboBox()
    combo.addItems([f"item {i}" for i in range(4)])
    combo.show()
    ret = app.exec_()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • It's worth noticing that Qt does some checks on the current QScreen in order to avoid showing a popup that goes outside the screen geometry. – musicamante Apr 01 '21 at 05:41