Simple FTP client
-
It may not be a problem for you but notice that QFtp will be deprecated with Qt5:
http://labs.qt.nokia.com/2011/05/12/qt-modules-maturity-level-the-list/ -
I knew that (still don't get why somebody would decide that) but thought it would be okay to reference to QFtp since QTShahal seems to be in need of a solution without much work :) - and as QNetworkAccessManager (still?) does not provide real FTP support I think QFtp to be the solution of choice.
-
I am trying this code from http://doc.trolltech.com/4.3/network-ftp.html
@// main.cpp
#include <QCoreApplication>
#include <QDebug>
#include <QFtp>
class Ftp : public QFtp
{
Q_OBJECT
public:
Ftp(QObject* parent = 0)
{
connect(this, SIGNAL(listInfo(QUrlInfo)), this, SLOT(doListInfo(QUrlInfo)));
connect(this, SIGNAL(done(bool)), QCoreApplication::instance(), SLOT(quit()));
}
protected slots:
void doListInfo(const QUrlInfo& info)
{
qDebug() << info.name();
}
};
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
Ftp ftp;
ftp.connectToHost("ftp.trolltech.com");
ftp.login();
ftp.list();
ftp.close();
return app.exec();
}
#include "main.moc"@I am testing this so that I can modify it for my use, but I am getting the following error:
:-1: error: No rule to make target
debug/main.moc', needed by
debug/Ftp.o'. Stop.Any idea how to resolve this? Thanks in advance
-
sorry .. I got the above code from here:
-
How and what of Qt did you install?
Do you use an IDE (e.g. Qt creator)? -
I am using Qt creator
-
I am not an expert on Qt creator, but I assume that the reason of your problem lies in the fact that you just added a new class definition to your main.cpp.
You need to introduce an additional header file containing the class definition. The macro Q_OBJECT shall add an additional build step for "moc".
-
I dont think that's it or I am not doing the header correctly. Trying the following now:
Header file:
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
class ButtonClicked;
class Ftp;
class QFtp;
}
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void ButtonClicked();
void Ftp();
private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
@FTP.cpp:
@// Ftp.cpp
#include <QCoreApplication>
#include <QDebug>
#include <QFtp>int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
QStringList args = QCoreApplication::arguments();Ftp putter; if (!putter.putFile (QUrl(args[1]))) return 1; QObject::connect(&putter, SIGNAL(done()), &app, SLOT(quit())); return app.exec();
}
class Ftp : public QObject
{
Q_OBJECTpublic:
Ftp(QObject* parent = 0);
bool putFile (const QUrl &url);signals:
void done();
private slots;
void ftpDone(bool error);private:
QFtp ftp;
QFile file;
};FtpPut::FtpPut(QObject *parent)
: QObject(parent)
{
connect (&ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool)));
}ftp.connectToHost(url.host(), url.port(21));
ftp.login();
ftp.put(url.path(), &file);
ftp.close();
return true;
}
@I modified this from a book that I bought. Originally the code was for getting a file. I still see this error:
:-1: error: No rule to make target
debug/main.moc', needed by
debug/Ftp.o'. Stop. -
Well your source code changed considerably from previous post. You NEED to straighten your code. Otherwise the tools are being confused as well.
You need to separate to header (declarations) and source (implementations) files.
For your last code it would look similar to the following code.
NOTE: Just roughly separated to give an idea what is meant. Not compiled not tested. And I am sure it will not compile, because some headers need to be included.
MainWindow.h:
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
class ButtonClicked;
class Ftp;
class QFtp;
}
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void ButtonClicked();
void Ftp();
private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
@FTP.h:
@
#ifndef FTP_H_INCLUDED
#define FTP_H_INCLUDED#includes <<<<<<<<<are missing
class Ftp : public QObject
{
Q_OBJECTpublic:
Ftp(QObject* parent = 0);
bool putFile (const QUrl &url);signals:
void done();
private slots;
void ftpDone(bool error);private:
QFtp ftp;
QFile file;
};
#endif
@FTP.cpp:
@// Ftp.cpp
#include <QCoreApplication>
#include <QDebug>
#include <QFtp>#include "FTP.h"
FtpPut::FtpPut(QObject *parent)
: QObject(parent)
{
connect (&ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool)));
}ftp.connectToHost(url.host(), url.port(21));
ftp.login();
ftp.put(url.path(), &file);
ftp.close();
return true;
}
@main.cpp:
@
#includes <<<<<<<<<<<<<<<<<<<<are missing hereint main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
QStringList args = QCoreApplication::arguments();Ftp putter; if (!putter.putFile (QUrl(args[1]))) return 1; QObject::connect(&putter, SIGNAL(done()), &app, SLOT(quit())); return app.exec();
}
@ -
Thanks, I will try that and see what happens.