Problem with QWebEnginePage::createWindow



  • Hello to everyone,
    I'm facing a problem I can't understand when using QWebEnginePage::createWindow and QWebEnginePage::acceptNavigationRequest in a somewhat particular way. Although the program I'm working on is quite complex, the issue can be seen with a much simpler application.

    The simplified program is made by a main window with a single tab widget, with each tab containing a QWebEngineView. The close button on the tab widget allows the user to close each page. The pages associated with the views are instances of class Page, derived from QWebEnginePage to allow to create new tabs. The usual way to allow web pages to open new tabs would be to do something like this:

    Page::Page(QTabWidget *tabs, QObject *parent): QWebEnginePage(parent), 
    m_tabWidget(tabs)
    {
    }
    
    QWebEnginePage* Page::createWindow(WebWindowType type){
        QWebEngineView *v = new QWebEngineView(m_tabWidget);
        m_tabWidget->insertTab(-1, v, "");
        Page *pg = new Page(m_tabWidget, v);
        v->setPage(pg);
        return pg;
    }
    

    Unfortunately, in my real application, I can't create the view (and the tab) from createWindow, because I need information which is only availlable from QWebEnginePage::acceptNavigationRequest. Because of this, I decided to separate the creation of the page from the creation of the view (and its insertion in the tab widget): the former is done in createWindow, while the latter happens in acceptNavigationRequest. This way,the above definition of createWindow is replaced with the following code:

    QWebEnginePage* Page::createWindow(WebWindowType type){
        return new Page(m_tabWidget, Q_NULLPTR);
    }
    
    bool Page::acceptNavigationRequest(const QUrl &, NavigationType, bool)
        QWebEngineView *v = new QWebEngineView(m_tabWidget);
        m_tabWidget->insertTab(-1, v, "");
        v->setPage(this);
        setParent(v);
        return true;
    }
    

    However, this doesn't work as expected, but I can't understand why. In particular:

    • opening a link using the middle mouse button doesn't always work. A simple page with a link like <a href="https://www.google.com">Google</a> creates a new tab with an empty page; opening the page https://www.kde.org and clicking with the middle button on any link correctly opens the page in a new tab. Choosing Open in a new window from the context menu always works correctly
    • in the first case above, if I use the close button on the second tab (the one with the empty page), the application crashes when deleting the view; in the second case it doesn't. The slot associated with the QTabWidget::tabCloseRequest signal is the following:
    void MainWindow::closeTab(int idx){
      QWidget *old = m_tabs->widget(idx);
      m_tabs->removeTab(idx);
      delete old;
    }
    
    • if I click on a link which opens a new window using javascript (window.open), a new tab is created with the new page as expected. However, when I close the first tab (the one which opened the new window), the page in the second tab disappears: there's no crash but the tab remains empty. Again, I can't understand how this could happen: the view thinks it's visible (isVisible returns true) and the page contains the correct HTML code, but nothing is shown. Removing the delete statement makes the problem disappear. If the new tab is opened using the middle mouse button or the context menu, everything works correctly

    I've been trying to solve this problem for several days, but with no success. Does anyone have any advice? Am I missing something? Could it be a bug with QWebEngine?

    Thanks in advance for any hints

    Stefano


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.