python-3.x - 当 singleShot 为 True 时,QTimer 永远不会触发超时
问题描述
我正在尝试使用 aQTimer
将缓冲行附加到我的QStandatdItemModel
. 请注意,在我下面的代码中,我也尝试过使用QTimer.singleShot(self.refresh, self.__dump_buffer)
而不是实例化QTimer
,并且这两种样式似乎都不起作用,静态成员或实例化的类方法。我也尝试将 移动QTimer
到主线程,这没有帮助。无论我尝试什么,无论我尝试什么,连接到的插槽QTimer.timeout
似乎都不会被触发。Dump buffer
我已经通过从未打印到控制台的事实验证了这一点。
class ProgressLogItemModel(QStandardItemModel):
def __init__(self, parent=None, limit=1000, refresh=20):
super().__init__(parent)
self.limit = limit
self.timer = QTimer()
self.buffer = list()
# self.timer.moveToThread(QApplication.instance().thread())
self.timer.setSingleShot(True)
self.timer.setInterval(refresh) # The default is 20ms, being 50Hz
# Every time the single-shot timer runs out after 'refresh' milliseconds, dump the buffer
self.timer.timeout.connect(self.__dump_buffer)
@helpers.make_slot()
def __dump_buffer(self):
print("Dump buffer")
self.insertRows(self.rowCount(), len(self.buffer))
for offset, item in enumerate(self.buffer):
self.setData(self.index(self.rowCount() - len(self.buffer) + offset, 0), item)
self.buffer.clear() # Reset the buffer
# Not sure if the 'stop()' this is necessary but it is here
# to ensure that 'isActive()' returns 'False'
self.timer.stop()
def __apply_limit(self):
if self.rowCount() > self.limit:
# Remove rows from the beginning, count being the number of rows over the limit
self.removeRows(0, self.rowCount() - self.limit)
def insertRows(self, row, count, _=None):
super().insertRows(row, count)
self.__apply_limit()
def appendRow(self, item):
# Append the QStandardItem to the internal list to be popped into the model on the next timeout
self.buffer.append(item)
# If the timer is not currently running (this method has not been called
# before less than 'refresh' milliseconds ago), start the next single-shot to dump the buffer
if not self.timer.isActive():
print("Timer started")
self.timer.start() # Start the next single-shot
class ExampleProgressLogDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.progress_log_list_view = QListView(self)
self.progress_log_item_model = ProgressLogItemModel(self.progress_log_list_view)
self.progress_log_list_view.setModel(self.progress_log_item_model)
self.setLayout(QVBoxLayout(self))
self.layout().addWidget(self.progress_log_list_view)
def log(self, text):
self.progress_log_item_model.appendRow(QStandardItem(text))
if __name__ == "__main__":
import sys
from threading import Thread
from qtpy.QtWidgets import QApplication
app = QApplication()
dialog = ExampleProgressLogDialog()
def __add_log_lines():
for line in range(10000):
dialog.log(f"Log line: {line}")
add_log_lines_thread = Thread(target=__add_log_lines(), daemon=True)
add_log_lines_thread.start()
dialog.show()
sys.exit(app.exec_())
实际上,子类化将从何时应向用户提供反馈QDialog
的实例中实例化。将从任何想要调用该方法的潜在线程QMainWindow
开始。QTimer
log
解决方案
我不知道是什么helpers.make_slot
,所以我只是评论了它,并且__dump_buffer
正确调用了。
但是有一个大问题:使用 QStandardItemModel 插入行是不够的,您还必须使用setItem()
(而不是setData()
) 为这些索引设置 QStandardItems。
将该循环更改为:
for offset, item in enumerate(self.buffer):
self.setItem(self.rowCount() - len(self.buffer) + offset, 0, item)
请注意,不鼓励直接从 python 线程访问 Qt 对象。虽然在这种情况下,这不是一个大问题(因为 QStandardItems 不是 QObjects,但 QStandardItemModel是),但通常最好创建 QThread 子类并使用信号/插槽与主线程中的 Qt 对象进行交互。
推荐阅读
- python - 如何在 python 中使用 cron 和 smtp 安排电子邮件在特定时间发送?
- jupyter-notebook - 让 Altair 与 Jupyter Notebook 一起工作
- android - 水平显示日历日期 无图书馆 Android
- javascript - 拖动时奇怪的位置
- leaflet - 在 Leaflet 中将一个 LatLngs 数组转换为一个简单数组的数组
- reactjs - 如何让 React 组件等待 http 调用返回
- mysql - 计算重复值并按 id 对数百万条记录求和
- android - 我想使用 Picasso 从文件路径加载图像
- angular - 阿波罗角片段模棱两可
- javascript - JS中的不同this绑定