Trying to use QT from DLL in simple console project but it doesn't work
-
@Jastot What is FormLoader? Please post the whole error message.
Also, did you do debugging and check the stack trace after the crash?@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, 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...
@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().
-
@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().
@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
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...@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. -
@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. -
-
Well, @jsulm , I am very grateful to you. Thank you.
I have found how to fix it and don't destroy logic. -
@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? :)
@JonB
OPS. forget about it:DThe 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. }