gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:41:02 27 4
gpt4 key购买 nike

下面是 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com