tags:

views:

1116

answers:

4

I am preaty new with QT.

I want to respond to linkClicked in QWebView...

I tried connect like this:

QObject::connect(ui->webView,SIGNAL(linkClicked(QUrl)),MainWindow,SLOT(linkClicked(QUrl)));

But I was getting error: C:/Documents and Settings/irfan/My Documents/browser1/mainwindow.cpp:9: error: expected primary-expression before ',' token

When I do this using UI Editing Signals Slots:

I have in header file declaration of slot:

void linkClicked(QUrl &url);

in source cpp file :

void MainWindow::linkClicked(QUrl &url)
{
   QMessageBox b;
   b.setText(url->toString());
   b.exec();
}

When I run this it compiles and runs but got a warning :

Object::connect: No such slot MainWindow::linkClicked(QUrl) in ui_mainwindow.h:100

What is propper way of doing this event handling?

A: 

Try this (notice QUrl& not QUrl) :

QObject::connect(ui>webView,SIGNAL(linkClicked(QUrl&)),MainWindow,SLOT(linkClicked(QUrl&)));
cartman
Georg
No, it should be included.
cartman
+1  A: 

I changed QObject::connect to only connect and it works.

So this code works:

connect(ui->webView,SIGNAL(linkClicked(const QUrl)),this,SLOT(linkClicked(const QUrl)),Qt::DirectConnection);

But I don't know why?

Irfan Mulic
Irfan Mulic
+2  A: 

You state that it now works because you changed QObject::connect to connect. Now I'm not 100% on this but I believe the reason for this is that by calling connect, you are calling the method associated with an object which is part of your application. i.e. it's like doing this->connect(...). That way, it is associated with an existing object - as opposed to calling the static method QObject::connect which doesn't know anything about your application.

Sorry if that's not clear, hopefully I got the point across!

+2  A: 

Using QObject::connect() and connect() is same in this context. I believe

QObject::connect(ui->webView,SIGNAL(linkClicked(QUrl)),
                 MainWindow,SLOT(linkClicked(QUrl)));

was called from a function inside MainWindow class. That is why when you tried

connect(ui->webView,SIGNAL(linkClicked(const QUrl)),
        this,SLOT(linkClicked(const QUrl)),Qt::DirectConnection);

it works. Notice the difference that make it work - the third parameter. You used this in the second snippet, where as you used MainWindow in the first snippet.

Read this to know how signals and slots mechanism works and how to properly implement it.

Donotalo