如何在我的代码中使用 createWindow [Qt5.8]
Posted
技术标签:
【中文标题】如何在我的代码中使用 createWindow [Qt5.8]【英文标题】:How to use createWindow in my code [Qt5.8] 【发布时间】:2017-04-02 18:43:33 【问题描述】:在以下代码中,当站点请求新选项卡或窗口时,它不会打印任何内容。如何使我的代码正常工作?请帮助,我太困惑了。谢谢!
class WebView:public QWebEngineView
protected:
QWebEngineView* createWindow(QWebEnginePage::WebWindowType type)
switch(type)
case QWebEnginePage::WebBrowserTab:
std::cout<<"Tab Requested"<<std::endl;
return new QWebEngineView();
case QWebEnginePage::WebBrowserBackgroundTab:
std::cout<<"Background Tab Requested"<<std::endl;
return new QWebEngineView();
case QWebEnginePage::WebBrowserWindow:
std::cout<<"window Requested"<<std::endl;
return new QWebEngineView();
case QWebEnginePage::WebDialog:
std::cout<<"popup Requested"<<std::endl;
return new QWebEngineView();
return nullptr;
public:
QString home_page;
QWebEngineView* view=new QWebEngineView();
WebView(QString page=QString("https://google.com"))
this->home_page=page;
this->view->settings()->setAttribute(QWebEngineSettings::javascriptCanOpenWindows,true);
createWebView();
void createWebView()
this->view->load(QUrl(this->home_page));
【问题讨论】:
【参考方案1】:createWindow
函数是为WebView
类的成员实现的,而不是为view
成员实现的。
#include <QApplication>
#include <QWebEngineView>
#include <QWebEngineSettings>
#include <iostream>
class WebView : public QWebEngineView
public:
WebView(QString page=QString("https://google.com"), QWidget *parent = Q_NULLPTR):QWebEngineView(parent)
home_page = page;
settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, true);
void createWebView()
load(QUrl(home_page));
protected:
QWebEngineView* createWindow(QWebEnginePage::WebWindowType type)
switch(type)
case QWebEnginePage::WebBrowserTab:
std::cout<<"Tab Requested"<<std::endl;
return new QWebEngineView();
case QWebEnginePage::WebBrowserBackgroundTab:
std::cout<<"Background Tab Requested"<<std::endl;
return new QWebEngineView();
case QWebEnginePage::WebBrowserWindow:
std::cout<<"window Requested"<<std::endl;
return new QWebEngineView();
case QWebEnginePage::WebDialog:
std::cout<<"popup Requested"<<std::endl;
return new QWebEngineView();
return nullptr;
private:
QString home_page;
;
int main(int argc, char *argv[])
QApplication a(argc, argv);
WebView web;
web.createWebView();
web.show();
return a.exec();
【讨论】:
以上是关于如何在我的代码中使用 createWindow [Qt5.8]的主要内容,如果未能解决你的问题,请参考以下文章