Qt: Having trouble answering the question QWebView :: linkClicked (QUrl) - slot

I am new to Qt.

I want to respond linkClicked

to QWebView

.

I tried to 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 with custom interface slots:

I have a slot file header declaration:

void linkClicked(QUrl &url);

      

in your cpp source file:

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

      

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

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

      

What is the correct way to handle this event?

+1


a source to share


3 answers


I QObject::connect

only changed to connect

and it works.

So this code works:



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

But I do not know why?

+1


a source


You are claiming that it now works because you changed QObject::connect

to connect

. Now I'm not 100% 100%, but I believe the reason for this is because by calling connect

, you are calling a method associated with an object that is part of your application. those. how to do it this->connect(...)

. Thus, it is linked to an existing object - as opposed to calling a static method QObject::connect

, which knows nothing about your application.



Sorry if this is not clear, I hope I have a point!

+2


a source


The use of QObject :: connect () and connect () is the same in this context. I believe

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

      

called from a function inside the MainWindow class. That's why when you tried

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

      

it works. Note the difference that makes it work - the third parameter. You used this in the second snippet, where is how you used MainWindow in the first snippet.

Read this one to learn how the signals and slots mechanism works and how to properly implement it.

+2


a source







All Articles