Results 1 to 2 of 2

Thread: QFtp with QThread issue

  1. #1
    Join Date
    Mar 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Cool QFtp with QThread issue

    Hi,

    I have writen a program which downloads files from particular server using QFtp.
    But when I tried to make it multithreaded, its not working.
    I am calling QFtp:Get method in QThread run() method but it fails to download.
    Here is my sample code:

    #include "ftp.h"
    #include <iostream>

    using namespace std;

    ftp::ftp(QObject *parent) :
    QThread(parent)
    {
    getDirectory();
    connect(&vftp,SIGNAL(listInfo(QUrlInfo)),this,SLOT (ftpListInfo(QUrlInfo)));
    connect(&vftp,SIGNAL(done(bool)),this,SLOT(ftpdone (bool)));
    connect(&vftp,SIGNAL(commandFinished(int,bool)),th is,SLOT(commandDone(int,bool)));
    }

    //Connection code
    void ftp::getDirectory(){
    vftp.connectToHost("192.168.2.10",21);
    vftp.login("user","pass");
    QString path = ftpDetails.value(3); //some path
    pendingDirs.append(path);
    vftp.cd(path);
    vftp.list();
    }

    void ftp::ftpListInfo(const QUrlInfo &urlInfo)
    {
    qDebug() << "Inside ftpListInfo";
    qDebug() << "File name" << urlInfo.name();
    if (urlInfo.isFile()) {

    if (urlInfo.isReadable() & urlInfo.name().startsWith(ftpDetails.value(11))) {
    fileNameList << urlInfo.name(); //Adding filenames into QStringList
    }
    }
    }

    void ftp::run()
    {
    QString name;
    if(!fileNameList.size()>0) {
    qDebug() << "fileNameList.size()=0";
    return;
    } else {
    name = fileNameList.takeFirst();
    }

    QFile *file = new QFile(currentLocalDir + "/" + name);
    filename = currentLocalDir + "/" + name;
    qDebug() << "filename: " << filename;
    if (!file->open(QIODevice::WriteOnly)) {
    cerr << "Warning: Cannot open file "
    << qPrintable(
    QDir::convertSeparators(file->fileName()))
    << endl;
    log.writeLog("Warning: Cannot open file " + file->fileName());
    return;
    }


    vftp.get(name, file);
    qDebug() << name;
    qDebug() << ftpDetails.value(4) + "/" + name;
    vftp.rename(name,ftpDetails.value(4) + "/" + name);
    openedFiles.append(file);

    dbRecord.clear();
    dbRecord << name << QString::number(file->size());
    //dbRecord << urlInfo.lastModified().toString("yyyyMMddhhmmss") << "D" << "OK";
    dbRecord << "20110606303300" << "D" << "OK";

    if (!db.addEntry(dbRecord))
    log.writeLog("Unable to insert record in FTP table");
    }

    void ftp::commandDone(int, bool error){

    if(vftp.currentCommand() == QFtp::List) {
    this->start();
    }
    }



    I am listing all the files in QStringList and then I am starting a thread to download files one by one. But only the vftp.get() method is giving problem and not downloading the files. Is it that the QFtp get() method and QThread run() are conflicting each other, something like that.
    Please help me on this.
    Thanks in advance.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: QFtp with QThread issue

    prasad, please use [ Code]...[/Code] tags to post code, its really a pain to read the code.

    Also try this quickly, (I have not read your complete code)

    Qt Code:
    1. void ftp::run()
    2. {
    3. QString name;
    4. if(!fileNameList.size()>0) {
    5. qDebug() << "fileNameList.size()=0";
    6. return;
    7. } else {
    8. name = fileNameList.takeFirst();
    9. }
    10. exec(); //Add this
    11. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 17th August 2010, 12:37
  2. QThread quit() issue.
    By rokkamraja in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2009, 16:34
  3. Replies: 0
    Last Post: 23rd September 2007, 11:54
  4. qftp in qthread canot upload file
    By cxl2253 in forum Qt Programming
    Replies: 16
    Last Post: 7th April 2007, 02:44
  5. Qthread Issue?
    By vishal.chauhan in forum Newbie
    Replies: 3
    Last Post: 29th March 2007, 08:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.