在以下代码中,当网站请求新标签页或窗口时,它不会打印任何内容。如何让我的代码工作?请帮助,我太困惑了。谢谢!
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));
}
答案 0 :(得分: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();
}