8

I have MainWindow and QNAMRedirect classes and I am trying to compile program but getting compiler error.

Here is QNAMRedirect class:

class QNAMRedirect :  public QObject
{
    Q_OBJECT
public:
    explicit QNAMRedirect(QObject *parent = 0);
    ~QNAMRedirect();

signals:

public slots:
    void doRequest();
    void replyFinished(QNetworkReply* reply);
signals:
    void finished(QString);

private:
        QPointer<QNetworkAccessManager> _qnam;
        QUrl _originalUrl;
        QUrl _urlRedirectedTo;
        QNetworkAccessManager* createQNAM();

};

and here is MainWindow class:

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_request_clicked();

private:
    Ui::MainWindow *ui;
};

and i am trying to connect NAMRedirect::finished(QString) signal to QTextEdit widget in MainWindow this way:

    void MainWindow::on_request_clicked()
{
    QNAMRedirect urlGet(this);
    QObject::connect(urlGet,SIGNAL(finished(QString)),ui->textEdit,SLOT(setText(QString)));

    urlGet.doRequest();

}

but i am getting compiler error:

error: no matching function for call to 'MainWindow::connect(QNAMRedirect&, const char*, QTextEdit*&, const char*)'

how can i fix that?

KolesnichenkoDS
  • 530
  • 1
  • 6
  • 20
Davit Tvildiani
  • 1,915
  • 3
  • 19
  • 29

1 Answers1

8

The reason for the compile error is that the two objects you pass to the connect() function need to be pointers. So using &urlGet (instead of just urlGet) will fix your compile error. However, as soon as your function returns this object will go out of scope and be destroyed, so I suggest you change your function to look something more like this:

QNAMRedirect *urlGet = new QNAMRedirect( this )
QObject::connect(urlGet,SIGNAL(finished(QString)),ui->textEdit,SLOT(setText(QString)));
urlGet->doRequest();

You will, of course, need to take measure that you're not leaking memory here.

Chris
  • 17,119
  • 5
  • 57
  • 60
  • 2
    It might be good to change your connections to use the new Qt5 syntax. Advantages is you get notification at compile time of any mismatch – Adam Miller Aug 08 '13 at 13:33