Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

the signal of QFtp class can not receive by the slot of in a Separate thread



  • Declear :

    class gFtpTask : public QThread,public QRunnable
    {
        Q_OBJECT
    public:
        explicit gFtpTask();
        void loadUploadFileLists(QString LocalPath,QStringList filelist);///file is the total path
        void loadUserInfo(QString ip,QString name,QString pwd,int port);
    
        void run();///在独立线程中处理的代码
    signals:
        void gFtpFinished(int id,QString err);///整个升级包上传成功或者失败的时候发送此信号
        void gFtpSingleFileFinished(int id,QString content);///单文件上传成功发送此信号
    public slots:
        void gFtpcmdRunStatus(bool isFail);
        qint64 gFtpProgressDected(qint64 done,qint64 total);
    private:
        QFtp *ftp;
        QString g_ServerIP;////远程服务器IP
        QString g_UserName;///ftp用户名
        QString g_UserPwd;///ftp密码
        int     g_port;///端口
    
        QString g_LocalPath;
        QStringList g_Filelist;///待上传的文件列表
    
        qint64  g_TotalSize;////要上传的所有文件的总大小
        qint64  g_UploadTotalSize;///所有已经上传的文件的大小
    
        qint64  g_CurrentFileUploadSize;///正在上传的文件,已经上传的文件大小
        int  g_FileListNum;
        int  g_CurrentFileNum;
    public:
        int id;
    
    };
    

    Run Function:

    void gFtpTask::run(){
        PRTLOG()<<"thread id:"<<int(QThread::currentThreadId());
        ftp =new QFtp(this);
        ftp->setTransferMode(QFtp::Passive);///设置传输模式为被动
        connect(this->ftp,SIGNAL(done(bool)),this,SLOT(gFtpcmdRunStatus(bool)),Qt::QueuedConnection);///判断某个ftp命令的执行结果
        connect(this->ftp,SIGNAL(dataTransferProgress(qint64,qint64)),this,SLOT(gFtpProgressDected(qint64,qint64)),Qt::QueuedConnection);///ftp传输文件的进度计算
    
        ////此槽函数要与外界相关联----------待确认
        PRTLOG()<<g_ServerIP<<g_port<<g_UserName<<g_UserPwd<<REMOTE_SERVER_PATH;
        PRTLOG()<<ftp->connectToHost(g_ServerIP,g_port);///与FTPserver建立链接 // 主机:192.168.***.*** 端口号:21
        PRTLOG()<<ftp->login(g_UserName, g_UserPwd);///登陆ftp
        PRTLOG()<<ftp->cd(REMOTE_SERVER_PATH);
    
        for(int i=0;i<g_Filelist.count();i++){
            QString filename = g_Filelist.at(i);
            QFile *upfile = new QFile(g_LocalPath+"/"+filename);
            if (!upfile->open(QIODevice::ReadOnly))
                 PRTLOG()<<"open fail";
            else
                PRTLOG()<<g_LocalPath+"/"+filename;
            ftp->put(upfile,filename);
        }
        emit ftp->dataTransferProgress(100,200);
        this->deleteLater();
        this->exec();
        this->quit();
        PRTLOG()<<g_ServerIP<<"finished";
    }
    

    The calling code:

     QStringList tx;tx.append("Meet.xml");
        tx.append("Meet1.xml");
        tx.append("Meet2.xml");
        tx.append("Meet3.xml");
        tx.append("123.tgz");
    
        gFtpTask *task = new gFtpTask;
        task->loadUploadFileLists(QString("."),tx);
        task->loadUserInfo(QString("192.168.157.41"),QString("root"),QString("11111"),21);
        task->run();
    
    

    the gFtpTask is a task thread of threadpool. In the calling code.I create a object of gFtpTask,and load the user ,password information,I also load the filelist that need to uploaded.
    the application uplaod the file to the remote ftp server.but the slots of connected to the QFtp 's done(bool) and dataTransferProgress(qint64,qint64),do not work. Because of the signale and slots work fail.I can not exit the eventloop.what should i do or what's wrong of my code

    [edit:koahnig code tags changed]


  • Lifetime Qt Champion

    Hi,

    Why are you inheriting from both QThread and QRunnable ?
    Why are you creating a QRunnable if you don't start it through QThreadPool ?

    @spite said in the signal of QFtp class can not receive by the slot of in a Separate thread:

    this->deleteLater();
    this->exec();
    this->quit();

    You ask for deletion before before you start the event loop.



  • @SGaist I will use the QThreadPool,the code is a part of my project


  • Lifetime Qt Champion

    That doesn't explain the double inheritance.



  • @SGaist Because the gFtpTask class is the QThreadPool's task class so it inherits the QRunnable.in addition,I want to use the signals and slot,the gFtpTask class inherits the QThread which the base class is QObejct.


  • Qt Champions 2019

    Still there is no need to derive from QThread then... QObject should be enough.



  • @Christian-Ehrlicher said in the signal of QFtp class can not receive by the slot of in a Separate thread:

    QObject
    NO,It can not use this->exec() into the eventloop by QObject rather than QThread .Without the eventloop,the qftp does not work.


Log in to reply