覆盖QCompleter弹出位置

2024-05-14 02:43:45 发布

您现在位置:Python中文网/ 问答频道 /正文

关于覆盖QCompleter弹出位置也有类似的问题,但我仍然找不到有效的解决方案。我只想将弹出窗口向下移动大约5px(我有一些特定的样式要求)

我尝试过子类化QListView,并使用setPopup()将其用作我的弹出窗口。然后,我覆盖showEvent并在Y中向下移动弹出窗口。我也在resizeEvent上这样做,因为我认为这是在过滤项目并调整弹出窗口大小时触发的。但是这不起作用。。然后我用一个单发计时器在1毫秒后触发移动。这确实是一种工作,但它看起来很不一致——第一次显示与后续时间或调整大小不同

下面是我的最新尝试(试图通过计算弹出窗口的数量来破解它…),希望有人能告诉我我做错了什么,或者有更好的解决方案

import sys
import os
from PySide2 import QtCore, QtWidgets, QtGui

class QPopup(QtWidgets.QListView):
    def __init__(self, parent=None):
        super(QPopup, self).__init__(parent)

        self.popups = 0

    def offset(self):
        y = 3 if self.popups < 2 else 7
        print('y: {}'.format(y))
        self.move(self.pos().x(), self.pos().y() + y)

        self.popups += 1

    def showEvent(self, event):
        print('show')
        # self.offset()
        QtCore.QTimer.singleShot(1, self.offset)

    def resizeEvent(self, event):
        print('resize')
        # self.offset()
        QtCore.QTimer.singleShot(1, self.offset)

class MyDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(MyDialog, self).__init__(parent)

        self.create_widgets()
        self.create_layout()
        self.create_connections()

    def create_widgets(self):
        self.le = QtWidgets.QLineEdit('')

        self.completer = QtWidgets.QCompleter(self)
        self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.completer.setCompletionMode(QtWidgets.QCompleter.PopupCompletion)
        self.completer.setMaxVisibleItems(10)
        self.completer.setFilterMode(QtCore.Qt.MatchContains)
        self.completer.setPopup(QPopup())

        popup = QPopup(self)
        self.completer.setPopup(popup)

        self.model = QtCore.QStringListModel()
        self.completer.setModel(self.model)

        self.le.setCompleter(self.completer)

        self.completer.model().setStringList(['one','two','three'])

    def create_layout(self):
        main_layout = QtWidgets.QVBoxLayout(self)

        main_layout.addWidget(self.le)

    def create_connections(self):
        pass

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    my_dialog = MyDialog()

    my_dialog.show()  # Show the UI
    sys.exit(app.exec_())

Tags: importselfinitdefcreatesysoffsetparent
1条回答
网友
1楼 · 发布于 2024-05-14 02:43:45

一种解决方案是创建QLineEdit和override keyPressEvent的子类,以显示带有偏移量的弹出窗口:

PySide2.QtWidgets.QCompleter.complete([rect=QRect()])

For PopupCompletion and QCompletion::UnfilteredPopupCompletion modes, calling this function displays the popup displaying the current completions. By default, if rect is not specified, the popup is displayed on the bottom of the widget() . If rect is specified the popup is displayed on the left edge of the rectangle.

doc.qt.io -> QCompleter.complete

完整、独立的示例

rect是基于光标rect的y位置计算的。弹出窗口的高度不变。宽度调整为ZLineEdit小部件的宽度

rect = QtCore.QRect(0,
                    self.cursorRect().y() + 4,
                    self.width(),
                    self.completer().widget().height())

您的代码,使用上面提到的要点稍微修改一下,可以如下所示:

import sys
from PySide2 import QtCore, QtWidgets
from PySide2.QtWidgets import QLineEdit, QDialog, QCompleter


class ZLineEdit(QLineEdit):
    def __init__(self, string, parent=None):
        super().__init__(string, parent)

    def keyPressEvent(self, event):
        super().keyPressEvent(event)
        if len(self.text()) > 0:
            rect = QtCore.QRect(0,
                                self.cursorRect().y() + 4,
                                self.width(),
                                self.completer().widget().height())
            self.completer().complete(rect)


class MyDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.le = ZLineEdit('')
        autoList = ['one', 'two', 'three']
        self.completer = QCompleter(autoList, self)

        self.setup_widgets()
        self.create_layout()
        self.create_connections()

    def setup_widgets(self):
        self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.completer.setCompletionMode(QtWidgets.QCompleter.PopupCompletion)
        self.completer.setMaxVisibleItems(10)
        self.completer.setFilterMode(QtCore.Qt.MatchContains)
        self.le.setCompleter(self.completer)

    def create_layout(self):
        main_layout = QtWidgets.QVBoxLayout(self)
        main_layout.addWidget(self.le)

    def create_connections(self):
        pass


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    my_dialog = MyDialog()
    my_dialog.show()
    sys.exit(app.exec_())

测试

在左侧可以看到默认行为。在右侧,弹出窗口向下移动4倍:

offset

相关问题 更多 >