## Qt-based GUI on worker thread

mroavi
Posts: 2
Joined: Fri Sep 20, 2019 8:00 am

### Qt-based GUI on worker thread

I am currently trying to plot the realtime acquired signals using a Qt-based library called QCustomPlot. I spent some time analyzing what is the best possible approach to achieve this. The most important requirement is to guarantee that the GUI will not influence the realtime acquisition and processing of the audio signals. I decided to implement this using a C++ worker thread in my plugin that takes care of the GUI. I managed to integrate the Qt-based plotting library successfully. However, I haven't managed to get it running in the background. I am running this piece of code inside the prepare method of my plugin:

Code: Select all

        int argc = 0;
char **argv = NULL;
(
[&] {
QApplication application(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return application.exec();
}
);
t1.detach();
Unfortunately, I've been stuck for some time now due to a segmentation fault:

Code: Select all

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
I wanted to ask for some opinions and advise about the approach I chose to solve this problem. Do you think I'm going in the right direction? Is there another much better approach and easier to implement than the one I'm currently striving for? If you need extra information please let me know. Thanks in advance!

tobiasherzke
Posts: 27
Joined: Mon Jun 24, 2019 12:51 pm

### Re: Qt-based GUI on worker thread

The most important requirement is to guarantee that the GUI will not influence the realtime acquisition and processing of the audio signals
I agree with that sentiment.

I do not know much about Qt. In your code, I noticed:
• argc and argv are function-local variables. They may be used by your thread after the function has exited, i.e. when the variables do not exist anymore.
• Values argc == 0 and argv == NULL may not be supported by QApplication.
• Just speculation: Maybe you can manage the lifetime of the Qt application some more, e.g. make QApplication and the thread members of your plugin class, signal the QApp to terminate in release and join the thread.
For visualizations, we have mostly used a different approach from yours: Plugins like ac2osc and ac2lsl can export data computed in the MHA over the network. A visualization can then execute in a separate process on the same host as the openMHA or on a different computer in the same network. That said, I think one MHA user was once displaying a Gtk
GUI directly from a self-written plugin similar to what you are trying to do, it should therefore be possible. Please keep us updated with your progress, even if we cannot help much directly.

mroavi
Posts: 2
Joined: Fri Sep 20, 2019 8:00 am

### Re: Qt-based GUI on worker thread

I managed to get the Qt-based UI working on a worker thread. The solution I found is based on these two Stack Overflow posts: