Jack
Jack

Reputation: 16724

connect() can't find signal function

I'm trying to set the connect() like this:

QObject::connect(&webControl,
                 SIGNAL(Ui::MainWindow::loadFinished(bool)),
                 &w,
                 SLOT(Ui::MainWindow::loadFinished(bool)));

in main() function but it give the error:

QObject::connect: No such signal QWebView::Ui::MainWindow::loadFinished(bool)

w and webControl are declared like this:

MainWindow w;
QWebView webControl;

And here's my files:

mainWindow.h

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void loadFinished(bool arg1);
private:
    Ui::MainWindow *ui;
};

mainWindow.cpp

void MainWindow::loadFinished(bool arg1)
{
}

Why I'm getting this error and how do I fix this?

Upvotes: 0

Views: 1044

Answers (3)

Megasa3
Megasa3

Reputation: 764

Your problem is that QWebView webControl; webcontrol is a qwebview and your signal is not in QWebview, but in MainWindow. You need that signal in QWebView. That is why its complaining about a signal that can't be found.

EDIT

You have a problem knowing what is a slot, and what a signal. Thy are two different things. A signal is like an alarm. An slot is the receiver and it works as a normal function.

If you want your webControl var to be the sender, then you have to declare the signal like this in y our QWebView.h class :

signals:
    void yourSignalName(bool arg1);

and use the connect like this:

QObject::connect(&webControl,
             SIGNAL(yourSignalName(bool)),
             &w,
             SLOT(loadFinished(bool)));

Upvotes: 1

Alexander Sorokin
Alexander Sorokin

Reputation: 741

You need to add QWebView *webView; to your mainwindow.h:

mainwindow.h

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QWebView *getWebView() const;

public slots:
    void loadFinished(bool arg1);
    void setWebView(QWebView *webControl);

private:
    Ui::MainWindow *ui;
    QWebView *webView;
};

mainwindow.cpp

...

QWebView *MainWindow::getWebView() const
{
    return webView;
}

void MainWindow::setWebView(QWebView *webControl)
{
    webView = webControl;
    QObject::connect(webControl,
                     SIGNAL(loadFinished(bool)),
                     this,
                     SLOT(loadFinished(bool)));
}

If you really need declaration of QWebView in main.cpp then pass pointer to setWebView() function:

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    QWebView webControl;
    w.setWebView(&webControl);

    w.show();

    return a.exec();
}

Upvotes: 2

Gusch5
Gusch5

Reputation: 137

I'd suggest the new Qt5 syntax (which is optional):

QObject::connect(&webControl, &Ui::MainWindow::loadFinished,
                 &w, &Ui::MainWindow::loadFinished);

more on the new syntax: http://wiki.qt.io/New_Signal_Slot_Syntax

Upvotes: 1

Related Questions