QLineEdit 中的模型/视图 QCompleter

发布于 2024-11-04 07:04:41 字数 2764 浏览 2 评论 0原文

ubuntu 10.04,KDE 4.4.5

python 2.6.4

qt 4.6.2

pyqt 4.6.2

我正在尝试创建一个 QCompleter,如果我只是构建 QLineEdit,它就可以正常工作。 但是,如果我将 QLineEdit 放入 QMainWindow 中,QCompleter 将不再工作。

这是 LineEdit 类

# LineEdit class
import sys
from PyQt4 import QtCore, QtGui

class LineEdit(QtGui.QLineEdit):
    def __init__(self, parent=None):
        super(LineEdit, self).__init__(parent)

        self.setFocusPolicy(QtCore.Qt.StrongFocus)

        self.completer = QtGui.QCompleter(self)
        self.completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)
        self.pFilterModel = QtGui.QSortFilterProxyModel(self)
        self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.completer.setPopup(self.view())
        self.setCompleter(self.completer)
        self.textEdited[unicode].connect(self.pFilterModel.setFilterFixedString)

    def setModel(self, model):
        self.pFilterModel.setSourceModel(model)
        self.completer.setModel(self.pFilterModel)

    def setModelColumn( self, column ):
        self.completer.setCompletionColumn(column)
        self.pFilterModel.setFilterKeyColumn(column)

    def view(self):
        return self.completer.popup()

    def index( self ):
        return self.currentIndex()

如果我以这种方式构建 LinEdit,则 QCompleter 可以正常工作

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    model = QtGui.QStandardItemModel()

    for i,word in enumerate(['test', 'blah', 'heh', 'yep']):
        item = QtGui.QStandardItem(word)
        model.setItem(i, 0, item)

    lineEdit = LineEdit()
    lineEdit.setModel(model)
    lineEdit.setModelColumn(0)
    lineEdit.show()
    sys.exit(app.exec_())

这可以正常编译,但不再显示 QCompleter

if __name__ == '__main__':
    class Example(QtGui.QMainWindow):
        def __init__(self):
            QtGui.QMainWindow.__init__(self)

            self.centralWidget = QtGui.QWidget(self)
            self.layout = QtGui.QVBoxLayout(self.centralWidget)

            # can I push this inside the LineEdit class instead?
            model = QtGui.QStandardItemModel()
            for i, word in enumerate(['test', 'blah', 'heh', 'yep', 'hello', 'hi']):
                item = QtGui.QStandardItem(word)
                model.setItem(i, 0, item)

            # Make a LineEdit instance
            self.lineEdit = LineEdit(parent=self.centralWidget)
            self.lineEdit.setModel(model)
            self.lineEdit.setModelColumn(0)
            self.layout.addWidget(self.lineEdit)
            self.setCentralWidget(self.centralWidget)


    app = QtGui.QApplication(sys.argv)
    QtWin = Example()
    QtWin.show()
    sys.exit(app.exec_())

ubuntu 10.04, KDE 4.4.5

python 2.6.4

qt 4.6.2

pyqt 4.6.2

I'm trying to create a QCompleter, which works fine if I just build the QLineEdit.
However if I drop the QLineEdit into a QMainWindow, the QCompleter no longer works.

Here is the LineEdit class

# LineEdit class
import sys
from PyQt4 import QtCore, QtGui

class LineEdit(QtGui.QLineEdit):
    def __init__(self, parent=None):
        super(LineEdit, self).__init__(parent)

        self.setFocusPolicy(QtCore.Qt.StrongFocus)

        self.completer = QtGui.QCompleter(self)
        self.completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)
        self.pFilterModel = QtGui.QSortFilterProxyModel(self)
        self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.completer.setPopup(self.view())
        self.setCompleter(self.completer)
        self.textEdited[unicode].connect(self.pFilterModel.setFilterFixedString)

    def setModel(self, model):
        self.pFilterModel.setSourceModel(model)
        self.completer.setModel(self.pFilterModel)

    def setModelColumn( self, column ):
        self.completer.setCompletionColumn(column)
        self.pFilterModel.setFilterKeyColumn(column)

    def view(self):
        return self.completer.popup()

    def index( self ):
        return self.currentIndex()

The QCompleter works if I build LinEdit this way

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    model = QtGui.QStandardItemModel()

    for i,word in enumerate(['test', 'blah', 'heh', 'yep']):
        item = QtGui.QStandardItem(word)
        model.setItem(i, 0, item)

    lineEdit = LineEdit()
    lineEdit.setModel(model)
    lineEdit.setModelColumn(0)
    lineEdit.show()
    sys.exit(app.exec_())

