I have a serious problem with QWebView::createWindow() and how to handle creating new browser window.
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.
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.
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!!!
Could someone please kindly check what is wrong with my code ?
First, the MyWebView constructor:
MyWebView::MyWebView(QWidget * parent):
QWebView(parent),newWindow(false)
{
this->myPage = new MyWebPage;
this->setPage(this->myPage);
/* Just change text and nothing more ... */
this->action_openLinkInNewWindow = this->pageAction(QWebPage::OpenLinkInNewWindow);
if(this->action_openLinkInNewWindow) {
if(this->action_openLinkInNewWindow->isEnabled()) {
this->action_openLinkInNewWindow->setText("Open Link in New Window");
}
}
}
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)
void MyWebView::openImage()
{
#ifndef NDEBUG
qDebug() << "=================================================";
qDebug() << __FUNCTION__ << " called ...";
#endif
this->imageUrl = true;
this->newWindow = false;
this->createWindow(QWebPage::WebBrowserWindow);
}
void MyWebView::openLinkInNewTab()
{
#ifndef NDEBUG
qDebug() << "=================================================";
qDebug() << __FUNCTION__ << " called ...";
#endif
this->linkUrl = true;
this->newWindow = false;
this->createWindow(QWebPage::WebBrowserWindow);
}
void MyWebView::openLinkInNewWindow()
{
#ifndef NDEBUG
qDebug() << "=================================================";
qDebug() << __FUNCTION__ << " called ...";
#endif
this->linkUrl = true;
this->newWindow = true;
this->createWindow(QWebPage::WebBrowserWindow);
}
Then, my reimplemented contextMenuEvent()
void MyWebView::contextMenuEvent(QContextMenuEvent * event)
{
#ifndef NDEBUG
qDebug() << "=================================================";
qDebug() << __FUNCTION__ << " called ...";
#endif
this->rel_pos = event->pos();
QWebHitTestResult r = page()->mainFrame()->hitTestContent(event->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);
}
And finally, the problematic createWindow()
QWebView* MyWebView::createWindow(QWebPage::WebWindowType type)
{
#ifndef NDEBUG
qDebug() << "=================================================";
qDebug() << __FUNCTION__ << " called ...";
#endif
switch(type) {
case QWebPage::WebBrowserWindow:
{
QWebHitTestResult hitTest = this->page()->mainFrame()->hitTestContent(rel_pos);
QUrl url;
if(this->linkUrl) {
url = hitTest.linkUrl();
this->linkUrl = false;
}
if(this->imageUrl) {
url = hitTest.imageUrl();
this->imageUrl = false;
}
if(newWindow) {
/* This does not work!!!!*/
MainWindow *mw = new MainWindow;
mw ->show();
return mw->currentTab();
} else {
MyBrowser* browser = new MyBrowser(url);
TabbedBrowser::getInstance()->newTab(browser,"Loading ...");
return browser->getwebView();
}
}
break;
}
return QWebView::createWindow(type);
}
If there is anything else with my code, besides createWindow() then I would gladly hear. Thank you!