PDA

View Full Version : linkClicked signal



maston
5th September 2010, 21:02
Hi.

How I can determine which webview widget emitted linkClicked signal?

I have tabWidget with WebView *wv = new WebView() create after triggered action in new tabBar. And i don't know which widget emitted signal :(

Please help
Maston

Lykurg
5th September 2010, 22:23
You can use QObject::sender() but that breaks the oo design.

maston
7th September 2010, 16:44
You can use QObject::sender() but that breaks the oo design.


THank You... but how can I access to webview by the QObject.sender()?
for example, i have to do webview->load(myclickedurl);

Lykurg
7th September 2010, 17:02
You have to cast sender() to QWebView. See qobject_cast.

maston
7th September 2010, 20:13
it's working but sender is always MainWindow.

i have something like this :


connect( nwb->page() , SIGNAL( linkClicked(const QUrl &)), this, SLOT(link_click(const QUrl &)));

and slot:
void MainWindow::link_click(const QUrl & ok)
{
. . .
}



nwb is QWebView. That's problem with sender? slot in MainWindow class?

tbscope
7th September 2010, 20:15
Is it possible to use a signal mapper?

maston
7th September 2010, 20:26
signal mapper is declared in constructor right? my WebView is created by QTCreator ( ui_mainwindow.h ) .so i don't have access to constructor class.

Or I don't know how to do it... yet :) Some advice?

tbscope
7th September 2010, 20:30
You define the mapper in your main window.
Then you connect the linkClicked signal to the mapper map() slot.
You set a mapping, like the pointer to the webview
Then you connect the mapped signal to your custom slot having instant access to the calling webview.

maston
7th September 2010, 20:52
thank You...do You know how change argumets in mapped() signal? i have this:

QObject::connect: Incompatible sender/receiver arguments
QSignalMapper::mapped(QObject*) --> MainWindow::link_click(QUrl)

tbscope
7th September 2010, 20:55
I'm sorry, I'm an idiot.
A signal mapper will not work here since you can only use signals with no parameters.

tbscope
7th September 2010, 21:01
Since I'm being stupid, here's another stupid idea ;-)

For each webview, connect to a unique slot.
This works if you have a small static amount of webviews. Not for a dynamic amount.


But, using sender() should not return the main window.


QWebPage *theWebPageWhoSentTheSignal = qobject_cast<QWebPage *>(sender());

If (theWebPageWhoSentTheSignal ) {
...
}

maston
7th September 2010, 21:10
it's exactly what i wrote earlier :( and returns MainWindow.
How for example chrome know which webview linkclicked signal was emitted? it is possible :) i am seeking further :) thank You anyway :)

tbscope
7th September 2010, 21:18
it's exactly what i wrote earlier :( and returns MainWindow.

That's not possible!

A qobject_cast returns 0 if it can't cast the object. You can not cast a QWebPage to a QMainWindow.
It would return 0 in your case.


connect( nwb->page() , SIGNAL( linkClicked(const QUrl &)), this, SLOT(link_click(const QUrl &)));

void MainWindow::link_click(const QUrl & ok)
{
QWebPage *theWebPageWhoSentTheSignal = qobject_cast<QWebPage *>(sender()); //<-- this can NEVER return the main window. The only two possibilities are a pointer to a QWebPage or 0.

If (theWebPageWhoSentTheSignal) {
// the if(...) check if the page pointer is 0, if not, you can use it to do something.
}
}

maston
7th September 2010, 21:26
You have right :) Now I'm stupid :D



If (theWebPageWhoSentTheSignal) {
qDebug() << QObject::objectName().toUtf8();
}


this print MainWindow , but theWebPageWhoSentTheSignal must be QWebPage because qDebug is working... sorry :)

edit2:



if(theWebPageWhoSentTheSignal ) {

QObject *obj = theWebPageWhoSentTheSignal->parent();
QWebView *okobj = qobject_cast<QWebView *>(obj);
okobj->load(url);
}