This compiles fine, but no longer shows the QCompleter

if __name__ == '__main__':
    class Example(QtGui.QMainWindow):
        def __init__(self):
            QtGui.QMainWindow.__init__(self)

            self.centralWidget = QtGui.QWidget(self)
            self.layout = QtGui.QVBoxLayout(self.centralWidget)

            # can I push this inside the LineEdit class instead?
            model = QtGui.QStandardItemModel()
            for i, word in enumerate(['test', 'blah', 'heh', 'yep', 'hello', 'hi']):
                item = QtGui.QStandardItem(word)
                model.setItem(i, 0, item)

            # Make a LineEdit instance
            self.lineEdit = LineEdit(parent=self.centralWidget)
            self.lineEdit.setModel(model)
            self.lineEdit.setModelColumn(0)
            self.layout.addWidget(self.lineEdit)
            self.setCentralWidget(self.centralWidget)


    app = QtGui.QApplication(sys.argv)
    QtWin = Example()
    QtWin.show()
    sys.exit(app.exec_())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝眼睛不忧郁 2024-11-11 07:04:41

事实证明确实非常简单,希望这能帮助其他使用 PyQt 的 QCompleter 进行自动完成的人

import sys
from PyQt4 import QtCore, QtGui

class LineEdit(QtGui.QLineEdit):
    def __init__(self, parent, completerContents):
        super(LineEdit, self).__init__(parent)

        self.completerList = QtCore.QStringList()
        for content in completerContents:
            self.completerList.append(QtCore.QString(content))
        self.completer = QtGui.QCompleter(self.completerList, self)
        self.completer.setCompletionMode(QtGui.QCompleter.PopupCompletion)
        self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.setCompleter(self.completer)

if __name__ == '__main__':
    class Example(QtGui.QMainWindow):
        def __init__(self):
            QtGui.QMainWindow.__init__(self)

            self.centralWidget = QtGui.QWidget(self)
            self.layout = QtGui.QVBoxLayout(self.centralWidget)

            # Example LineEdit Call
            self.lineEdit = LineEdit(parent=self.centralWidget, completerContents=('test', 'blah', 'heh', 'yep', 'hello', 'hi'))

            self.layout.addWidget(self.lineEdit)
            self.setCentralWidget(self.centralWidget)

app = QtGui.QApplication(sys.argv)
QtWin = Example()
QtWin.show()
sys.exit(app.exec_())

turned out to be quite simple really, hopefully this will help anyone else using PyQt's QCompleter for auto-completion

import sys
from PyQt4 import QtCore, QtGui

class LineEdit(QtGui.QLineEdit):
    def __init__(self, parent, completerContents):
        super(LineEdit, self).__init__(parent)

        self.completerList = QtCore.QStringList()
        for content in completerContents:
            self.completerList.append(QtCore.QString(content))
        self.completer = QtGui.QCompleter(self.completerList, self)
        self.completer.setCompletionMode(QtGui.QCompleter.PopupCompletion)
        self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.setCompleter(self.completer)

if __name__ == '__main__':
    class Example(QtGui.QMainWindow):
        def __init__(self):
            QtGui.QMainWindow.__init__(self)

            self.centralWidget = QtGui.QWidget(self)
            self.layout = QtGui.QVBoxLayout(self.centralWidget)

            # Example LineEdit Call
            self.lineEdit = LineEdit(parent=self.centralWidget, completerContents=('test', 'blah', 'heh', 'yep', 'hello', 'hi'))

            self.layout.addWidget(self.lineEdit)
            self.setCentralWidget(self.centralWidget)

app = QtGui.QApplication(sys.argv)
QtWin = Example()
QtWin.show()
sys.exit(app.exec_())
迷荒 2024-11-11 07:04:41

在第二种情况下,这种行为可能有两个原因:

  1. 在第二种情况下,您的完成器没有完成模型
  2. 您的 LineEdit 设置了其他完成器

不知道您是否可以调试此问题并在 python 中的 QLineEdit::setCompleter 上设置断点。

There is possible 2 reasons of such a behavior in the second case:

  1. Your completer has no completion model in the second case
  2. Your LineEdit has set other completer

Don't know if U can to debug this and set breakpoint on QLineEdit::setCompleter in python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文