如何为非Qt应用程序创建Qt共享库 [英] How to create a Qt shared library for a non-Qt application
问题描述
现在我被要求制作Qt共享库作为一个理想的DLL,客户端应用程序不依赖于Qt框架。
我看到以下帖子,但在非Qt应用程序中使用基于Qt的DLL ,但不确定是否是最佳方法。
有些人可以启发我有可能选择开发用于非Qt应用程序的Qt共享库。
你可以在库中的新线程中创建 QCoreApplication
的实例。您应该检查它只创建一个实例,这是因为每个Qt应用程序应该只包含一个 QCoreApplication
。
所以你的图书馆可以像:
class Q_DECL_EXPORT SharedLibrary:public QObject
{
Q_OBJECT
public:
SharedLibrary();
私人插槽:
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应用程序。
I have developed an application that has Qt shared library and Qt application. Qt shared library exports a single class with few signals in it. I have made use of Q_DECL_EXPORT / Q_DECL_IMPORT macros for this. Right now the communication between the dll and application is through Qt signals and slots and that needs the application to be developed using QObject.
Now I was asked to make Qt shared library as an ideal DLL where Client application doesn't depend on Qt framework.
I saw the following post but Using a Qt-based DLL in a non-Qt application but not sure if that is the best approach.
Could some one please enlighten me with possible options to develop Qt shared library to be used in a non-Qt application.
You can create the instance of the QCoreApplication
in a new thread in the library. You should check to create only one instance of it, That's because each Qt application should contain only one QCoreApplication
.
So your library can be like :
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();
}
}
This way you can use your Qt shared library even in non Qt applications.
这篇关于如何为非Qt应用程序创建Qt共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!