Python 2.7 PyQt5 QCOMBOX列表项更改位置

Python 2.7 PyQt5 QCOMBOX列表项更改位置,python-2.7,pyqt5,ubuntu-16.04,qcombobox,Python 2.7,Pyqt5,Ubuntu 16.04,Qcombobox,我在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;bo

我在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”中选择了最后一个元素。现在,如果我试图通过单击框来选择一个新项目,列表将弹出“向上”,而不是向下。见下文


应该做些什么来修复它?我不认为这是样式表的问题,没有样式表,这个问题就会发生。我之所以需要修正这个问题,是因为当列表弹出时,它会覆盖上面的标签,你看到的是一种依赖于操作系统和样式的行为

为了避免它,最好的方法是将QComboBox子类化并覆盖,然后调用基类实现(负责显示、调整和定位弹出视图),并在必要时移动它

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())
或者,如果您希望在整个程序中保持这种行为的一致性,而不总是使用子类,则可以使用某种类型的monkey patching hack。
优点是,您创建的任何
QComboBox(即使在运行时加载UI文件或创建组合时)都将始终使用新行为

重要:这必须在程序的文件的最开头,可能就在导入部分之后

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

如果您可以提供界面的实际屏幕截图(而不是屏幕照片),无论是否有弹出窗口,这可能会有所帮助。我相信我已经理解了发生的事情,但我想对此更加确定。谢谢,我将尝试实现itchange
popup=self。findChild(qtwidts.QFrame)
popup=self.view().window()
,更优雅
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