简体   繁体   中英

QFTP signals are not emitted?

Hi I've written a simple FTP code to upload images into a FTP server.

Now the problem is that commandStarted(int) and commandFinished(int,bool) signals never get emitted !!!

I've grabbed this code from qftp.pro which comes with Qt library and works successfully on my computer (It is a gui program) But when I put this ftp code it in my project which is a console project it fails to work !!!

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

I can connect to my ftp server using qt sample project and also Filezilla which is an FTP client software so IP and username and passwords are okay. (for security reasons I changed username and passwords + IP in here)

What's my problem here ?

Using QFtp in a console app . Are you sure your program doesn't work (since it works asynchronously)? Furthermore the QT api reference suggests you should use QNetworkAccessManager and QNetworkReply instead. See: http://qt-project.org/doc/qt-4.8/qftp.html#details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM