Problem with QFtp



  • Hello!
    I've some problem with the use of QFtp
    This is the code i've written
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtGui>
    #include <QtNetwork/QFtp>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),ftp(0)
    {
    ui->setupUi(this);
    // QFtp prova;
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_connetti_clicked()
    {
    ftp = new QFtp(this);
    ftp->connectToHost(ui->ftpserver->toPlainText());
    ftp->login(ui->username->toPlainText(),ui->password->toPlainText());
    ui->console->setText("Connesso al server "+ ui->ftpserver->toPlainText()+"\n");
    //connect(ftp,SIGNAL(stateChanged(int)),MainWindow,SLOT(connessione(int)));

    }

    void MainWindow::connessione(int a)
    {
    QString prova;
    switch(a){
    case 0: prova="Impossibile connettersi al server!\n";
    break;
    case 1: prova="Ricerca dell'host in corso\n";
    break;
    case 2: prova="Connessione in corso\n";
    break;
    case 3: prova="Connessione avvenuta! Benvenuto su "+ui->ftpserver->toPlainText()+"\n";
    break;
    case 4: prova="Ti sei loggato. Ciao "+ui->username->toPlainText()+"\n";
    break;
    case 5: prova="Chiusura connessione in corso. A Presto!\n";
    break;}
    ui->console->setText(ui->console->toPlainText()+prova);
    }
    @

    And this is the problem

    !http://i1025.photobucket.com/albums/y315/shi-kaz/Cattura-18.png(errorscreen)!

    What could it be the problem?
    I tried to fix it but i wasn't able...

    Thanks



  • You need to link against the network module. Add
    @
    QT += network
    @
    in your Qt project file



  • thanks!
    but i still have a problem with QFtp::Get
    I try to download file but when i open it...they are empty
    This is the code
    @void MainWindow::stampa(const QUrlInfo &file)
    {

    ui->console->setText(ui->console->toPlainText()+file.name()+"\n");
    
        //ftp->get(file.name());
        QString nome=file.name();
    
    
        if (QFile::exists(nome)){
            ui->console->setText(ui->console->toPlainText()+"ESISTE GIà UN FILE CHIAMATO "+file.name()+"\n");
            return;
        }
        nomefile = new QFile&#40;nome&#41;;
        if (!nomefile->open(QIODevice::ReadWrite))
             {
               ui->console->setText(ui->console->toPlainText()+"IMPOSSIBILE APRIRE IL FILE "+file.name()+"\n");
               //nomefile->close();
               return;
             }
    
    
        ftp->get(file.name(),nomefile,QFtp::Ascii);
        //ftp->get(file.name());
        //ui->console->setText(ui->console->toPlainText()+ftp->read()+"\n");
        ui->console->setText(ui->console->toPlainText()+nomefile->size()+"\n");
        nomefile->close();@


  • Don't close "nomefile" file because ...

    bq. The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously.

    So declare a slot and connect it to "QFtp::commandFinished signal":http://qt-project.org/doc/qt-4.8/qftp.html#commandFinished. In this slot close the file and delete the pointer.



  • i still have the problem
    this is the edited code
    @void MainWindow::chiudiFile(int a)
    {
    //if (a==filetrasf)
    nomefile->close();
    //delete nomefile;
    }

    void MainWindow::inizioTrasferimento(int a)
    {
    ui->console->setText(ui->console->toPlainText()+"Inizio trasferimento iniziato!\n");
    filetrasf=a;
    }

    void MainWindow::segnalaErrore(int a, bool err)
    {
    if (err)
    ui->console->setText(ui->console->toPlainText()+"C'è stato un errore! "+ftp->error()+"\n");
    else
    ui->console->setText(ui->console->toPlainText()+"Nessun errore!\n");
    }

    void MainWindow::stampa(const QUrlInfo &file)
    {

    ui->console->setText(ui->console->toPlainText()+file.name()+"\n");
    
        //ftp->get(file.name());
        QString nome=file.name();
    
    
        if (QFile::exists(nome)){
            ui->console->setText(ui->console->toPlainText()+"ESISTE GIà UN FILE CHIAMATO "+file.name()+"\n");
            return;
        }
        nomefile = new QFile&#40;nome&#41;;
        if (!nomefile->open(QIODevice::ReadWrite))
             {
               ui->console->setText(ui->console->toPlainText()+"IMPOSSIBILE APRIRE IL FILE "+file.name()+"\n");
               //nomefile->close();
               return;
             }
    
        ftp->get(file.name(),nomefile,QFtp::Ascii);
        connect(ftp,SIGNAL(commandStarted(int)),this,SLOT(inizioTrasferimento(int)));
    
        connect(ftp,SIGNAL(commandFinished(int,bool)),this,SLOT(chiudiFile&#40;int&#41;));
        connect(ftp,SIGNAL(commandFinished(int,bool)),this,SLOT(segnalaErrore(int, bool)));
        ui->console->setText(ui->console->toPlainText()+nomefile->size()+"\n");
    

    }
    @

    I added a slot in order to control the Start of the transfering and also che finish...
    It gave me no error but the files i downloaded are still empty



  • At first sight (I'm too tired now) you have some errors in code:

    • connect ftp signals to your slots before you call ftp get function ( or better just after initialize "ftp" member variable )
    • why do you need two slots connected to QFtp::commandFinished signal ?
    • you'll have memory leaks if don't delete "nomefile" pointer. ( i. e. delete it in one slot connected to QFtp::commandFinished signal )

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.