python - PyQt、QComboBox 和 QStringModel 导致 QObject::startTimer: QTimer 只能与以 QThread 启动的线程一起使用

标签 python qt pyqt qcombobox

下面是 PyQt4 的可运行示例,我使用下面的代码并得到了非常奇怪的问题,例如 QObject::startTimer: QTimer can only be used withthreads started with QThread after close the window:

combo = ExtendedComboBox()
# combo.addItems(string_list)
combo.setModel(QStringListModel(string_list))

一旦我更改为以下代码,一切都运行良好:

combo = ExtendedComboBox()
combo.addItems(string_list)
# combo.setModel(QStringListModel(string_list))

感谢您的每一条评论:)

在此处附上完整代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QCompleter, QComboBox, QSortFilterProxyModel, QDialog


class ExtendedComboBox(QComboBox):
    def __init__(self, parent=None):
        super(ExtendedComboBox, self).__init__(parent)

        self.setFocusPolicy(Qt.StrongFocus)
        self.setEditable(True)

        # add a filter model to filter matching items
        self.pFilterModel = QSortFilterProxyModel(self)
        self.pFilterModel.setFilterCaseSensitivity(Qt.CaseInsensitive)
        self.pFilterModel.setSourceModel(self.model())

        # add a completer, which uses the filter model
        self.completer = QCompleter(self.pFilterModel, self)
        # always show all (filtered) completions
        self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
        self.setCompleter(self.completer)

        # connect signals
        self.lineEdit().textEdited[unicode].connect(self.pFilterModel.setFilterFixedString)
        self.completer.activated.connect(self.on_completer_activated)


    # on selection of an item from the completer, select the corresponding item from combobox
    def on_completer_activated(self, text):
        if text:
            index = self.findText(text)
            self.setCurrentIndex(index)


    # on model change, update the models of the filter and completer as well
    def setModel(self, model):
        super(ExtendedComboBox, self).setModel(model)
        self.pFilterModel.setSourceModel(model)
        self.completer.setModel(self.pFilterModel)


    # on model column change, update the model column of the filter and completer as well
    def setModelColumn(self, column):
        self.completer.setCompletionColumn(column)
        self.pFilterModel.setFilterKeyColumn(column)
        super(ExtendedComboBox, self).setModelColumn(column)


if __name__ == "__main__":
    import sys
    from PyQt4.QtGui import QStringListModel, QApplication, \
        QStandardItemModel, QStandardItem, QVBoxLayout, QHBoxLayout, \
        QMainWindow

    app = QApplication(sys.argv)
    # win = QDialog()
    # win.setMinimumSize(400, 400)
    # layout = QHBoxLayout()
    # win.setLayout(layout)
    string_list = ['hola muchachos', 'adios amigos', 'hello world', 'good bye']
    combo = ExtendedComboBox()
    # combo.addItems(string_list)
    combo.setModel(QStringListModel(string_list))
    # model = QStandardItemModel()
    # for i, word in enumerate(['hola', 'adios', 'hello', 'good bye']):
    #     item = QStandardItem(word)
    #     model.setItem(i, 0, item)
    #
    # combo.setModel(model)
    # layout.addWidget(combo)
    combo.show()
    # win.show()

    sys.exit(app.exec_())

最佳答案

我从这里得到了答案:Error in model view implemention of GUI in pyqt

添加带有父模型的模型后combo.setModel(QStringListModel(string_list,combo)),错误消失了。

关于python - PyQt、QComboBox 和 QStringModel 导致 QObject::startTimer: QTimer 只能与以 QThread 启动的线程一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30549477/

相关文章:

python - 调整列宽以适应 QTableWidget pyqt

python - 检测 Ctrl+S ionic QTextedit?

python - 在 python 中我应该使用 try 还是 If ,为什么?

python - 我犯了什么错误?

qt - 如何仅设置 `Widget` 大小?

c++ - QQuickImageProvider::requestImage图像缩放,如何处理

Python - 使用 pyqtgraph 快速绘图(16ms)?

python - 如何在python中查找连续出现的条件1

python - 显示是否可以从整数列表中选择两个整数以使它们的差等于给定值的递归算法

c++ - 在发布文件夹中运行的 Qt 可执行文件 - Program Files (x86) 中的运行时错误