我开发了一个具有 Qt 共享库和 Qt 应用程序的应用程序。 Qt 共享库导出一个类,其中包含很少的信号。我为此使用了 Q_DECL_EXPORT / Q_DECL_IMPORT 宏。目前,dll 和应用程序之间的通信是通过 Qt 信号和插槽进行的,因此需要使用 QObject 来开发应用程序。
现在我被要求将 Qt 共享库制作为理想的 DLL,其中客户端应用程序不依赖于 Qt 框架。
我看到了下面的帖子但是在非 Qt 应用程序中使用基于 Qt 的 DLL但不确定这是否是最好的方法。
请有人告诉我开发用于非 Qt 应用程序的 Qt 共享库的可能选项。
您可以创建QCoreApplication
在库中的新线程中。您应该检查只创建它的一个实例,这是因为每个 Qt 应用程序应该只包含一个QCoreApplication
.
所以你的图书馆可以是这样的:
class Q_DECL_EXPORT SharedLibrary :public QObject
{
Q_OBJECT
public:
SharedLibrary();
private slots:
void onStarted();
private:
static int argc = 1;
static char * argv[] = {"SharedLibrary", NULL};
static QCoreApplication * app = NULL;
static QThread * thread = NULL;
};
SharedLibrary::SharedLibrary()
{
if (thread == NULL)
{
thread = new QThread();
connect(thread, SIGNAL(started()), this, SLOT(onStarted()), Qt::DirectConnection);
thread->start();
}
}
SharedLibrary::onStarted()
{
if (QCoreApplication::instance() == NULL)
{
app = new QCoreApplication(argc, argv);
app->exec();
}
}
这样,即使在非 Qt 应用程序中,您也可以使用 Qt 共享库。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)