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. How to handle signals and slots inside cpp dll?
Forum Updated to NodeBB v4.3 + New Features

How to handle signals and slots inside cpp dll?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5
7 Posts 3 Posters 1.2k Views 2 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
    Selvajothi
    wrote on last edited by
    #1

    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*)));
    }

    1 Reply Last reply
    0
    • gde23G Offline
      gde23G Offline
      gde23
      wrote on last edited by
      #2

      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()
      
      1 Reply Last reply
      2
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        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.
        1 Reply Last reply
        3
        • S Offline
          S Offline
          Selvajothi
          wrote on last edited by
          #4

          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?

          Chris KawaC 1 Reply Last reply
          0
          • S Selvajothi

            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?

            Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            1 Reply Last reply
            2
            • S Offline
              S Offline
              Selvajothi
              wrote on last edited by Chris Kawa
              #6
                     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.

              Chris KawaC 1 Reply Last reply
              0
              • S Selvajothi
                       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.

                Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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
                
                1 Reply Last reply
                1

                • Login

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