pyQt如何防止QWebEngine冻结

时间:2017-06-09 21:05:50

标签: python pyqt freeze qthread

我有一个仅包含QWebEngineView的Gui,用户交互功能是用html和javascript编写的。

首先我的应用:

# Create an application
app = QApplication([])

win = QWidget()
layout = QVBoxLayout()

# Create and fill a QWebView
view = QWebEngineView()
view.load(QUrl.fromLocalFile(os.path.abspath('templates/index.html')))

# Create a channel
channel = QWebChannel()
handler = CallHandler()
channel.registerObject('bridge', handler) # i called my channel 'bridge' so i can communicate with pyQt slots in my WebEngine

# Register channel
view.page().setWebChannel(channel)

# Add the QWebView to the layout
layout.addWidget(view)

win.setLayout(layout)
win.setWindowTitle('Test')
win.setMinimumSize(1280, 720)

# Show the window
win.show()

# Run the app
app.exec_()

问题是当我在我的JS代码中调用我的CallHandler类的方法时,Gui会冻结,直到该方法完成。我的期望是在python代码运行时用加载微调器显示dom,在完成之后我会隐藏微调器。以下是javascript中方法的调用:

enter image description here

connectedCallback() {
  console.log("custom element inserted into DOM.");

  // append template to custom element
  const tmpl = document.currentScript.ownerDocument.querySelector('#main-window');
  this.appendChild(tmpl.content.cloneNode(true));

  // backend listener
  if(window.hasOwnProperty("bridge")) {
      window.bridge.statistic_table_data_to_client.connect((data, that) => this.initStatisticTable(data, this));
      window.bridge.games_table_data_to_client.connect((data, that) => this.initGamesTable(data, this));

      $(".spinner").toggle();
      window.bridge.collect_games_table_data(); // this is the call to a pyQt slot
      $(".spinner").toggle();
  }
}

我的CallHandler:

class CallHandler(QObject):

    @pyqtSlot()
    def collect_games_table_data(self):
        time.sleep(10) # after this command the gui isn't freezed anymore

我尝试了QThreadapp.processEvents()QEventLoop,但我认为我以错误的方式使用了它。 有人可以请我帮忙吗?

0 个答案:

没有答案