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?
-
Try this (notice QUrl& not QUrl) :
QObject::connect(ui>webView,SIGNAL(linkClicked(QUrl&)),MainWindow,SLOT(linkClicked(QUrl&)));Georg : & should be left out.cartman : No, it should be included. -
I changed
QObject::connectto onlyconnectand 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 : And also declaration of slots: changed to be with const... linkClicked(const QUrl &url) -
You state that it now works because you changed
QObject::connecttoconnect. Now I'm not 100% on this but I believe the reason for this is that by callingconnect, you are calling the method associated with an object which is part of your application. i.e. it's like doingthis->connect(...). That way, it is associated with an existing object - as opposed to calling the static methodQObject::connectwhich doesn't know anything about your application.Sorry if that's not clear, hopefully I got the point across!
-
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.
0 comments:
Post a Comment