简体   繁体   English

将Qt GUI添加到动态库

[英]Adding a Qt GUI to a Dynamic Library

Greetings overflowers. 问候溢出。 I am trying to add a GUI to to an existing project. 我正在尝试将GUI添加到现有项目中。 More specifically to a plugin that is loaded as a .so file (or when compiled on win32 a .dll ) 更具体地说,是作为.so文件加载的插件(或者在win32上编译时.dll

The project has its own threading implementation already to deal with portability. 该项目已经有自己的线程实现来处理可移植性。 I know that Qt has its own cross platform threading model but it would be preferable to stay within this existing threading model. 我知道Qt有自己的跨平台线程模型,但最好留在现有的线程模型中。

My question to the Qt veterans out there [I have only just started reading the docs] is: Would it be possible to embed a GUI using Qt in to a plugin as described above? 我对Qt老手的问题[我刚刚开始阅读文档]是:如上所述,是否可以使用Qt将GUI嵌入到插件中? The plugin already is a command line interface and I would like to have the GUI optional, even if its compiled in. Since those standard functions get called by the main program, the GUI (which I assume will live in another thread) will have to be accessible or able to have methods called on it so that the CLI thread can coexist and the standard functions can work with any permutation of the two interfaces. 该插件已经是一个命令行界面,我希望GUI可选,即使它已编译。由于这些标准函数被主程序调用,GUI(我假设将存在于另一个线程中)将不得不可访问或能够调用方法,以便CLI线程可以共存,标准函数可以与两个接口的任何排列一起使用。

edit 1: 编辑1:

after playing with the code a bit I am able to launch a simple GUI from the plugin. 在使用代码后,我可以从插件中启动一个简单的GUI。 The plugin already is the CLI and has functions that are called from the main program. 该插件已经是CLI,并具有从主程序调用的功能。 I simply created a new thread on initialization of the plugin and launched the blocking GUI from there: 我只是在初始化插件时创建了一个新线程,并从那里启动了阻塞GUI:

QApplication app(NULL, NULL);
window = new zGui;
window->show();
app.exec();

The question here is: Is it possible to communicate with the GUI or rather access GUI elements from the CLI thread? 这里的问题是:是否可以与GUI通信或者从CLI线程访问GUI元素?

edit 2: some results 编辑2:一些结果

Alright, so far starting the blocking GUI in a separate thread has worked with no problems. 好吧,到目前为止,在一个单独的线程中启动阻塞GUI已经没有问题。 I am able to access widgets in the GUI from the main plugin thread as well. 我也可以从主插件线程访问GUI中的小部件。 I understand that this practice is discouraged as not only per the answers I've received so far but also the Qt libs are spitting out some warning about unsafe access by another thread. 据我所知,这种做法不仅仅是因为我到目前为止收到的答案,而且Qt库正在吐出一些关于另一个线程不安全访问的警告。

As of now I have only been working in a linux environment, perhaps real issues will be presented on other systems. 截至目前我只在linux环境中工作,也许真正的问题将在其他系统上呈现。 I have seen only one glitch that I can not say for sure is related: 我只看到一个我无法肯定的故障是相关的:

Upon changing the the max and min values of a progress bar widget, the progress bar appears blank. 更改进度条小组件的最大值和最小值后,进度条显示为空白。 I was able to apply a simple fix to this by the following 我能够通过以下方式对此进行简单的修复

//here is me setting the values
window->progressBar->setMaximum(character.maxHP);
window->progressBar_2->setMaximum(character.maxMP);
window->progressBar->setValue(character.curHP);
window->progressBar_2->setValue(character.curMP);

//and here is the fix
window->progressBar->setVisible(false);
window->progressBar->setVisible(true);
window->progressBar_2->setVisible(false);
window->progressBar_2->setVisible(true);

I suppose my final question is 'What specifically are the situations where accessing a Qt GUI from an other thread is unsafe and why?' 我想我的最后一个问题是“从其他线程访问Qt GUI不安全的原因是什么?为什么?”

You can use a Qt GUI from a dll or so that is called from a non-Qt application, but it cannot be from a secondary thread, it has to run in the main thread. 您可以使用从非Qt应用程序调用的dll中的Qt GUI,但它不能来自辅助线程,它必须在主线程中运行。 And the application event loop is started via a blocking method that returns when the GUI is closed out, so if you needed to have logic running in your app that is independent of the GUI, then that logic would need to be running in a secondary thread. 应用程序事件循环通过阻塞方法启动,该方法在GUI关闭时返回,因此如果您需要在应用程序中运行独立于GUI的逻辑,那么该逻辑需要在辅助线程中运行。

If you felt ambitious, you could modify the QCoreApplication and QEventLoop classes in such a way that you can manage the event loop from your calling application, and it probably wouldn't be all that difficult. 如果您觉得有野心,可以修改QCoreApplication和QEventLoop类,以便您可以从调用应用程序管理事件循环,并且可能不会那么困难。 But as far as I know there's no way to do it with Qt out of the box. 但据我所知,开箱即用Qt无法做到这一点。

Consideing to Gerald answer, might I suggest that its better to keep the CLI (your app) separate from your apps GUI (ergo, a separate app). 考虑到Gerald的回答,我是否可以建议将CLI(您的应用程序)与您的应用程序GUI(ergo,一个单独的应用程序)分开更好。

Make your GUI app use the cli in the background. 使您的GUI应用程序在后台使用cli。 its easily done by using QProcess. 它很容易通过使用QProcess完成。

cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM