QT there is no corresponding function to call "MainWindow :: connect ()

I have the MainWindow and QNAMRedirect , and I'm trying to compile a program, but I get a compiler error.

Here is the 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 the 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'm trying to connect the NAMRedirect::finished(QString) signal to the QTextEdit widget in MainWindow as follows:

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

but I get a compiler error:

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

how can i fix this?

+7
source share
1 answer

The reason for the compilation error is that the two objects you pass to the connect () function must be pointers. So using & urlGet (and not just urlGet) will fix your compilation 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 like this:

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

You, of course, will need to take measures so that you do not leak memory here.

+7
source

All Articles