Making a singleton qobject calling QTimer - qt

I have a requirement for creating a singleton for QTimer across my program. The problem is that when the QTimer object times out, I get a segmentation fault.
I am currently using qt creator and the debug stack shows the crash on activation() call of the moc_ class for the same on the timeout event.
My current project uses the component architecture template(basically something like a star structure where multiple components communicate via a central component) and I intend to use the class over multiple components.
I originally created the same code on a separate project using the basic template where the code was working.
Hence, this brings me to my question; are QObjects not allowed to exist as singletons over multiple components in a component structure?

Related

Offscreen render with QOffscreenSurface using Docker

I am using Qt5 for off-screen rendering and have a segmentation fault when running inside Docker.
I have the code inside a c++ and calling the function via python using pybind11
This is the code causing the segfault
QSurfaceFormat glFormat;
glFormat.setVersion(3, 3);
glFormat.setProfile(QSurfaceFormat::CoreProfile);
glFormat.setRenderableType(QSurfaceFormat::OpenGL);
surface = new QOffscreenSurface();
surface->setFormat(glFormat);
surface->create(); // <-- Here
The exact error is :
QObject::connect: Cannot connect (null)::destroyed(QObject*) to QOffscreenSurface::screenDestroyed(QObject*)
I tried to run it via xvfb-run -a python prg.py and got the same error
Any pointers to debug this?
A little more context to the issue , the above code works fine if I use it inside main thread wrapped in QApplication. This problem occurs only when I move it inside a function so I can call it inside python.
Your last clue about running outside of the main thread is probably the problem you are running into.
Looking at the docs https://doc.qt.io/qt-5/qoffscreensurface.html#details:
Note: Due to the fact that QOffscreenSurface is backed by a QWindow on
some platforms, cross-platform applications must ensure that create()
is only called on the main (GUI) thread. The QOffscreenSurface is then
safe to be used with makeCurrent() on other threads, but the
initialization and destruction must always happen on the main (GUI)
thread.
So the solution would probably be to create the surface in the main thread and then just use it like you need it via pybind11.

Run JavaFX Application Thread/Toolkit in Java9

I'm trying to migrate a program from java 8 to java 9.
In my program I found the following code.
//Run JavaFX Application Thread/Toolkit
PlatformImpl.startup(() -> {
});
It tries to start the JavaFX Toolkit
Unfortunately, the PlatformImpl.startup is no longer supported in java 9.
Which substitute is there for it?
How can I start the JavaFX Toolkit?
Thank you
The startup() method is one of the methods that was promoted from the non-public class PlatformImpl to the public API Platform class in the Java 9 release. It is now fully documented in the API documentation.
Thus the equivalent call in Java 9 is
Platform.startup(() -> { });
Note that the use cases for this method are fairly rare, and the API docs go to some lengths to emphasize this:
In general it is not necessary to explicitly call this method, since it is invoked as a consequence of how most JavaFX applications are built.
...
As noted, it is normally the case that the JavaFX Application Thread is started automatically. It is important that this method only be called when the JavaFX runtime has not yet been initialized. Situations where the JavaFX runtime is started automatically include:
For standard JavaFX applications that extend Application, and use either the Java launcher or one of the launch methods in the Application class to launch the application, the FX runtime is initialized automatically by the launcher before the Application class is loaded.
For Swing applications that use JFXPanel to display FX content, the FX runtime is initialized when the first JFXPanel instance is constructed.
For SWT application that use FXCanvas to display FX content, the FX runtime is initialized when the first FXCanvas instance is constructed.
When an application does not follow any of these common approaches, then it becomes the responsibility of the developer to manually start the JavaFX runtime by calling this startup method.
Calling this method when the JavaFX runtime is already running will result in an IllegalStateException being thrown - it is only valid to request that the JavaFX runtime be started once.
So, while you are in the process of making the changes you need to update your application to be Java 9 compatible, you might want to carefully consider if you need to call startup() at all; maybe there is a more standard and robust approach to starting your JavaFX application anyway.

Forcefully crash qml pages

My application has qml as frontend and c++ as backend.
To check some issues, I want to forcefully made qml to crash.
Mainly I am interested what error message comes up when qml crashes on client site so that we can handle those error better.
I have tried few things like assigning null values or null pointer on QML page but it doesnt make application crash.
From C++ side, I load application from QQmlApplicationEngine.
Any advice will be helpful.
You can use debug mode in Qt Creator.
And the application will stop when it crashes. Then, you can check your stack.
QML won't crash :)
You can't really make some pure QML/JavaScript code "crash", at least not the same way that you can shoot yourself in the foot in C++.
Remember than QML and JavaScript are interpreted, and any error will typically generate a QML warning. Something like:
qrc:/main.qml:33: ReferenceError: timer is not defined
This error will interrupt whatever operation you're performing in your local scope, maybe mess-up a few bindings or not do exactly what you expect, but it won't "crash" your QML application.
But you can always crash your app
If you expose some C++ component to your QML, you can technically crash your app from QML by triggering some buggy C++ code from your QML context. But this is the same thing as crashing your app from your C++ backend....
You can catch errors (locally)
When dealing with QML/JavaScript errors, you can catch these errors the same way you would in regular JavaScript code, using try { ... } catch(e) { ... } mechanism, as demonstrated in Catch QML error message
You can report errors (globally)
Even though not catastrophic, an error in your QML may impact the behaviour of your QML UI. One possible possible way to prevent this is to write tests for you UI where you exerce your QML components in various ways to ensure no errors ensue.
One trick we use in our test code (could work in production too), is to override the default log handler and watch out for QML errors and warnings. This allows us to test our QML components (do they behave as expected?) and also verify that no warnings were generated during these tests. The relevant documentation is available here: http://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler

Swing to JavaFX2.0

I’m moving from Swing to JavaFX2.0. I have an internal service with various JFrame windows to display/alter internal parameters. I have a menu with buttons. Each button kicks off one of the JFrame windows as such:
new ASvr().setVisible(true);
Works fine. Now going to JavaFX2.0: Each “window” is an independent FXML application. The independent FXML applications run as standalone applications when the service is remote using RPC to communicate with the service. This is the internal version where the “windows” are part of the same JVM.
The Menu starts with application.launch(). There can only be one launch() in the JVM so I can’t use launch() for the independent FXML applications. After much trial and error (mostly error) what I’ve come up with is:
new ASvr().start(new Stage());
Works fine, I think. Since I’m new to JavaFX I could be missing something which will ruin this solution. Calling start() myself, not from a launch(), may have nasty side effects. I can’t use a new ProcessBuilder and put each application inside a new JVM since I need direct access to references within the internal service.
My questions: Is there a preferred way to have several independent (need to run as standalone without alteration) FXML application scenes running within a JVM? Do you see any potential problems with what I did?
I would not recommend calling start(...) yourself. The launch(...) method does a lot of complex housekeeping, such as starting the FX toolkit and the FX Application Thread, instantiating the application class, and then invoking start(...) on the FX Application thread.
You should basically think of start(...) as the equivalent of the main(...) method for a JavaFX application, and the Application subclass as the "main-class", not the JFrame subclass. (Indeed, in Java 8, if your class is a subclass of Application it can be launched directly by the JVM without even having a main(...) method.)
Once you have created your single start(...) method (again, just think of it as the replacement for your single main(...) entry point from a "regular" Java application), then you can show the contents of an FXML file with the idiom
Stage stage = new Stage();
stage.setScene(new Scene(FXMLLoader.load(fxmlResource));
stage.show();
(Of course, there are variations on this, especially if you need access to the controller, but you get the idea.)
You could if you wanted create subclasses of Stage, in the old swing style of creating JFrame subclasses, and let the constructor of the Stage subclass load its FXML and set its own scene. Then you could use essentially the same idiom as you used in Swing:
new MyStageSubclass().show();
Notice again how the analogue of a JFrame subclass is a Stage subclass, not an Application subclass.

Qt: Writing plugins for other applications

I am writing a plugin for another application. I want to support the plugin on multiple platforms, so I am strongly considering using Qt.
The plugin needs to be able to show some basic GUI. The plugin interface does not in any way handle GUI - it is just a simple DLL/shared library specified with a C-header file.
Can I use Qt inside such a shared library? The calling application might or might not be using Qt itself. Any hints on what to do? Do I need to run a QApplication event-loop in a separate thread? Or can I just call the event-loop myself while waiting for input? (I only need modal dialogs).
I don't think it is possible because you need to create the QApplication eventloop in the main thread.
Note that QCoreApplication::exec()
must always be called from the main
thread (the thread that executes
main()), not from a QThread. In GUI
applications, the main thread is also
called the GUI thread because it's the
only thread that is allowed to perform
GUI-related operations.

Resources