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. Trying to use QT from DLL in simple console project but it doesn't work
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 768 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.
  • jsulmJ jsulm

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

    J Offline
    J Offline
    Jastot
    wrote on last edited by Jastot
    #5

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

    jsulmJ 1 Reply Last reply
    0
    • J 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...

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @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
      2
      • jsulmJ jsulm

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

        J Offline
        J Offline
        Jastot
        wrote on last edited by
        #7

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

        jsulmJ 1 Reply Last reply
        0
        • J Jastot

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @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
          0
          • jsulmJ jsulm

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

            J Offline
            J Offline
            Jastot
            wrote on last edited by
            #9

            @jsulm

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

            jsulmJ 1 Reply Last reply
            0
            • J Jastot

              @jsulm

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #10

              @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
              0
              • jsulmJ jsulm

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

                J Offline
                J Offline
                Jastot
                wrote on last edited by
                #11

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

                1 Reply Last reply
                0
                • jsulmJ jsulm

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

                  J Offline
                  J Offline
                  Jastot
                  wrote on last edited by Jastot
                  #12

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

                  JonBJ 1 Reply Last reply
                  0
                  • J Jastot has marked this topic as solved on
                  • J Jastot

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

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

                    @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
                    1
                    • JonBJ JonB

                      @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 Offline
                      J Offline
                      Jastot
                      wrote on last edited by
                      #14

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

                      • Login

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