Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. the signal of QFtp class can not receive by the slot of in a Separate thread

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spite
    wrote on last edited by koahnig
    #1

    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]

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      3
      • SGaistS SGaist

        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.

        S Offline
        S Offline
        spite
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          That doesn't explain the double inheritance.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          0
          • SGaistS SGaist

            That doesn't explain the double inheritance.

            S Offline
            S Offline
            spite
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              S 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

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

                S Offline
                S Offline
                spite
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved