1

I have the following Dll, which should show a single QWidget:

file testqtlib.h

#pragma once

#include <QtCore/qglobal.h>

#if defined(TESTQTLIB_LIBRARY)
#  define TESTQTLIB_EXPORT Q_DECL_EXPORT
#else
#  define TESTQTLIB_EXPORT Q_DECL_IMPORT
#endif

extern "C" TESTQTLIB_EXPORT void createWidget();

file testqtlib.cpp

#include <QtWidgets>
#include "testqtlib.h"

void createWidget() {
    auto widget = new QWidget;
    widget->resize(100, 100);
    widget->move(0, 0);
    widget->show();
}

I try to call the createWidget() function from another Qt-program, which originally does not contain gui and widgets in its .pro-file:

QLibrary library("testQtLib.dll");
library.load();
typedef void (*testWidget)(void);
auto crW = (testWidget)library.resolve("createWidget");
if (crW) {
  debugFile << __func__ << "crW OK:" << std::endl;
  crW();
} else
  debugFile << __func__ << "crW empty" << std::endl;

debugFile shows that crW was true (non-zero), but the calling program crashs at the call crW(). What is the reason?

Qt5Gui.dll and Qt5Widgets.dll are available in the system path. If I try the same call from the program, which contains gui and widgets in its .pro-file, everything works fine.

Should I load somehow Qt5Gui.dll and Qt5Widgets.dll or what can be a solution?

Borys L.
  • 145
  • 1
  • 9
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) - [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Jesper Juhl May 09 '23 at 08:58
  • 2
    Where is the `QApplication` instance created? You can't create widgets without that. – G.M. May 09 '23 at 09:15
  • Since the calling application was without GUI/Widgets, the instance of QCoreApplication was created. Could it be the reason? – Borys L. May 09 '23 at 09:19
  • @BorysL. As far as I'm aware you *must* use a `QApplication` if you want to create any `QWidget` instances -- a `QCoreApplication` will not suffice. Check your console logs or whatever -- I would expect to see some sort of diagnostic message being output regarding this. – G.M. May 09 '23 at 11:43

0 Answers0