【问题标题】:PyQt5 QComboBox list items changing postionPyQt5 ComboBox 列表项改变位置
【发布时间】:2020-06-17 02:47:33
【问题描述】:

我在 Qcombobox 项目的显示样式方面遇到了一些问题。目前正在硬编码要在组合框中显示的数据。

这里是代码:

self.Dummy = QComboBox(self)
self.Dummy.setGeometry(200,600, 350, 50)

self.Dummy.setStyleSheet("QComboBox {background-color: white;border-style: outset;" border-width: 2px;border-radius: 5px;border-color: #448aff; font:  12px; min-width: 10em; padding: 3px;}")

self.Dummy.addItems(["-Select-", "2", "3","4","5","6","7","8","9","0","11",])

问题是下拉“列表”位置在每次选择后都会不断变化。这是我面临的问题的图像。

下面是我的组合框

列表包含项目 ,2,3,4,5,6,7,8,9,0,11 ,其中 将是第一个显示的元素。

现在,当我单击框时,框会“向下”列出元素并假设我选择了“2”。然后,如果我尝试选择另一个项目,列表将朝“向下”方向下降。见下文

现在,假设从项目中选择了最后一个元素,“11”。现在,如果我尝试通过单击该框来选择一个新项目,该列表将“向上”弹出而不是向下弹出。见下文

应该怎么做才能修复它?我不认为它是样式表的问题,没有它,这个问题正在发生。我需要解决这个问题的原因是当列表弹出时,它会覆盖它上面的标签

【问题讨论】:

  • 如果您可以提供带有和不带有弹出窗口的界面的实际屏幕截图(不是屏幕照片),这可能会有所帮助。我相信我已经了解发生了什么,但我想更加确定。

标签: python-2.7 pyqt5 ubuntu-16.04 qcombobox


【解决方案1】:

您看到的是一种依赖于操作系统和样式的行为。

为了避免这种情况,最好的方法是继承 QComboBox 并覆盖 showPopup(),然后我们调用基类实现(负责显示、调整大小和定位弹出视图)并在必要时移动它。

class Combo(QtWidgets.QComboBox):
    def showPopup(self):
        super().showPopup()
        # find the widget that contains the list; note that this is *not* the view
        # that QComboBox.view() returns, but what is used to show it.
        popup = self.view().window()
        rect = popup.geometry()
        if not rect.contains(self.mapToGlobal(self.rect().center())):
            # the popup is not over the combo, there's no need to move it
            return
        # move the popup at the bottom left of the combo
        rect.moveTopLeft(self.mapToGlobal(self.rect().bottomLeft()))
        # ensure that the popup is always inside the edges of the screen
        # we use the center of the popup as a reference, since with multiple
        # screens the combo might be between two screens, but that position
        # could also be completely outside the screen, so the cursor position
        # is used as a fallback to decide on what screen we'll show it
        done = False
        for i, pos in enumerate((rect.center(), QtGui.QCursor.pos())):
            for screen in QtWidgets.QApplication.screens():
                if pos in screen.geometry():
                    screen = screen.geometry()
                    if rect.x() < screen.x():
                        rect.moveLeft(screen.x())
                    elif rect.right() > screen.right():
                        rect.moveRight(screen.right())
                    if rect.y() < screen.y():
                        rect.moveTop(screen.y())
                    elif rect.bottom() > screen.bottom():
                        # if the popup goes below the screen, move its bottom
                        # *over* the combo, so that the its current selected
                        # item will always be visible
                        rect.moveBottom(self.mapToGlobal(QtCore.QPoint()).y())
                    done = True
                    break
            if done:
                break
        popup.move(rect.topLeft())

这也可以在没有子类化的情况下完成(例如,如果您有许多组合,您从 Designer 创建了 UI,并且不想使用提升的小部件),但您必须记住更改对组合的所有引用.

class MyWindow(QtWidgets.QWidget):
    def __init__(self):
        # ...
        self.combo = QtWidgets.QComboBox()
        self.combo.showPopup = self.showPopupAndCheck

    def showPopupAndCheck(self):
        QtWidgets.QComboBox.showPopup(self.combo)
        popup = self.view().window()
        rect = popup.geometry()
        if not rect.contains(self.combo.mapToGlobal(self.combo.rect().center())):
            # the popup is not over the combo, there's no need to move it
            return
        # change everything from self to self.combo

或者,如果您想在所有程序中保持这种行为一致而不总是使用子类,您可以使用某种猴子修补技巧。
优点是您创建的任何 QComboBox(即使在加载 UI 文件或在运行时创建组合时)将始终使用新行为。

重要:此必须位于程序文件的最开头,可能就在导入部分之后。 p>

from PyQt5 import QtCore, QtGui, QtWidgets

def customShowPopup(self):
    # we can't use super(), because we're not in the class definition, but
    # calling the class method with "self" as first argument is practically the 
    # same thing; note the underscore!
    QtWidgets.QComboBox._showPopup(self)
    popup = self.view().window()
    # ... go on, exactly as above

# create a new reference to the showPopup method, which is the one we've used
# in the function above
QtWidgets.QComboBox._showPopup = QtWidgets.QComboBox.showPopup
# overwrite the original reference with the new function
QtWidgets.QComboBox.showPopup = customShowPopup

【讨论】:

  • 谢谢,我会努力实现的
  • popup = self.findChild(QtWidgets.QFrame)改成popup = self.view().window(),更优雅
猜你喜欢
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多