久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<tfoot id='0xC4B'></tfoot>
  • <i id='0xC4B'><tr id='0xC4B'><dt id='0xC4B'><q id='0xC4B'><span id='0xC4B'><b id='0xC4B'><form id='0xC4B'><ins id='0xC4B'></ins><ul id='0xC4B'></ul><sub id='0xC4B'></sub></form><legend id='0xC4B'></legend><bdo id='0xC4B'><pre id='0xC4B'><center id='0xC4B'></center></pre></bdo></b><th id='0xC4B'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0xC4B'><tfoot id='0xC4B'></tfoot><dl id='0xC4B'><fieldset id='0xC4B'></fieldset></dl></div>

    <small id='0xC4B'></small><noframes id='0xC4B'>

        <bdo id='0xC4B'></bdo><ul id='0xC4B'></ul>
      <legend id='0xC4B'><style id='0xC4B'><dir id='0xC4B'><q id='0xC4B'></q></dir></style></legend>

      1. 如何在單獨(dú)的 QThread 中使用 QTimer

        How to use a QTimer in a separate QThread(如何在單獨(dú)的 QThread 中使用 QTimer)
      2. <i id='2E22v'><tr id='2E22v'><dt id='2E22v'><q id='2E22v'><span id='2E22v'><b id='2E22v'><form id='2E22v'><ins id='2E22v'></ins><ul id='2E22v'></ul><sub id='2E22v'></sub></form><legend id='2E22v'></legend><bdo id='2E22v'><pre id='2E22v'><center id='2E22v'></center></pre></bdo></b><th id='2E22v'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='2E22v'><tfoot id='2E22v'></tfoot><dl id='2E22v'><fieldset id='2E22v'></fieldset></dl></div>

                <tbody id='2E22v'></tbody>

              <small id='2E22v'></small><noframes id='2E22v'>

                • <bdo id='2E22v'></bdo><ul id='2E22v'></ul>
                • <legend id='2E22v'><style id='2E22v'><dir id='2E22v'><q id='2E22v'></q></dir></style></legend>
                  <tfoot id='2E22v'></tfoot>

                  本文介紹了如何在單獨(dú)的 QThread 中使用 QTimer的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有一些計(jì)算量很大的任務(wù),我想在每 5 秒后循環(huán)運(yùn)行一次,而不會(huì)阻塞主事件循環(huán).為此,我打算使用 QTimer 和單獨(dú)的線程來運(yùn)行它.我已經(jīng)嘗試了以下代碼,但到目前為止還沒有成功:

                  I have some computationally heavy task that I want to run in a loop after every 5 seconds without blocking the main event-loop. For this, I intend to use a QTimer and a separate thread to run it. I have tried the following code but it has not worked so far:

                  @pyqtSlot()
                  def heavy_task_function():
                      # Sleep for 10 seconds to simulate heavy computation
                      time.sleep(10)
                      print "First Timer Fired"
                  
                  if __name__ == "__main__":
                      app = QCoreApplication.instance()
                      if app is None:
                          app = QApplication(sys.argv)
                  
                      threaded_timer = ModbusComThread(heavy_task_function)
                      threaded_timer.start()
                  
                      sys.exit(app.exec_())
                  

                  地點(diǎn):

                  class ModbusComThread(QThread):
                  
                      def __init__(self, slot_function):
                          QThread.__init__(self)
                          self.slot_function = slot_function
                          self.send_data_timer = None
                  
                      def run(self):
                          print "Timer started on different thread"
                          self.send_data_timer = QTimer(self)
                          self.send_data_timer.timeout.connect(self.slot_function)
                          self.send_data_timer.start(5000)
                  
                      def stop(self):
                          self.send_data_timer.stop()
                  

                  slot_function 永遠(yuǎn)不會(huì)被 threaded_timer 中的 QTimer 觸發(fā).我的線程架構(gòu)是否正確?

                  The slot_function is never fired by the QTimer in threaded_timer. Is my threading architecture correct?

                  推薦答案

                  一個(gè) QTimer 需要一個(gè)正在運(yùn)行的事件循環(huán).默認(rèn)情況下,QThread.run() 將為線程啟動(dòng)一個(gè)本地事件循環(huán),但如果你以你所做的方式完全覆蓋它,那將不會(huì)發(fā)生 - 所以計(jì)時(shí)器事件永遠(yuǎn)不會(huì)被處理.

                  A QTimer needs a running event-loop. By default, QThread.run() will start a local event-loop for the thread, but if you completely override it in the way that you have done, that won't happen - so the timer events will never be processed.

                  通常,當(dāng)您需要本地事件循環(huán)時(shí),您應(yīng)該創(chuàng)建一個(gè)工作對(duì)象來完成所有處理,然后使用 moveToThread 將其放在單獨(dú)的線程中.如果沒有,完全可以覆蓋 QThread.run().

                  In general, when you need a local event-loop you should create a worker object to do all the processing and then use moveToThread to put it in a separate thread. If not, it's perfectly okay to override QThread.run().

                  下面的演示展示了如何做到這一點(diǎn).請(qǐng)注意,在線程啟動(dòng)后創(chuàng)建計(jì)時(shí)器非常重要,否則它將在錯(cuò)誤的線程中創(chuàng)建,并且其計(jì)時(shí)器事件不會(huì)被線程的事件循環(huán)處理.同樣重要的是,工作線程和主線程之間的所有通信都是通過信號(hào)完成的,以確保線程安全.永遠(yuǎn)不要嘗試在主線程之外直接執(zhí)行 GUI 操作,因?yàn)?Qt 根本不支持.出于演示的目的,主線程中的第二個(gè)計(jì)時(shí)器用于在固定時(shí)間間隔后停止所有處理.如果有 GUI,用戶通過按鈕進(jìn)行干預(yù)也能實(shí)現(xiàn)同樣的效果.

                  The demo below shows how to do this. Note that it's very important to create the timer after the thread has started, otherwise it would be created in the wrong thread and its timer-events wouldn't be processed by the thread's event-loop. It's also important that all communication between the worker thread and the main thread is done via signals, so as to ensure thread-safety. Never try to directly perform GUI operations outside the main thread, as Qt does not support that at all. For the purposes of the demo, a second timer in the main thread is used to stop all processing after a fixed interval. If there was a GUI, user intervention via a button would achieve the same thing.

                  演示:

                  import sys
                  from PyQt5.QtCore import *
                  from PyQt5.QtWidgets import *
                  
                  class ModbusComWorker(QObject):
                      finished = pyqtSignal()
                  
                      def start(self):
                          self._timer = QTimer(self)
                          self._timer.timeout.connect(self.process)
                          self._timer.start(2000)
                  
                      def stop(self):
                          self._timer.stop()
                          self.finished.emit()
                  
                      def process(self):
                          print('processing (thread: %r)' % QThread.currentThread())
                          QThread.sleep(3)
                  
                  if __name__ == "__main__":
                  
                      app = QCoreApplication.instance()
                      if app is None:
                          app = QApplication(sys.argv)
                  
                      thread = QThread()
                      worker = ModbusComWorker()
                      worker.moveToThread(thread)
                  
                      def finish():
                          print('shutting down...')
                          thread.quit()
                          thread.wait()
                          app.quit()
                          print('stopped')
                  
                      worker.finished.connect(finish)
                      thread.started.connect(worker.start)
                      thread.start()
                  
                      timer = QTimer()
                      timer.setSingleShot(True)
                      timer.timeout.connect(worker.stop)
                      timer.start(15000)
                  
                      print('starting (thread: %r)' % QThread.currentThread())
                  
                      sys.exit(app.exec_())
                  

                  輸出:

                  starting (thread: <PyQt5.QtCore.QThread object at 0x7f980d096b98>)
                  processing (thread: <PyQt5.QtCore.QThread object at 0x7f980d0968a0>)
                  processing (thread: <PyQt5.QtCore.QThread object at 0x7f980d0968a0>)
                  processing (thread: <PyQt5.QtCore.QThread object at 0x7f980d0968a0>)
                  processing (thread: <PyQt5.QtCore.QThread object at 0x7f980d0968a0>)
                  processing (thread: <PyQt5.QtCore.QThread object at 0x7f980d0968a0>)
                  shutting down...
                  stopped
                  

                  這篇關(guān)于如何在單獨(dú)的 QThread 中使用 QTimer的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數(shù)綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動(dòng)后進(jìn)度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標(biāo)簽設(shè)置在固定位置,以便當(dāng)我向左或向右滾動(dòng)時(shí),yaxis 刻度標(biāo)簽應(yīng)該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構(gòu)造函數(shù)有未知關(guān)鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時(shí)顯示進(jìn)度條?)

                  <i id='CIcAG'><tr id='CIcAG'><dt id='CIcAG'><q id='CIcAG'><span id='CIcAG'><b id='CIcAG'><form id='CIcAG'><ins id='CIcAG'></ins><ul id='CIcAG'></ul><sub id='CIcAG'></sub></form><legend id='CIcAG'></legend><bdo id='CIcAG'><pre id='CIcAG'><center id='CIcAG'></center></pre></bdo></b><th id='CIcAG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='CIcAG'><tfoot id='CIcAG'></tfoot><dl id='CIcAG'><fieldset id='CIcAG'></fieldset></dl></div>

                      <small id='CIcAG'></small><noframes id='CIcAG'>

                        <tbody id='CIcAG'></tbody>
                      <legend id='CIcAG'><style id='CIcAG'><dir id='CIcAG'><q id='CIcAG'></q></dir></style></legend>
                        <bdo id='CIcAG'></bdo><ul id='CIcAG'></ul>
                        <tfoot id='CIcAG'></tfoot>
                          • 主站蜘蛛池模板: 九九综合 | 日韩久久精品 | 一区二区三区亚洲精品国 | 成人久久久 | 伊人超碰在线 | 91原创视频| 欧美黄色片 | 日韩在线看片 | 亚洲综合在线视频 | 亚洲欧美日韩精品久久亚洲区 | www.狠狠干 | 99视频网站 | 涩涩视频在线观看 | 欧美久久久久 | 亚洲精品成人av久久 | 涩爱av一区二区三区 | 视频一区二区三区四区五区 | 久久久久国产一区二区三区 | 日韩一区精品 | 国产精品亚洲成在人线 | 国产二区在线播放 | 国产片侵犯亲女视频播放 | 99精品久久久 | 国产精品自拍一区 | 国产精品人人做人人爽 | 人人九九精 | 一区中文字幕 | 一区二区三区在线免费观看 | 永久av | 久久国内精品 | 日韩综合在线 | 成人国产免费视频 | 精品二区 | 日韩在线观看中文字幕 | 欧美另类视频 | 超碰伊人| 亚洲狠狠爱 | 一区二区三区精品视频 | 国产在线视频在线观看 | 一级a爱片性色毛片免费 | 成人在线播放 |