FET Forum

Discussions and Chat => Programming and other Technical Help => Topic started by: Mejda on May 14, 2012, 06:39:53 PM

Title: how I use QFtp
Post by: Mejda on May 14, 2012, 06:39:53 PM
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
Title: Re: how I use QFtp
Post by: Liviu Lalescu on May 14, 2012, 06:48:34 PM
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.
Title: Re: how I use QFtp
Post by: Mejda on May 14, 2012, 10:30:01 PM
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  :)
Title: Re: how I use QFtp
Post by: Liviu Lalescu on May 15, 2012, 05:16:12 AM
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.
Title: Re: how I use QFtp
Post by: Mejda on May 15, 2012, 08:20:58 AM
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  :)
Title: Re: how I use QFtp
Post by: Liviu Lalescu on May 15, 2012, 08:45:04 AM
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.
Title: Re: how I use QFtp
Post by: Mejda on May 15, 2012, 09:45:44 AM
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  :(
Title: Re: how I use QFtp
Post by: Liviu Lalescu on May 15, 2012, 09:55:18 AM
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.
Title: Re: how I use QFtp
Post by: Mejda on May 15, 2012, 10:21:31 AM
Hi,
there are two class where I do connexion
Title: Re: how I use QFtp
Post by: Liviu Lalescu on May 15, 2012, 01:23:28 PM
The attached is compiling with FET-5.16.1. But I don't know if it is correct.
Title: Re: how I use QFtp
Post by: Mejda on May 15, 2012, 01:45:23 PM
thank you Mr Liviu it compile true now  :)
Now all my work (send request to server etc  will be in this function ftpconnction()?)
Title: Re: how I use QFtp
Post by: Liviu Lalescu on May 15, 2012, 02:50:39 PM
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).
Title: Re: how I use QFtp
Post by: Mejda on May 16, 2012, 08:08:58 AM
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  :)
Title: Re: how I use QFtp
Post by: Liviu Lalescu on May 16, 2012, 08:39:11 AM
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).
Title: Re: how I use QFtp
Post by: Mejda on May 16, 2012, 08:53:15 AM
yes of course I am agree with you   :)
Title: Re: how I use QFtp
Post by: Mejda on May 19, 2012, 03:50:07 PM
Hi Liviu,
I know you are not a human network but I ask because you use signals and slots so when I use in QFtp  class  methode connecttohost I should link it to a signal.
thank you :)
Title: Re: how I use QFtp
Post by: Liviu Lalescu on May 19, 2012, 04:09:31 PM
Hello :)

Of course, I'll try to help as my time permits, but you also need to go through the Qt documentation when necessary and read existing FET code.

Referring to code for FET version 5.18.0, in file src/interface/fetmainform.cpp:

In lines 519-526, if the user selected that FET should check for updates, so FET needs to get the crtversion.txt file from the FET homepage, I prepare for getting the file. When the file will be available (the program got it from the server), the finished signal will be emitted and the replyFinished slot will be called (see line 521). The signal belongs to the networkManager, the slot belongs to the FetMainForm.

Then, when the slot is called, I work on the file which was retrieved from the server (see lines 642-704).

Was that your question, or you meant something else?

Please let me know.
Title: Re: how I use QFtp
Post by: Mejda on May 19, 2012, 04:35:06 PM
Hi,
yes I refer to documetation of Qt and also class fetmainform but I inderstand that when you called a local function (in your case get) you don't link it to a signal
and my case I just use a local functions to QFtp class (like connect to host, put etc)  so I am not forced to link those function (connect to host,put,...) to signal
bref this is my code :

QFtp *ftp = new QFtp(this);
     //connect(ftp,SIGNAL(commandFinished ( int, bool)),this,SLOT(fincommande()));
     ftp->connectToHost("ftp.icwus.net");
     ftp->login("mylogin","passwd");
     ftp->cd("acrgafsa");
     int i = 0;
     QString filename;
     QString files = QFileDialog::getOpenFileName(this, tr("Select File"),
     "D://");

     if (files.isEmpty())
     return;

     while(files.indexOf("/", i) != (-1)) //find the file name not include the full url
     i = files.indexOf("/",i) + 1;
     while(i < files.size())
     {
     filename.append(files.at(i));
     i++;
     }

     QFile *file = new QFile(filename);
     Q_ASSERT(file != NULL);

     if(!file->open(QIODevice::ReadWrite)) //finished create the file in the debug file
     {
     qDebug() << "opern err" << file->fileName();
     return;
     }
      qDebug() << "start upload";
     ftp->put(file,filename);
     qDebug() << "fin upload";
     ftp->close();


I have in output of program "start upload" and "fin upload" but in server I have'nt any file uploaded
I am not inderstand where the problem in my program
thank you for response :)
Title: Re: how I use QFtp
Post by: Liviu Lalescu on May 19, 2012, 05:42:30 PM
I think that the ftp->put command will exit immediately, and the ftp object will emit a 'done' or similar signal when finished, and you need to connect that signal to a slot, and in the slot you can do your work (you enter this slot after the transfer was done). But I am not sure.

If you didn't work too much with QFtp, Qt recommends to use QNetworkAccessManager instead, so a good thing would be to change to the recommended class.

I hope other programmers here can help you with better advice than me.

PS: I remind you: 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).

PPS: The correct English sentence is "I do not understand" (please forgive me for the correction :) )
Title: Re: how I use QFtp
Post by: Liviu Lalescu on May 19, 2012, 08:54:17 PM
Off-topic replies have been redirected here: http://lalescu.ro/liviu/fet/forum/index.php?topic=1156.0