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. As dynamic libraries to use class variables?
Forum Updated to NodeBB v4.3 + New Features

As dynamic libraries to use class variables?

Scheduled Pinned Locked Moved Solved General and Desktop
50 Posts 4 Posters 13.0k Views 3 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.
  • M Offline
    M Offline
    Mikee
    wrote on last edited by
    #33

    But what to do with NBar?

    void StrategyCod()
    {
        MainWindow *w;
        qDebug()<<"Library work2"<<w->NBar;
      
    }
    
    jsulmJ 1 Reply Last reply
    0
    • M Mikee

      But what to do with NBar?

      void StrategyCod()
      {
          MainWindow *w;
          qDebug()<<"Library work2"<<w->NBar;
        
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #34

      @Mikee

      typedef void (*MyPrototype)(MainWindow *w);
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mikee
        wrote on last edited by Mikee
        #35

        I have done this and it's working

         QLibrary MyLib("C:\\Qt\\project\\build-StrategyCod-Desktop_Qt_5_9_2_MinGW_32bit-Debug\\debug\\StrategyCod");  
            MyLib.load();
            typedef void (*MyPrototype)(MainWindow *);
            MyPrototype StrategyCod = (MyPrototype) MyLib.resolve("StrategyCod");
            StrategyCod(&w);  
            qDebug()<<"q1"<< q1;
            MyLib.unload();
        

        Thank you

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #36

          Congrats :)

          1 Reply Last reply
          1
          • M Offline
            M Offline
            Mikee
            wrote on last edited by
            #37

            I have are new problem.

            qDebug()<<NBar;
            

            return NBar=1064,

            qDebug()<<"Library work2"<< Parametr<<w->NBar;
            

            return NBar=0
            In class MainWindow:

            void MainWindow::on_ButtonRuneOne_clicked()//запуск стратегии на одном ядре
            {
                bool ok;
                double d = QInputDialog::getDouble(this, tr("ParametrForStrategy"),
                                                       tr("double:"), 37.56, -100000, 100000, 6, &ok);
                if (ok) {ParametrForStrategyCod=d; }
            
                QLibrary MyLib("C:\\Qt\\project\\build-StrategyCod-Desktop_Qt_5_9_2_MinGW_32bit-Debug\\debug\\StrategyCod");  
                MyLib.load();
                typedef void (*MyPrototype)(double,MainWindow *);
                MyPrototype StrategyCod = (MyPrototype) MyLib.resolve("StrategyCod");
                MainWindow w;
                qDebug()<<NBar;
                StrategyCod(ParametrForStrategyCod,&w);  /
                MyLib.unload();
            }
            

            In library:

            void StrategyCod(double Parametr, MainWindow *w)
            {
                qDebug()<<"Library work2"<< Parametr<<w->NBar;
            }
            

            But in main.cpp it works good. How can I fix problem with link of object class?

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

              You're passing w to your method which is not the current MainWindow object you are calling StrategyCod from.

              Pass this to the method.

              On a side note, you are doing some pretty convoluted stuff here for what seems to be a pretty simple task. I'd encourage you to re-think a bit the design of your application.

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

              1 Reply Last reply
              3
              • M Offline
                M Offline
                Mikee
                wrote on last edited by
                #39

                Thank you. I wrote it like this

                StrategyCod(ParametrForStrategyCod,this); 
                

                and it works

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mikee
                  wrote on last edited by
                  #40

                  How can I create lamda function in the slot of the class?

                  
                         QVector <double> Perebor1;
                         for (double i=d1;i<=d2;i=i+d3) 
                         {
                             Perebor1.append(i);
                         }
                         QLibrary MyLib("C:\\Qt\\project\\build-StrategyCod-Desktop_Qt_5_9_2_MinGW_32bit-Debug\\debug\\StrategyCod");  
                         MyLib.load();
                         typedef void (*MyPrototype)(double,MainWindow *);
                         MyPrototype StrategyCod = (MyPrototype) MyLib.resolve("StrategyCod");
                         //StrategyCod(ParametrForStrategyCod,this);  
                         QFuture<void> Perebor2 = QtConcurrent::map(Perebor1,[&](const double& d){ StrategyCod(d,this);}); 
                         MyLib.unload();
                  
                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mikee
                    wrote on last edited by Mikee
                    #41

                    And if I do, the function does not work.

                    void StrategyCod(double Parametr, MainWindow *w)
                    {
                        qDebug()<<"Library work2"<< Parametr<<w->NBar;
                        w->NBar=2000;
                    }
                    

                    error:
                    Invalid parameter passed to C runtime function.
                    Invalid parameter passed to C runtime function.

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #42

                      Well if Perebor2 still runs when you do MyLib.unload()
                      its not so nice for it...

                      1 Reply Last reply
                      1
                      • M Offline
                        M Offline
                        Mikee
                        wrote on last edited by Mikee
                        #43

                        How to wait for all Perebor2?
                        How to change w->NBar throw StrategyCod? If I do it in one thred

                        void StrategyCod(double Parametr, MainWindow *w)
                        {
                            qDebug()<<"Library work2"<< Parametr<<w->NBar;
                            w->NBar=2000;
                        }
                        

                        it compiles, but when it works errors appears:
                        ASSERT failure in QVector<T>::operator[]: "index out of range", file ....\Qt5.9.2\5.9.2\mingw53_32\include/QtCore/qvector.h, line 430
                        Invalid parameter passed to C runtime function.
                        Invalid parameter passed to C runtime function.

                        mrjjM 1 Reply Last reply
                        0
                        • M Mikee

                          How to wait for all Perebor2?
                          How to change w->NBar throw StrategyCod? If I do it in one thred

                          void StrategyCod(double Parametr, MainWindow *w)
                          {
                              qDebug()<<"Library work2"<< Parametr<<w->NBar;
                              w->NBar=2000;
                          }
                          

                          it compiles, but when it works errors appears:
                          ASSERT failure in QVector<T>::operator[]: "index out of range", file ....\Qt5.9.2\5.9.2\mingw53_32\include/QtCore/qvector.h, line 430
                          Invalid parameter passed to C runtime function.
                          Invalid parameter passed to C runtime function.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #44

                          @Mikee
                          Dont wait for it.
                          Just move the MyLib.unload(); to a better place.

                          also if you have

                          void mainwindow::somefunction {
                          QVector <double> Perebor1;
                          for (double i=d1;i<=d2;i=i+d3)
                          {
                          Perebor1.append(i);
                          }
                          QLibrary MyLib("C:\Qt\project\build-StrategyCod-Desktop_Qt_5_9_2_MinGW_32bit-Debug\debug\StrategyCod");
                          MyLib.load();
                          typedef void (*MyPrototype)(double,MainWindow *);
                          MyPrototype StrategyCod = (MyPrototype) MyLib.resolve("StrategyCod");
                          //StrategyCod(ParametrForStrategyCod,this);
                          QFuture<void> Perebor2 = QtConcurrent::map(Perebor1,[&](const double& d){ StrategyCod(d,this);});
                          MyLib.unload();
                          } // Perebor1 is deleted here.

                          Perebor1 also dies as its a local
                          so make them member variables of the class
                          and structure code better.

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Mikee
                            wrote on last edited by
                            #45

                            `Perebor in class ,MainWindow. It doesn't work. Errors are the same.
                            ``
                            for (double i=d1;i<=d2;i=i+d3)
                            {
                            Perebor.append(i);
                            }

                               QLibrary MyLib("C:\\Qt\\project\\build-StrategyCod-Desktop_Qt_5_9_2_MinGW_32bit-Debug\\debug\\StrategyCod");  
                               MyLib.load();
                               typedef void (*MyPrototype)(double,MainWindow *);
                               MyPrototype StrategyCod = (MyPrototype) MyLib.resolve("StrategyCod");
                               //StrategyCod(ParametrForStrategyCod,this);  
                               //QFuture<void> Perebor2 = QtConcurrent::map(Perebor1,[&](const double& d){ StrategyCod(d,this);});  
                               QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[&](const double& d){ StrategyCod(d,this);});
                            
                            1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #46

                              What about Perebor2 ?
                              Wont it suffer the same and run out of scope ?

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                Mikee
                                wrote on last edited by
                                #47

                                No. It's okey with Perebor2. Function StrategyCod2 is in class MainWindow works correctly.

                                for (double i=d1;i<=d2;i=i+d3) 
                                       {
                                           Perebor.append(i);
                                       }
                                
                                       QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[this](const double& d){ StrategyCod2(d);});
                                
                                mrjjM 1 Reply Last reply
                                0
                                • M Mikee

                                  No. It's okey with Perebor2. Function StrategyCod2 is in class MainWindow works correctly.

                                  for (double i=d1;i<=d2;i=i+d3) 
                                         {
                                             Perebor.append(i);
                                         }
                                  
                                         QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[this](const double& d){ StrategyCod2(d);});
                                  
                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #48

                                  @Mikee
                                  Ok for
                                  QLibrary MyLib;
                                  docs says
                                  QLibrary::~QLibrary()

                                  Destroys the QLibrary object.
                                  Unless unload() was called explicitly, the library stays in memory until the application terminates.

                                  so that seems ok.

                                  Im not sure what line gives the
                                  "Invalid parameter passed to C runtime function."

                                  You should use the debugger to find out.

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

                                    Out of curiosity, why are you dynamically loading that library since it's so tied to your application and that you are in control of both ?

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

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      Mikee
                                      wrote on last edited by
                                      #50

                                      Because I need to change the function code.
                                      There is problem in this string:

                                      QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[&](const double& d){ StrategyCod(d,this);});
                                      

                                      because it's working:

                                      QLibrary MyLib("C:\\Qt\\project\\build-StrategyCod-Desktop_Qt_5_9_2_MinGW_32bit-Debug\\debug\\StrategyCod");  
                                          MyLib.load();
                                          typedef void (*MyPrototype)(double,MainWindow *);
                                          MyPrototype StrategyCod = (MyPrototype) MyLib.resolve("StrategyCod");
                                          StrategyCod(ParametrForStrategyCod,this);  
                                          MyLib.unload();
                                      

                                      and it's working:

                                      for (double i=d1;i<=d2;i=i+d3) 
                                             {
                                                 Perebor.append(i);
                                             }
                                      QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[this](const double& d){ StrategyCod2(d);});
                                      

                                      Maybe it needs to insert something there:

                                      QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[&](const double& d)(/*insert something there*/){ StrategyCod(d,this);});
                                      
                                      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