2014-06-29 134 views
0

嗨我寫了一個簡單的FTP代碼上傳圖像到FTP服務器。QFTP信號不發射?

現在的問題是,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%[email protected]@]"); 
    } 


} 
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)

這裏有什麼問題?

回答

0

Using QFtp in a console app。你確定你的程序不工作(因爲它是異步工​​作的)?此外,QT API參考建議您應該使用QNetworkAccessManager和QNetworkReply。請參閱:http://qt-project.org/doc/qt-4.8/qftp.html#details

+0

感謝您的回覆,我很確定它的失敗,因爲我已經盡一切可能調試了它。當我運行Qt示例項目時,它會成功。我試圖使用Qnetworkaccessmanager來實現它,但它也失敗:( – Dimitry