Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Trying to use QT from DLL in simple console project but it doesn't work

    General and Desktop
    4
    14
    97
    Loading More Posts
    • 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.
    • J
      Jastot last edited by

      Well. I have find topic with a problem like my,but not the same...

      Trying to call the dynamic QWidget loader from simple console project - error.
      Which error? Heap is dead :D
      I have a look in disassembler and see that the pointer to QApplication is zero. (Think it blocked in anther tread...but dont enough experience to fix it)..

      Try to call my code from console project (copy+paste). all is ok. Back to dll - error.

      Can we use QT from DLL without create another treads or something like that???

      Code for example: (nevermind what is going on in classes which are in dll)

      QApplication* qapp = new QApplication(argc, argv);
      QtWidgetsClassTest* test = new QtWidgetsClassTest(); - QDialog
      FormLoader* formLoader = new FormLoader(qapp); - new uiLoader
      formLoader->uiLoader = new QUiLoader();
      formLoader->uiLoader->addPluginPath(qapp->applicationDirPath());
      

      So.

      addPluginPath(qapp->applicationDirPath());
      

      shows error...that heap is broken...

      1 Reply Last reply Reply Quote 0
      • hskoglund
        hskoglund last edited by

        Hi, I've had success with starting Qt from a .dll belonging a console application. but I used QCoreApplication and also created it the usual way on the stack (i..e no new keyword).

        J 1 Reply Last reply Reply Quote 0
        • J
          Jastot @hskoglund last edited by

          @hskoglund , Hi. Thank you for help.
          Try to do it like you write. Now compiler says true - that it can't use QApplication or QCoreApplication.

          Will try to read about treads and use something like that....cuz QApplication is blocked...
          But it is really interesting. i am create an QApplication on dll...and after that it writes that QAapp is blocked... But when i create all logic (not only QApp) in console project, which is calling dll - all is work.
          Best regards,
          Jastot.

          For example put some code here. If you have some ideas - i will be happy.

          part 1: app init in console and call func in dll

          #include <QtWidgets>
          #include <QApplication>
          #include <QtUIEditorDLL.h>
          int main(int argc, char* argv[])
          {
          	//QApplication
          	QApplication qapp(argc, argv);
          	QtUIEditorDLL* test = new QtUIEditorDLL(); - error Access violation reading location 
                  return 0;
          }
          //////////
          QtUIEditorDLL::QtUIEditorDLL()
          {
          	FormLoader* formLoader = new FormLoader();
          	formLoader->uiLoader = new QUiLoader();
          	formLoader->uiLoader->addPluginPath(QApplication::applicationDirPath()); - error Access violation reading location 
          }
          

          part 2: call in dll

          int main(int argc, char* argv[])
          {
          	QtUIEditorDLL* test = new QtUIEditorDLL(); - error Access violation reading location 
                  return 0;
          }
          //////////
          QtUIEditorDLL::QtUIEditorDLL()
          {
          int argc = 1;
          	char* argv[2] = { strdup("dummy"), {} };
          	QApplication qapp(argc, argv);
          	FormLoader* formLoader = new FormLoader();
          	formLoader->uiLoader = new QUiLoader();
          	formLoader->uiLoader->addPluginPath(QApplication::applicationDirPath()); - error Access violation reading location 
          }
          

          part 3: all in console project

          int main(int argc, char* argv[])
          {
          	QApplication qapp(argc, argv);
          	FormLoader* formLoader = new FormLoader();
          	formLoader->uiLoader = new QUiLoader();
          	formLoader->uiLoader->addPluginPath(QApplication::applicationDirPath()); - all is ok. it works.
                  return 0;
          }
          
          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @Jastot last edited by

            @Jastot What is FormLoader? Please post the whole error message.
            Also, did you do debugging and check the stack trace after the crash?

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

            J 1 Reply Last reply Reply Quote 0
            • J
              Jastot @jsulm last edited by Jastot

              @jsulm, hello.
              FormLoader - class with pointer on QUiLoader and some another functions...nothing more and nothing interesting.

              Full error looks like:
              Exception thrown at 0x00007FFCD0C91AFC (ntdll.dll) in .exe: 0xC0000005: Access violation reading location 0x0000000000000024.

              I read disassembled code and see that it crashes when trying to call QApplication...

              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @Jastot last edited by

                @Jastot I think your problem is that you're allocating qapp as a local variable inside QtUIEditorDLL. That means it only exists inside that method, as soon as it terminates qapp is deleted. If you want to use Qt signals/slots you need to start Qt event loop calling qapp.exec().

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

                J 1 Reply Last reply Reply Quote 2
                • J
                  Jastot @jsulm last edited by

                  @jsulm
                  Well. Sadly it didn't help.
                  So i tried to some experiments...
                  I found that QApplication works in dll without exec().

                  now QtUIEditorDLL class is QApplication.

                  class QTUIEDITORDLL_EXPORT QtUIEditorDLL: QApplication
                  

                  In console app i have made something like that: (believe that pointer can help...)

                  std::shared_ptr<QtUIEditorDLL> ptrQtUIEditorDLL(new QtUIEditorDLL(argc, argv));
                  	QtUIEditorDLL* test = ptrQtUIEditorDLL.get();
                  	test->Init();
                  

                  and in dll made something like that:

                  QtUIEditorDLL::QtUIEditorDLL(int& argc, char** argv)
                  	:QApplication(argc,argv) {}
                  
                  void QtUIEditorDLL::Init()
                  {
                  	QtWidgetsClassTest* test = new QtWidgetsClassTest(); // QDialog initialization worked......So QApplication works
                  	QUiLoader quiLoader;
                  	QString qstr = QCoreApplication::applicationDirPath(); // All is ok. QApplication works. in qstr i can see true way to application.
                  	quiLoader.addPluginPath(qstr); // same error... Exception thrown at 0x00007FFCD0C91AFC (ntdll.dll) in .exe: 0xC0000005: Access violation reading location 0x0000000000000024.
                  

                  So...its problem of QUiLoader...or am i not right?
                  Sorry for stupid questions...

                  jsulm 1 Reply Last reply Reply Quote 0
                  • jsulm
                    jsulm Lifetime Qt Champion @Jastot last edited by

                    @Jastot said in Trying to use QT from DLL in simple console project but it doesn't work:

                    So...its problem of QUiLoader...or am i not right?

                    Since you did not post the stack trace after the crash I don't know.
                    Subclassing QApplication is also not needed - adding qapp as class member should be enough.

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

                    J 1 Reply Last reply Reply Quote 0
                    • J
                      Jastot @jsulm last edited by

                      @jsulm

                      30e285ee-07af-4633-9b95-53c1e18c45ce-image.png

                      jsulm 1 Reply Last reply Reply Quote 0
                      • jsulm
                        jsulm Lifetime Qt Champion @Jastot last edited by

                        @Jastot Is it still crashing if you allocate quiLoader on the heap (just for testing)?

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

                        J 2 Replies Last reply Reply Quote 0
                        • J
                          Jastot @jsulm last edited by

                          @jsulm
                          Yes. Allocate it on the heap and the same error.

                          1 Reply Last reply Reply Quote 0
                          • J
                            Jastot @jsulm last edited by Jastot

                            Well, @jsulm , I am very grateful to you. Thank you.
                            I have found how to fix it and don't destroy logic.

                            JonB 1 Reply Last reply Reply Quote 0
                            • Topic has been marked as solved  J Jastot 
                            • JonB
                              JonB @Jastot last edited by

                              @Jastot said in Trying to use QT from DLL in simple console project but it doesn't work:

                              I have found how to fix it

                              Wouldn't you like to share that with us, and maybe benefit future readers? :)

                              J 1 Reply Last reply Reply Quote 1
                              • J
                                Jastot @JonB last edited by

                                @JonB
                                OPS. forget about it:D

                                The main problem wasn't in QApplication as I thought initially.
                                QUiLoader didn't want to work and showed an error that there was no access to the address.

                                So i created QApplication and QUiLoader in console project and then transfer pointer to library.
                                I still have a question why QCoreApplication::applicationDirPath(); is ok but quiLoader->addPluginPath(qstr); which calls QApplication, is not ok...

                                Simple Console Project:

                                int main(int argc, char* argv[])
                                {
                                	std::shared_ptr<QtUIEditorDLL> ptrQtUIEditorDLL(new QtUIEditorDLL(argc, argv)); // you can use it without any ptr.
                                	QtUIEditorDLL* test = ptrQtUIEditorDLL.get();
                                	QUiLoader* quiLoader = new QUiLoader(); 
                                // as i can see it must be created in the same tread near QApp. Jsulm said that it not important...but it doesn't work if create it in DLL.
                                	test->Init(quiLoader);
                                }
                                

                                And the library:

                                QtUIEditorDLL::QtUIEditorDLL(int& argc, char** argv)
                                	:QApplication(argc,argv){}
                                void QtUIEditorDLL::Init(QUiLoader* quiLoader)
                                {
                                	QString qstr = QCoreApplication::applicationDirPath();
                                	quiLoader->addPluginPath(qstr);
                                        //And it works. 
                                }
                                
                                1 Reply Last reply Reply Quote 1
                                • First post
                                  Last post