QFTP信号不发射?

时间:2014-06-29 08:50:10

标签: c++ qt qftp

您好我已经编写了一个简单的FTP代码将图像上传到FTP服务器。

现在的问题是commandStarted(int)commandFinished(int,bool)信号永远不会被发出!!!

我从qftp.pro中获取了这个代码,它附带了Qt库,并在我的计算机上成功运行(这是一个gui程序)但是当我把这个ftp代码放在我的项目中,这是一个控制台项目它无法工作!!!

My.pro

QT       += core network
QT       -= gui
TARGET = FTP_PROJECT
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

CONFIG += link_pkgconfig
PKGCONFIG += opencv


SOURCES += main.cpp \
    FTP_Class.cpp

HEADERS += \
    FTP_Class.h \
    Definitions.h

Main.cpp的

#include <QCoreApplication>
#include "FTP_Class.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    FTP_Class u;
    return a.exec();
}

FTP_Class.cpp

#include "FTP_Class.h"

FTP_Class::FTP_Class(QObject *parent) :
    QObject(parent)
{


    ftp = new QFtp(this);
    connect(ftp,SIGNAL(commandStarted(int)),this,SLOT(ftpStartedCommand(int)));
    connect(ftp, SIGNAL(commandFinished(int,bool)),this, SLOT(ftpCommandFinished(int,bool)));


    QUrl url("12.128.32.54");
    //if (!url.isValid() || url.scheme().toLower() != QLatin1String("ftp"))
    {
        ftp->connectToHost("12.128.32.54", 21);
        ftp->login("Hidden","[STr_9&65%Est@@]");
    }


}
void FTP_Class::ftpStartedCommand(int id)
{

    qDebug()<<"Command with ID "<<QString::number(id)<<" is started";
}

void FTP_Class::ftpCommandFinished(int, bool error)
{
    if (ftp->currentCommand() == QFtp::ConnectToHost) {
        if (error) {
            qDebug()<< " Unable to connect to the FTP server Please check that the host name is correct";
            ftp->abort();
            ftp->deleteLater();
            return;
        }
    }
    if(ftp->currentCommand() == QFtp::Cd)
    {
        static bool enable = false;
        if(enable)
        {

        QByteArray ImAgE = ReadImage(); //reads from external library

        QString Final_Time = "Image.jpg";
        ftp->put(ImAgE,Final_Time);
        }
        enable=true;
    }
    if(ftp->currentCommand() == QFtp::Mkdir)
    {
        qDebug()<<"Going to dest folder";
        ftp->cd("Test");

    }
    if(ftp->currentCommand() == QFtp::Login)
    {
        ftp->cd("Image_Folder");
        qDebug()<<"creating dest folder";
        ftp->mkdir("Test");
    }
    if(ftp->currentCommand()== QFtp::Put)
    {
        if (error) {
            qDebug()<<"Error in Uploading ...";
        } else {
            qDebug("Upload is completed ...");
        }
    }
  return;
}

FTP_Class.h

#ifndef UPLOADER_H
#define UPLOADER_H

#include "Definitions.h"
class FTP_Class: public QObject
{
Q_OBJECT
public:
explicit FTP_Class(QObject *parent = 0);
QFtp *ftp;
public slots:

void ftpCommandFinished(int,bool);
void ftpStartedCommand(int id);
private:
};

#endif // UPLOADER_H

我可以使用qt示例项目连接到我的ftp服务器,也可以连接到FTP客户端软件Filezilla,因此IP和用户名和密码都可以。 (出于安全考虑,我在这里更改了用户名和密码+ IP)

我的问题是什么?

1 个答案:

答案 0 :(得分:0)

Using QFtp in a console app。你确定你的程序不起作用(因为它是异步工​​作的)吗?此外,QT api参考建议您应该使用QNetworkAccessManager和QNetworkReply。请参阅:http://qt-project.org/doc/qt-4.8/qftp.html#details

相关问题