how I use QFtp

Started by Mejda, May 14, 2012, 06:39:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mejda

hello,
I use Qftp when I instantiate this class it is ok but when do an appel to a function like(ftp.connectToHost("ftp.trolltech.com"); compilator send me this error(error: expected constructor, destructor, or type conversion before '.' token)
thank you

Liviu Lalescu

#1
Quote from Qt documentation on http://qt-project.org/doc/qt-4.8/qftp.html#details :

QuoteThis class provides a direct interface to FTP that allows you to have more control over the requests. However, for new applications, it is recommended to use QNetworkAccessManager and QNetworkReply, as those classes possess a simpler, yet more powerful API.

In newer FET versions, I switched to QNetworkAccessManager (I used QHttp previously).

Please see also Qt documentation: http://qt-project.org/doc/qt-4.8/classes.html , see entries for QFtp ( http://qt-project.org/doc/qt-4.8/qftp.html )  and QNetworkAccessManager ( http://qt-project.org/doc/qt-4.8/qnetworkaccessmanager.html ). But I strongly recommend you to go with the new class (QNetworkAccessManager).

I recommend you to upgrade to latest Qt (4.8.1) and latest FET (5.18.0).

About your exact problem, if you have:


#include <QFtp>
...
QFtp ftp;
ftp.connectToHost("ftp.trolltech.com");


it should compile.

Mejda

OK  I add in my programm   #include <QNetworkAccessManager> and I write those instructions
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
         this, SLOT(replyFinished(QNetworkReply*)));

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

but I have same errors  ": error: expected constructor, destructor, or type conversion before '(' token  : this error in  line "connect (manager, SIGNAL ..."
My Question is when I use this class it is sufficient to include it by #include .. or I must do others thinks
thank you  :)

Liviu Lalescu

I think that the error is that you should do an "#include <QNetworkReply>". And probably in your first post you needed an "#include <QFtp>".

Please let me know if this solves your problem.

Mejda

Good morning,
look I do those include
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>


QNetworkAccessManager* nam = new QNetworkAccessManager();

QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
         this, SLOT(finishedSlot(QNetworkReply*)));
but I have this error ": error: expected constructor, destructor, or type conversion before '(' token"  this error is also in line where I do the " QObject::connect(nam, SIGNAL(finished...."
thank you  :)

Liviu Lalescu

Hi,

Hmm, maybe from SIGNAL and SLOT macros.

Please see http://qt-project.org/doc/qt-4.8/signalsandslots.html#signals-and-slots . I'll write you a resume (but I might forget something, so please check the Qt documentation link):

1) Maybe you need to have your class be a subclass of QObject. But might not be true.
2) I think you need your class to begin with Q_OBJECT macro.
3) I think you need to declare your slot with "slots" word, like in the link above.

All these must be done in the function declaration (in the header, .h). I hope I do not mix function declaration with function definition. I was never sure which is which :-)

Please let me know.

Mejda

Hi,
I do this

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

void TimetableExport::replyFinished(QNetworkReply *reply)
{
     reply = qobject_cast<QNetworkReply*>(sender());

     if (reply) {
         if (reply->error() == QNetworkReply::NoError) {
             QString myCustomData = reply->property("mycustomdata").toString();
         }

         reply->deleteLater();
     }

}
and I declare this function (replyFinished(QNetworkReply *reply)) like slot in my header
I do this

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

My class herite from QObject 

class TimetableExport: public QObject{
   Q_OBJECT

but I have the same error  in connect  :(

Liviu Lalescu

You do not need to declare that signal.

Please attach here your code (archived) and I'll try to make it compile and send it back.

Mejda

Hi,
there are two class where I do connexion

Liviu Lalescu

The attached is compiling with FET-5.16.1. But I don't know if it is correct.

Mejda

thank you Mr Liviu it compile true now  :)
Now all my work (send request to server etc  will be in this function ftpconnction()?)

Liviu Lalescu

You can work in TimetableExport::ftpConnection(...) to do the initialization and in TimetableExport::replyFinished(...) to work on the data after the network operation was completed.

Be careful that you may need to uncomment lines 79 and 80 (I commented these #includes so that I could compile).

Mejda

Hi Mr Liviu,
please can you help me to test my program when I do an appel to this function

void TimetableExport::ftpConnection()
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
connect(manager, SIGNAL(finished(QNetworkReply *)),
         this, SLOT(replyFinished(QNetworkReply *)));
  QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("http://someurl.com")));

QString url ="http://localhost/upload.php";
QByteArray line;
QNetworkRequest request(url);
QFile file("/home/mejda/timetables/BEN ARBIA ANIS/BEN ARBIA ANIS.html");
while(!file.atEnd())
     {
         line.append(file.readLine());
     }
     file.close();
     //manager->authenticationRequired(reply,authentificator);
     manager->post(request, line);
     cout<<"fin du upload";
}
in end to execution I show this message (fin du upload) so appel is correct but when I go to browser and copy this adress(http://localhost/upload.php) I have response of server like this :

Not Found

The requested URL /upload.php was not found on this server.
Apache/2.2.14 (Ubuntu) Server at localhost Port 80

can you give me a true adress  :)

Liviu Lalescu

#13
Please call me Liviu :)

First of all, I would recommend you not to use spaces in the file names.

I am sorry, but I am not skilled in networking. The only networking I do in FET is, in case the user enables automatic search for new versions of FET, it will open the file http://lalescu.ro/liviu/fet/crtversion/crtversion.txt , which is updated by me each time I release a new FET version, and FET will report if the user's version and the latest official version differ. I just get the file in FET using the HTPP protocol.

Please see the file interface/fetmainform.cpp to see how I do my minor networking thing (search for the strings "crtversion.txt", "QNetworkAccessManager" or "QNetworkReply").

PS: I might consider moving this topic to the board http://lalescu.ro/liviu/fet/forum/index.php?board=1.0 ("Programming and other Technical Help" from the "Discussions and Chat" category), since it does not concern exactly FET usage/modification, but more like another C++ project. Please let me know if you agree.

PPS: There is a special tag here on the forum to enclose C++ code, it is a button with the '#' sign on it. You need to enclose your code between this tag and your code will look nicer on the forum (I mean if you write directly code in the message, not when attaching it).

Mejda

yes of course I am agree with you   :)