Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. How to bind qt to python? (boost.python).
QtWS25 Last Chance

How to bind qt to python? (boost.python).

Scheduled Pinned Locked Moved Solved Language Bindings
3 Posts 2 Posters 476 Views
  • 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.
  • M Offline
    M Offline
    mhanusek
    wrote on last edited by mhanusek
    #1

    Hello!
    I bind my library (it uses QThread and QSslSocket) to Python.
    I use qt/5.15.3 and boost/1.77.0.

    My wrapper:

    class AuthThreadWrapper : public AuthThread  // AuthThread inherits by QThread
    {
        Q_OBJECT
    
    public:
        AuthThreadWrapper(QObject *parent, const std::string ip, Ed25519Certificate& cert, Credentials &credentials) : AuthThread(parent, QString::fromStdString(ip), cert, credentials)
        {
            QObject::connect(this, &AuthThread::error, this, &AuthThreadWrapper::onError);
            QObject::connect(this, &AuthThread::connected, this, &AuthThreadWrapper::onConnected);
            QObject::connect(this, &AuthThread::disconnected, this, &AuthThreadWrapper::onDisconnected);
            QObject::connect(this, &AuthThread::loggedOut, this, &AuthThreadWrapper::onLogout);
            QObject::connect(this, &AuthThread::stopped, this, &AuthThreadWrapper::onStopped);
    
            QObject::connect(this, &AuthThread::listUsersInfoUpdated, this, &AuthThreadWrapper::onListUsersInfoUpdated);
        }
    
        ~AuthThreadWrapper() {}
        
        int exec()
        {
            return QThread::exec();
        }
        
        void onConnectedHandler(boost::python::object callback)
        {
            _onConnectedCallback = callback;
        }
    
        void onDisconnectedHandler(boost::python::object callback)
        {
            _onDisconnectedCallback = callback;
        }
    
        void onLogoutHandler(boost::python::object callback)
        {
            _onLogoutCallback = callback;
        }
    
        void onStoppedHandler(boost::python::object callback)
        {
            _onStoppedCallback = callback;
        }
    
        void onErrorHandler(boost::python::object callback)
        {
            _onErrorCallback = callback;
        }
    
        void onListUsersInfoUpdatedHandler(boost::python::object  callback)
        {
            _onListUsersInfoUpdatedCallback = callback;
        }
    
    private:
        boost::python::object _onConnectedCallback;
        boost::python::object _onDisconnectedCallback;
        boost::python::object _onLogoutCallback;
        boost::python::object _onStoppedCallback;
        boost::python::object _onErrorCallback;
        boost::python::object _onListUsersInfoUpdatedCallback;
    
    public Q_SLOTS:
        void onStopped()
        {
            if(_onStoppedCallback)
            {
                _onStoppedCallback();
            }
        }
    
        void onConnected(bool value)
        {
            if(_onConnectedCallback)
            {
                _onConnectedCallback(value);
            }
        }
    
        void onDisconnected()
        {
            if(_onDisconnectedCallback)
            {
                _onDisconnectedCallback();
            }
        }
    
        void onLogout()
        {
            if(_onLogoutCallback)
            {
                _onLogoutCallback();
            }
        }
    
        void onError(const QString msg, const Auth::ErrorType errType)
        { 
            if (_onErrorCallback) { _onErrorCallback(msg, errType); }
        }
    
        void onListUsersInfoUpdated(const QString msg)
        { 
            if (_onListUsersInfoUpdatedCallback) { _onListUsersInfoUpdatedCallback(msg); }
        }
    };
    
    import auth_client_python_api 
    from PySide2 import *
    
    ///...
    
    if __name__ == '__main__':
      t = auth_client_python_api.AuthThreadWrapper(None, ip, cert, creds)
      t.on_stopped(stop_handler)
      t.on_connected(connect_handler)
      t.on_error(err_handler)
    
      app = QApplication(sys.argv)
      t.connect();
      t.start();
       sys.exit(app.exec_())
    

    It does not work with PySide2. QSslSocket does not emit the signal QAbstractSocket::readyRead.
    If when I do not use QApplication, the program writes "QEventLoop: Cannot be used without QApplication".
    How to do without QApplication?
    Please help me.

    JonBJ 1 Reply Last reply
    0
    • M mhanusek

      Hello!
      I bind my library (it uses QThread and QSslSocket) to Python.
      I use qt/5.15.3 and boost/1.77.0.

      My wrapper:

      class AuthThreadWrapper : public AuthThread  // AuthThread inherits by QThread
      {
          Q_OBJECT
      
      public:
          AuthThreadWrapper(QObject *parent, const std::string ip, Ed25519Certificate& cert, Credentials &credentials) : AuthThread(parent, QString::fromStdString(ip), cert, credentials)
          {
              QObject::connect(this, &AuthThread::error, this, &AuthThreadWrapper::onError);
              QObject::connect(this, &AuthThread::connected, this, &AuthThreadWrapper::onConnected);
              QObject::connect(this, &AuthThread::disconnected, this, &AuthThreadWrapper::onDisconnected);
              QObject::connect(this, &AuthThread::loggedOut, this, &AuthThreadWrapper::onLogout);
              QObject::connect(this, &AuthThread::stopped, this, &AuthThreadWrapper::onStopped);
      
              QObject::connect(this, &AuthThread::listUsersInfoUpdated, this, &AuthThreadWrapper::onListUsersInfoUpdated);
          }
      
          ~AuthThreadWrapper() {}
          
          int exec()
          {
              return QThread::exec();
          }
          
          void onConnectedHandler(boost::python::object callback)
          {
              _onConnectedCallback = callback;
          }
      
          void onDisconnectedHandler(boost::python::object callback)
          {
              _onDisconnectedCallback = callback;
          }
      
          void onLogoutHandler(boost::python::object callback)
          {
              _onLogoutCallback = callback;
          }
      
          void onStoppedHandler(boost::python::object callback)
          {
              _onStoppedCallback = callback;
          }
      
          void onErrorHandler(boost::python::object callback)
          {
              _onErrorCallback = callback;
          }
      
          void onListUsersInfoUpdatedHandler(boost::python::object  callback)
          {
              _onListUsersInfoUpdatedCallback = callback;
          }
      
      private:
          boost::python::object _onConnectedCallback;
          boost::python::object _onDisconnectedCallback;
          boost::python::object _onLogoutCallback;
          boost::python::object _onStoppedCallback;
          boost::python::object _onErrorCallback;
          boost::python::object _onListUsersInfoUpdatedCallback;
      
      public Q_SLOTS:
          void onStopped()
          {
              if(_onStoppedCallback)
              {
                  _onStoppedCallback();
              }
          }
      
          void onConnected(bool value)
          {
              if(_onConnectedCallback)
              {
                  _onConnectedCallback(value);
              }
          }
      
          void onDisconnected()
          {
              if(_onDisconnectedCallback)
              {
                  _onDisconnectedCallback();
              }
          }
      
          void onLogout()
          {
              if(_onLogoutCallback)
              {
                  _onLogoutCallback();
              }
          }
      
          void onError(const QString msg, const Auth::ErrorType errType)
          { 
              if (_onErrorCallback) { _onErrorCallback(msg, errType); }
          }
      
          void onListUsersInfoUpdated(const QString msg)
          { 
              if (_onListUsersInfoUpdatedCallback) { _onListUsersInfoUpdatedCallback(msg); }
          }
      };
      
      import auth_client_python_api 
      from PySide2 import *
      
      ///...
      
      if __name__ == '__main__':
        t = auth_client_python_api.AuthThreadWrapper(None, ip, cert, creds)
        t.on_stopped(stop_handler)
        t.on_connected(connect_handler)
        t.on_error(err_handler)
      
        app = QApplication(sys.argv)
        t.connect();
        t.start();
         sys.exit(app.exec_())
      

      It does not work with PySide2. QSslSocket does not emit the signal QAbstractSocket::readyRead.
      If when I do not use QApplication, the program writes "QEventLoop: Cannot be used without QApplication".
      How to do without QApplication?
      Please help me.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @mhanusek said in How to bind qt to python? (boost.python).:

      If when I do not use QApplication, the program writes "QEventLoop: Cannot be used without QApplication".
      How to do without QApplication?

      I don't follow: you cannot have a QEventLoop without a QApplication (or one of its base classes), hence the error message. So answer to "How to do without QApplication?" is you cannot.

      M 1 Reply Last reply
      0
      • JonBJ JonB

        @mhanusek said in How to bind qt to python? (boost.python).:

        If when I do not use QApplication, the program writes "QEventLoop: Cannot be used without QApplication".
        How to do without QApplication?

        I don't follow: you cannot have a QEventLoop without a QApplication (or one of its base classes), hence the error message. So answer to "How to do without QApplication?" is you cannot.

        M Offline
        M Offline
        mhanusek
        wrote on last edited by
        #3

        @JonB thanks for your reply.
        I found a similar topic on stackoverflow.
        That helped to solve my problem.

        1 Reply Last reply
        0
        • M mhanusek has marked this topic as solved on

        • Login

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