簡體   English   中英

PyQt,帶有QStringModel的QComboBox導致QObject :: startTimer:QTimer僅可用於以QThread開頭的線程

[英]PyQt, QComboBox with QStringModel cause QObject::startTimer: QTimer can only be used with threads started with QThread

以下是PyQt4的可運行示例,我使用下面的代碼,遇到了一個非常奇怪的問題,例如QObject::startTimer: QTimer can only be used with threads started with QThread關閉窗口后QObject::startTimer: QTimer can only be used with threads started with QThread

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_())

我從這里得到了答案: pyqt中GUI的模型視圖實現中的錯誤

在添加帶有父combo.setModel(QStringListModel(string_list, combo)) ,錯誤消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM