View Full Version : Convert html links in widget actions
jiveaxe
15th November 2009, 17:38
Hi,
as in the title, is it possible clicking a link in a QWebView and perform an action in qt? I have attached an image to explain what I mean; in "Tab 1" there is a qwebview with three links; now when cliking on one of them should be activated the corresponding Tab.
Thanks
Lykurg
15th November 2009, 17:52
Use the signal QWebView::linkClicked() and react on it. E.g.: href="#myAction::doOpenTab2".
jiveaxe
15th November 2009, 18:32
First of all thanks for the quick reply; I'm trying to follow your suggestion but I don't know what should be '#myAction::' :o
Edit:
I've added this line:
connect(ui->webView,SIGNAL(linkClicked(QUrl)), this, SLOT(openTab(QUrl)));
but seems that linkClicked() signal isn't emitted; I've tryed with this simple code:
void MainWindow::openTab(QUrl u)
{
qDebug() << "you have clicked a link...";
}
but no output in terminal.
Lykurg
15th November 2009, 19:13
I'm trying to follow your suggestion but I don't know what should be '#myAction::' :o
It's just a magic string you could parse in your slot and react on its value.
For your other problem look that you have the right linkDelegationPolicy.
jiveaxe
16th November 2009, 12:17
For your other problem look that you have the right linkDelegationPolicy.
Perhaps I'm wrong but it is not possible to set linkDelegationPolicy for a QWebView; it seems a property of QWebPage, that isn't a widget.
EDIT:
I've added the following lines:
page = new QWebPage;
page->setView(ui->webView);
page->setLinkDelegationPolicy(QWebPage::DelegateAllLinks );
now, both QWebView and QWebPage have setHtml() and linkClicked(): what is the right combination to use? I've tryed using setHtml() of QWebView and linkClicked() of QWebView the first time and of QWebPage the second but with no success.
EDIT 2:
Forget my last edit, have found QWebView::page() so adding this line:
ui->webView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
now it works; two final questions:
1) the url returned with linkClicked() contains 'about:blank...': is it possible avoid it?
2) now the function that parses the clicked url is a sequence of if...else; can you suggest me a more elegant way?
Very thanks
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.