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

Multithreading



  • Hello,

    I did simple static library :

    class MY_SFTP : public QObject
    {
        Q_OBJECT
    public:
      //  ~~CN16K_SFTP(QObject *parent = nullptr);~~
      MY_SFTP(QObject *parent = nullptr);
    
        Q_PROPERTY(bool connected READ connected WRITE setConnected NOTIFY connectedChanged)
        bool connected()const{
            return m_connected;
        }
        void setConnected(const bool &co){
            if(m_connected!=co){
                m_connected = co;
                emit connectedChanged(m_connected);
            }
        }
    public slots:
        void connectToServer(QString url){ /*this will take 5 seconds*/ }
    signals:
        void connectedChanged(bool);
      private :
        bool m_connected;
     };
    

    I import and use it in another project. I use it i a new QThread to not freeze my GUI, like this :

    SftpUser::SftpUser(QObject *parent) : QObject(parent)
    {
        wt = new QThread();
        m_sftp = new MY_SFTP();
        m_sftp->moveToThread(wt);
    }
    

    Is this a good approch or the QThread should be created in the lib itself ?


  • Moderators

    @LeLev said in Multithreading:

    Is this a good approch or the QThread should be created in the lib itself ?

    It's good. This way you leave users of your library a choice - they can use your class in thread or not.

    By the way: your class name does not match the name of the constructor (which means that it is not a constructor). Probably a copy-paste error, but I point it out just in case.



  • Thank you @sierdzio.
    class name edited :)


Log in to reply