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

How to handle signals and slots inside cpp dll?



  • I have a cpp dll in which i have functions. Now my project requires qt functions inside. I tried to add connect function of qt inside the cpp dll function but it is showing connect is an undefined identifier. Is there any way to use connect function inside cpp dll functions.
    Below is the code i have tried

    extern "C" __declspec(dllexport)QStringList * getinput()
    {
    QNetworkAccessManager* networkaccess = new QNetworkAccessManager();
    connect(networkaccess, SIGNAL(finished(QNetworkReply*)),this, SLOT(getcredential(QNetworkReply*)));
    }



  • I guess this has nothing to do if its a dll or not.
    When you are using connect from somewhere where QObject is not known, you need to use

    QObject::connect()
    

  • Moderators

    Couple of issues:

    • Move your implementation to cpp
    • Add Qt include paths to your dll project
    • Add Qt libs path to your dll project
    • Add QtCore library dependency to your dll project
    • Include <QObject>
    • connect is not a global function, so use QObject::connect like @gde23 suggested
    • You have a memory leak. Every time you call this function it will create new instance of QNetworkAccessManager and never release it.
    • QStringList is a class so you can't use it in "C" export. There are no classes in C.
    • You are not returning anything from that function
    • this has no meaning inside a free function.


  • if i use
    QObject::connect(networkaccess, SIGNAL(finished(QNetworkReply*)), SLOT(getcredential(QNetworkReply*)));

    i am getting error like a nonstatic member reference must be relative to a specific object

    where i have to create that slot function?


  • Moderators

    @Selvajothi Slot should be a member of a QObject derived class and you need to pass that class instance as the third parameter of connect.



  •        serverAuthentication::serverAuthentication(QObject* parent)
                :QObject(parent)
          {
          }
    
         extern "C" __declspec(dllexport)QStringList* AuthenticateServer()
         {
       
        QUrl url;
        url.setUrl(QUrl::fromLocalFile(path);
        url.setUserName("user");
        url.setPassword("password");
        QNetworkRequest request;
        request.setUrl(url);
        networkaccess = new QNetworkAccessManager();
        QNetworkProxy proxy;
        proxy.setApplicationProxy(QNetworkProxy::NoProxy);
        networkaccess->setProxy(proxy);
        networkaccess->get(request);
        QObject::connect(networkaccess, SIGNAL(finished(QNetworkReply*)),  SLOT(getcredential(QNetworkReply*)));#getting error here
        }
        
        void serverAuthentication::getcredential(QNetworkReply* reply)
        {
        QString er = reply->error();
        if (er == QNetworkReply::NoError)
        {
            QFile file(path);
            if (!file.open(QIODevice::ReadOnly)) {
                QMessageBox::information(0, "error", file.errorString());
            }
    
            QTextStream in(&file);
    
            while (!in.atEnd()) {
                QString line = in.readLine();
                QStringList fields = line.split(",");
                getdata->append(fields);
            }
    
            file.close();
            
            //qDebug() << "File data" << getdata;
            
        }
        else
            qDebug() << reply->error() << reply->errorString();
        
         }
    

    calling the slot of serverAuthentication class I think that's the mistake. But i don't know where to create SLOT for this.


  • Moderators

    @Selvajothi as I said - you need an instance of serverAuthentication passed to connect

    QObject::connect(networkaccess, SIGNAL(finished(QNetworkReply*)),  __here__, SLOT(getcredential(QNetworkReply*)));
                                                                         ^
                                                                        //you need a pointer to instance of serverAuthentication
    

    Where do you get it from? That's up to you. Also you're still leaking memory and have all the other issues I listed previously.

    Usually when working with C style libraries you have it organized kinda like this:

    void init();             // Creates instances of any "global" things you need, e.g. serverAuthentication and QNetworkAccessManager
    something doSomething(); // Does something with the global instances, like your AuthenticateServer function
    void shutdown();         // Destroys the global instances
    

Log in to reply