将Qt GUI添加到动态库 [英] Adding a Qt GUI to a Dynamic Library

查看:230
本文介绍了将Qt GUI添加到动态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候溢出。
我试图添加一个GUI到现有项目。更具体地说,一个作为 .so 文件加载的插件(或在win32上编译时 .dll

Greetings overflowers. I am trying to add a GUI to to an existing project. More specifically to a plugin that is loaded as a .so file (or when compiled on win32 a .dll)

该项目已经有了自己的线程实现来处理可移植性。我知道Qt有自己的跨平台线程模型,但最好是保持在这个现有的线程模型中。

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老兵在那里只是刚刚开始阅读文档]是:是否可能嵌入一个GUI使用Qt到插件如上所述?插件已经是一个命令行接口,我想让GUI可选,即使它的编译。由于这些标准函数被主程序调用,GUI(我假设将居住在另一个线程)将有可访问或能够有调用的方法,以便CLI线程可以共存,并且标准函数可以与两个接口的任何排列一起工作。

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? 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。插件已经是CLI,并且具有从主程序调用的函数。我只是创建一个新的线程初始化插件,并从那里启动阻止GUI:

after playing with the code a bit I am able to launch a simple GUI from the plugin. The plugin already is the CLI and has functions that are called from the main program. I simply created a new thread on initialization of the plugin and launched the blocking GUI from there:

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

这里的问题是:是否可以与GUI通信或者从CLI访问GUI元素线程?

The question here is: Is it possible to communicate with the GUI or rather access GUI elements from the CLI thread?

好了,到目前为止,工作没有问题。我可以从主插件线程访问GUI中的窗口小部件。我理解,这种做法不鼓励,不仅是迄今为止收到的答案,而且Qt库都吐出一些警告关于不安全访问另一个线程。

Alright, so far starting the blocking GUI in a separate thread has worked with no problems. I am able to access widgets in the GUI from the main plugin thread as well. 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.

到目前为止,我只是在linux环境中工作,也许真正的问题将提交给其他系统。我已经看到只有一个毛刺,我不能说肯定是相关的:

As of now I have only been working in a linux environment, perhaps real issues will be presented on other systems. 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);

我想我的最后一个问题是从其他线程访问Qt GUI的具体情况是不安全的,为什么?'

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从一个dll或者从非Qt应用程序,但它不能来自辅助线程,它必须在主线程中运行。并且应用程序事件循环通过阻塞方法启动,阻塞方法在GUI被关闭时返回,因此如果您需要在您的应用程序中运行独立于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. 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.

如果你觉得有野心,你可以修改QCoreApplication和QEventLoop类,这样你可以从你的调用应用程序管理事件循环,它可能会'所有的困难。但是据我所知,没有办法用Qt开箱。

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. But as far as I know there's no way to do it with Qt out of the box.

这篇关于将Qt GUI添加到动态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