PDA

View Full Version : invalid use of this in non member function



dineshkumar
19th January 2011, 10:27
hi i want to do a qt mobile apps which using network access but while compiling my code it shows the following errors.

i. invalid use of this in non member function
ii. connect was not declared in this scope

I have do the following in my code:

.pro file:


QT += network

main.cpp file


#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtNetwork>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));

manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));

#if defined(Q_WS_S60)
w.showMaximized();
#else
w.show();
#endif
return a.exec();
}


Is it any problem with my code please help me. Thanks.

wysota
19th January 2011, 10:31
connect() is a method of QObject and not a standalone function.

QObject::connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));

dineshkumar
19th January 2011, 10:37
hi thanks,
But it again shows the following error:

invalid use of this in non member function

wysota
19th January 2011, 10:53
There is no "this" in main().

nish
19th January 2011, 10:56
replyFinished(QNetworkReply*)
where is this function defined? you need that class object to be passed in connect instead of "this", the same as you pass "manager".

helloworld
19th January 2011, 13:38
It is probably easier if you move your code to the MainWindow instead:

main.cpp:


// main.cpp

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

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;

#if defined(Q_WS_S60)
w.showMaximized();
#else
w.show();
#endif

return a.exec();
}


mainwindow.h:


// mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

class QNetworkReply;

class MainWindow : public QMainWindow
{
Q_OBJECT

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

protected slots:
void replyFinished(QNetworkReply *reply);
};

#endif // MAINWINDOW_H


mainwindow.cpp


// mainwindow.cpp

#include <QtNetwork/QtNetwork>
#include <QtNetwork/QNetworkReply>
#include "mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));

manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));
}

MainWindow::~MainWindow()
{
}

void MainWindow::replyFinished(QNetworkReply *reply)
{
qDebug() << "ok!";
}

dineshkumar
20th January 2011, 05:19
Thankyou friend its works..

tylor2000
13th December 2015, 19:37
Thank you helloworld, this helped me out a lot. You will probably never see this but just wanted to thank you for the code example. It's still helping people out after all these years.