0

PyQt5 HorizontalScrollBar on combobox overlaps last item

This is without scrollbar

This is after combo.view().setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)

And also if i try ScrollBarAsNeeded i get this bug

Please help me to fix that issue

Thanks!

I found (Allow horizontal scrolling in a QComboBox)

It helped me a lot but if combo has 2-4 items it's starting overlap last item.

And if combo.view().setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) then VerticalScrollBar also appears but his default policy is ScrollBarAsNeeded as i know

Edited...

This a pretty simple example of issue:

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class Window(QMainWindow):

    def __init__(self):
        super().__init__()
        self.combo_box_issue = None
        self.combo_box_good = None
        self.setWindowTitle("Python ")
        self.setGeometry(100, 100, 600, 400)
        self.UiComponents()
        self.show()

    def UiComponents(self):
        # bad combo
        self.combo_box_issue = QComboBox(self)
        self.combo_box_issue.setFixedWidth(300)
        self.combo_box_issue.setGeometry(170, 100, 120, 30)
        self.combo_box_issue.setEditable(True)
        view = QListView(self)
        geek_list = ["Geek", "Geeky GeekGeeky GeekGeeky GeekGeeky"]
        self.combo_box_issue.addItems(geek_list)
        self.combo_box_issue.setView(view)
        view.setTextElideMode(QtCore.Qt.ElideNone)
        view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        # good combo
        self.combo_box_good = QComboBox(self)
        self.combo_box_good.setFixedWidth(300)
        self.combo_box_good.setGeometry(170, 200, 120, 30)
        self.combo_box_good.setEditable(True)
        view = QListView(self)
        geek_list = ["Geek", "Geeky GeekGeeky GeekGeeky GeekGeekyGeekGeeky GeekGeeky GeekGeekyGeekGeeky GeekGeeky GeekGeekyGeekGeeky GeekGeeky GeekGeeky"]
        self.combo_box_good.addItems(geek_list)
        self.combo_box_good.setView(view)
        view.setTextElideMode(QtCore.Qt.ElideNone)
        view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)


App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

So if i clicked a bad combo first time i got this. But if i clicked one more time i got this. HorizontalScroll overlaps last item of combo

And when i use policy ScrollBarAsNeeded i got this

Why it's starting overlaps? Why verticalscrollbar is born?

2
  • What do you mean by "if combo has 2-4 items it's starting overlap last item"? Can you please provide a minimal reproducible example that also shows the actual need for the horizontal scroll bar? Because right now your images only show text that never needs it. Commented Mar 5, 2024 at 19:02
  • Are you sure that you just don't want to show a wider popup? By default, QComboBox tries to show itself as wide as possible, but the layouts may make it smaller than expected, with the result that the popup is limited by that width; a better solution is to make the view wider, not to always show its horizontal scroll bar, unless the item width is really that big (which doesn't seem a good idea from the UX perspective). Also, you're not using layout managers, which would eventually solve the problem, as fixed geometries are almost never a good idea. Commented Mar 6, 2024 at 2:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.