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:

Qt Code:
  1. Page::Page(QTabWidget *tabs, QObject *parent): QWebEnginePage(parent),
  2. m_tabWidget(tabs)
  3. {
  4. }
  5.  
  6. QWebEnginePage* Page::createWindow(WebWindowType type){
  7. QWebEngineView *v = new QWebEngineView(m_tabWidget);
  8. m_tabWidget->insertTab(-1, v, "");
  9. Page *pg = new Page(m_tabWidget, v);
  10. v->setPage(pg);
  11. return pg;
  12. }
To copy to clipboard, switch view to plain text mode 
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:

Qt Code:
  1. QWebEnginePage* Page::createWindow(WebWindowType type){
  2. return new Page(m_tabWidget, Q_NULLPTR);
  3. }
  4.  
  5. bool Page::acceptNavigationRequest(const QUrl &, NavigationType, bool)
  6. QWebEngineView *v = new QWebEngineView(m_tabWidget);
  7. m_tabWidget->insertTab(-1, v, "");
  8. v->setPage(this);
  9. setParent(v);
  10. return true;
  11. }
To copy to clipboard, switch view to plain text mode 

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:

    Qt Code:
    1. void MainWindow::closeTab(int idx){
    2. QWidget *old = m_tabs->widget(idx);
    3. m_tabs->removeTab(idx);
    4. delete old;
    5. }
    To copy to clipboard, switch view to plain text mode 
  • 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