title: Howto use Qt QWebView::createWindow(QWebPage::WebWindowType type) correctly?
body: <p>I have a serious problem with QWebView::createWindow() and how to handle creating
  new browser window.</p>
  
  <p>I have sub-classed QWebView as told by docs (MyWebView) and reimplemented it's createWindow() method and also it's contextMenuEvent(QContextMenuEvent * event) method so that I could add and change my own menu entries. </p>
  
  <p>The context menu entries "Open Link", "Open Link in New Tab" and "Open Image" all work perfectly and either open the required link/image into same tab if "Open Link" is selected or into a new tab if one of the other three remaining "Open ..." entries is selected. Also, when clicking web pages that want to open in new window they will open in new tab, as expected. </p>
  
  <p>However, I added new menu entry called "Open Link in New Window" and for the life of me can't get it working. It opens completely new browser window but it does not load the clicked url. Even worse, if I manually enter some www address to this new browser window and then try to open new tabs they will open in the old window!!!</p>
  
  <p>Could someone please kindly check what is wrong with my code ?</p>
  
  <p>First, the MyWebView constructor:</p>
  
  <pre><code>MyWebView::MyWebView(QWidget * parent):
      QWebView(parent),newWindow(false)
  {
      this-&gt;myPage = new MyWebPage;
      this-&gt;setPage(this-&gt;myPage);
  
      /* Just change text and nothing more ... */
      this-&gt;action_openLinkInNewWindow = this-&gt;pageAction(QWebPage::OpenLinkInNewWindow);
      if(this-&gt;action_openLinkInNewWindow) {
          if(this-&gt;action_openLinkInNewWindow-&gt;isEnabled()) {
              this-&gt;action_openLinkInNewWindow-&gt;setText("Open Link in New Window");
          }
      }
  }
  </code></pre>
  
  <p>Then the "Open Image", "Open Link in New Tab" and "Open Link in New Window" functionality 
  (default "Open Link" worked just fine so I did not need to reimplement that one)</p>
  
  <pre><code>void    MyWebView::openImage()
  {
  #ifndef NDEBUG
      qDebug() &lt;&lt; "=================================================";
      qDebug() &lt;&lt; __FUNCTION__ &lt;&lt; " called ...";
  #endif
      this-&gt;imageUrl = true;
      this-&gt;newWindow = false;
      this-&gt;createWindow(QWebPage::WebBrowserWindow);
  }
  
  void    MyWebView::openLinkInNewTab()
  {
  #ifndef NDEBUG
      qDebug() &lt;&lt; "=================================================";
      qDebug() &lt;&lt; __FUNCTION__ &lt;&lt; " called ...";
  #endif
      this-&gt;linkUrl = true;
      this-&gt;newWindow = false;
      this-&gt;createWindow(QWebPage::WebBrowserWindow);
  }
  
  
  void    MyWebView::openLinkInNewWindow()
  {
  #ifndef NDEBUG
      qDebug() &lt;&lt; "=================================================";
      qDebug() &lt;&lt; __FUNCTION__ &lt;&lt; " called ...";
  #endif
      this-&gt;linkUrl = true;
      this-&gt;newWindow = true;
      this-&gt;createWindow(QWebPage::WebBrowserWindow);
  }
  </code></pre>
  
  <p>Then, my reimplemented contextMenuEvent()</p>
  
  <pre><code>void MyWebView::contextMenuEvent(QContextMenuEvent * event)
  {
  #ifndef NDEBUG
      qDebug() &lt;&lt; "=================================================";
      qDebug() &lt;&lt; __FUNCTION__ &lt;&lt; " called ...";
  #endif
  
      this-&gt;rel_pos = event-&gt;pos();
      QWebHitTestResult r = page()-&gt;mainFrame()-&gt;hitTestContent(event-&gt;pos());
  
      QMenu menu(this);
      if (!r.linkUrl().isEmpty()) {
          menu.addAction(pageAction(QWebPage::OpenLink));
          menu.addAction(tr("Open Link in New Tab"), this, SLOT(openLinkInNewTab()));
          menu.addAction(tr("Open Link in New Window"),this,SLOT(openLinkInNewWindow()));
  
          menu.addAction(pageAction(QWebPage::DownloadLinkToDisk));
          menu.addAction(pageAction(QWebPage::CopyLinkToClipboard));
          menu.addSeparator();
      }
  
      if(!r.imageUrl().isEmpty()) {
          menu.addAction(tr("Open Image"),this,SLOT(openImage()));
          menu.addAction(pageAction(QWebPage::DownloadImageToDisk));
          menu.addAction(pageAction(QWebPage::CopyImageToClipboard));
          menu.addAction(pageAction(QWebPage::CopyImageUrlToClipboard));
      }
  
      if(!r.linkUrl().isEmpty() || !r.imageUrl().isEmpty()) {
          menu.exec(mapToGlobal(rel_pos));
          return;
      }
      QWebView::contextMenuEvent(event);
  }
  </code></pre>
  
  <p>And finally, the problematic createWindow()</p>
  
  <pre><code>QWebView*  MyWebView::createWindow(QWebPage::WebWindowType type)
  {
  #ifndef NDEBUG
      qDebug() &lt;&lt; "=================================================";
      qDebug() &lt;&lt; __FUNCTION__ &lt;&lt; " called ...";
  #endif
      switch(type) {
          case    QWebPage::WebBrowserWindow:
          {
              QWebHitTestResult  hitTest = this-&gt;page()-&gt;mainFrame()-&gt;hitTestContent(rel_pos);
              QUrl    url;
              if(this-&gt;linkUrl) {
                      url = hitTest.linkUrl();
                      this-&gt;linkUrl = false;
              }
  
              if(this-&gt;imageUrl) {
                      url = hitTest.imageUrl();
                      this-&gt;imageUrl = false;
              }
  
              if(newWindow) {
                  /* This does not work!!!!*/
                      MainWindow *mw = new MainWindow;
                      mw -&gt;show();
                      return mw-&gt;currentTab();
                  } else {
                      MyBrowser*  browser = new MyBrowser(url);
                      TabbedBrowser::getInstance()-&gt;newTab(browser,"Loading ...");
                      return browser-&gt;getwebView();
                  }
          }
          break;
      }
      return  QWebView::createWindow(type);
  }
  </code></pre>
  
  <p>If there is anything else with my code, besides createWindow() then I would gladly 
  hear.
  Thank you!</p>