如何在我的代码中使用createWindow [Qt5.8]

时间:2017-04-02 18:43:33

标签: c++ qt qt5 qtwebengine qt5.8

在以下代码中,当网站请求新标签页或窗口时,它不会打印任何内容。如何让我的代码工作?请帮助,我太困惑了。谢谢!

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 个答案:

答案 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();
}