简体   繁体   English

如何在QGIS插件中从1打印到10? (不是一次)

[英]How can I print from 1 to 10 in the QGIS Plugin? ( not at once )

for i in range(1, 10):
    QgsMessageLog.logMessage(str(i), tag="Validating", level=QgsMessageLog.INFO)
    time.sleep(1)

QGIS will wait for the plugin to finish. QGIS将等待插件完成。 ( 123456789 ) (123456789)

It is bring to me result after finished work. 完成工作后给我带来的结果。

So i can't see step by step logging at QGIS. 所以我看不到QGIS的逐步记录。 ( 1... 2... 3... 4... ... 9 ) (1 ... 2 ... 3 ... 4 ... 9)

I would like to see it printed each time it is executed. 我希望每次执行时都将其打印出来。

How can i do that? 我怎样才能做到这一点?

Never use time.sleep() in the main thread of the GUI, that function is block the execution of the GUI so you get that behavior, in your particular case you can use QTimeLine : 永远不要在GUI的主线程中使用time.sleep() ,该函数会阻止GUI的执行,因此您会得到这种行为,在特定情况下,您可以使用QTimeLine

def onframeChanged(val):
    msg = str(val)
    QgsMessageLog.logMessage(msg, tag="Validating", level=QgsMessageLog.INFO)

n = 9
self.timeline = QtCore.QTimeLine(n*1000)
self.timeline.setFrameRange(0, n)
self.timeline.frameChanged.connect(onframeChanged)
self.timeline.finished.connect(self.timeline.deleteLater)
self.timeline.start()

If you want the task to run periodically you can use QTimer : 如果您希望任务定期运行,则可以使用QTimer

def onTimeout():
    msg = QtCore.QDateTime.currentDateTime().toString()
    QgsMessageLog.logMessage(msg, tag="Validating", level=QgsMessageLog.INFO)

self.timer = QtCore.QTimer(interval=1000)
self.timer.timeout.connect(onTimeout)

self.timer.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM