2017-11-02 17 views
0

QWebEnginePage 객체에서 HTML 코드를 가져 오려고합니다. Qt 참조에 따르면 QWebEnginePage 객체의 'toHtml'은 아래와 같은 비동기 메소드입니다.(PyQt5.9) QWebEnginePage의 객체 인 'toHtml'메서드를 동 기적으로 호출 할 수있는 방법이 있습니까?

비동기 방법은 HTML 및 BODY 태그 안에 HTML로 페이지의 내용을 검색 할 수 있습니다. 성공적으로 완료되면 resultCallback이 페이지의 내용과 함께 호출됩니다.

그래서이 메소드를 동 기적으로 호출하는 방법을 찾으려고했습니다.

내가 얻고 싶은 결과는 아래에 있습니다.

class MainWindow(QWidget): 
    html = None 
    ... 
    ... 
    def store_html(self, data): 
    self.html = data 

    def get_html(self): 
    current_page = self.web_view.page() 
    current_page.toHtml(self.store_html) 
    # I want to wait until the 'store_html' method is finished 
    # but the 'toHtml' is called asynchronously, return None when try to return self.html value like below. 
    return self.html 
    ... 
    ... 

감사합니다.

좋은 하루 되세요.

+0

왜 이것을 원하는지 분명하지 않습니다. QWebEngine은 Blink를 기반으로합니다. Blink는 웹 콘텐트를위한 별도의 프로세스를 실행합니다. (최신 브라우저와 마찬가지입니다.) 프로세스 간의 IPC 호출에 시간이 걸릴 수 있으므로 QWebEngine은 콜백 함수를 정의하여 메인 프로세스의 이벤트 루프를 계속 진행할 수 있도록 요청합니다 IPC 호출이 완료되는 동안. 따라서이 질문에 대한 정당성을 알지 못하면 어둠 속에서 가능한 가장 좋은 대답을 제공하는 것이 뻔뻔 ​​스러울 것입니다. – MrEricSir

+0

@MrEricSir QWebEngine이 Blink 프레임 워크의 기반이라는 사실을 알지 못했습니다. html 응답을받은 후 웹 뷰 화면의 내용을 변환하기를 원했을뿐입니다. 답변 해 주셔서 감사합니다. –

답변

0

해당 동작을 얻는 간단한 방법은 QEventLoop()을 사용하는 것입니다. 이 클래스의 객체를 사용하면 exec_() 이후의 코드가 실행되지 않으며 GUI가 계속 작동하지 않는다는 의미는 아닙니다.

class Widget(QWidget): 
    toHtmlFinished = pyqtSignal() 

    def __init__(self, *args, **kwargs): 
     QWidget.__init__(self, *args, **kwargs) 
     self.setLayout(QVBoxLayout()) 
     self.web_view = QWebEngineView(self) 
     self.web_view.load(QUrl("http://doc.qt.io/qt-5/qeventloop.html")) 
     btn = QPushButton("Get HTML", self) 
     self.layout().addWidget(self.web_view) 
     self.layout().addWidget(btn) 
     btn.clicked.connect(self.get_html) 
     self.html = "" 

    def store_html(self, html): 
     self.html = html 
     self.toHtmlFinished.emit() 

    def get_html(self): 
     current_page = self.web_view.page() 
     current_page.toHtml(self.store_html) 
     loop = QEventLoop() 
     self.toHtmlFinished.connect(loop.quit) 
     loop.exec_() 
     print(self.html) 


if __name__ == '__main__': 
    import sys 
    app = QApplication(sys.argv) 
    w = Widget() 
    w.show() 
    sys.exit(app.exec_()) 
+0

정말 고마워요. 당신의 대답은 나에게 매우 도움이된다. 좋은 하루 되세요! –