Add qtjambi.jar to the classpath of your Java project containing the most essential Qt Core, Gui and Widgets modules. If you use Maven to build your application simply add following dependency to your project:
<dependency>
<groupId>io.qtjambi</groupId>
<artifactId>qtjambi</artifactId>
<version>$VERSION</version>
</dependency>
(exchange $VERSION
either by 6.5.10
or 6.8.1
).
Otherwise, download QtJambi JAR file from Maven Central Repository. Find the list of all available QtJambi modules.
Create a file Test.java containing the following code
import io.qt.widgets.*;
public class Test {
public static void main(String[] args) {
QApplication.initialize(args);
QMessageBox.information(null, "QtJambi", "Hello World!");
QApplication.shutdown();
}
}
Compile the file:
javac -cp qtjambi-6.8.1.jar Test.java
For execution you need the platform dependent binaries of QtJambi. For
instance, if you are working on Windows download the windows-x64
binaries. Additionally, you need Qt. Use the Qt
installer to install Qt on
your system. Make sure you are using the same Qt version and QtJambi
version (e.g. 5.15 or 6.5). On Linux, you can alternatively use Qt system libraries (correct version provided).
The required DLLs are to be found in the bin
folder on Windows and lib
folder on Linux and macOS, respectively.
When running a QtJambi application you have to make the locations of Qt and QtJambi libraries known to Java. Therefore, use the PATH environment (LD_LIBRARY_PATH on Linux, DYLD_FRAMEWORK_PATH on macOS) or the Java runtime property java.library.path.
The example program can be executed this way on Windows:
java -cp qtjambi-6.8.1.jar;qtjambi-native-windows-x64-6.8.1.jar;. -Djava.library.path=C:\Qt\6.8.1\msvc2022_64\bin Test
On Linux it looks this way:
java -cp qtjambi-6.8.1.jar:qtjambi-native-linux-x64-6.8.1.jar:. -Djava.library.path=<path to>/Qt/6.8.1/gcc_64/lib Test
On macOS you additionally need to use the start parameter -XstartOnFirstThread:
java -cp qtjambi-6.8.1.jar:qtjambi-native-macos-6.8.1.jar:. -Djava.library.path=<path to>/Qt/6.8.1/macos/lib -XstartOnFirstThread Test
If the example fails with a UnsatisfiedLinkError
QtJambi libraries and Qt libraries seem to be incompatible.
Read here about library requirements and compatibility.
QtJambi automatically detects the required native component jars if they are located next to their Java counterparts or in a subfolder native
.
You can simply skip qtjambi-native-OS-VERSION.jar
in your classpath (-cp
).
If you intend to use automatic module loading (java -p <dir>
) you strictly need to place native components in native
subfolder next to qtjambi-6.8.1.jar
.
Native bundles are extracted every time at program startup. By default, this is a process specific temporal directory purged after program shutdown.
Alternatively, you can use Java system property io.qt.deploymentdir
to let libraries to be exctacted and persist in user
application data path or common directory (see below).
In general, you can start learning how to use Qt in Java as it is introduced for C++. There are a special characteristics of QtJambi that are introduced here. Instead of starting your program with a java command as shown above you can deploy your application as executable as described here. Read here about creating self-exctracting bundles containing Qt library. Read more about how to debug a QtJambi program. Read more about developing applications for Android.
and QtJambi 6.5 API Reference Documentation
and QtJambi 6.8 API Reference Documentation
Following system properties are accepted by QtJambi.
You can specify Java system properties as start argument -Dproperty=value
or in Java code System.setProperty("property", "value")
.
io.qt.log-messages
- By specifying any combination ofALL
,CRITICAL
,DEBUG
,WARNING
,FATAL
,INFO
andSYSTEM
you can install a message handler forwarding messages of given types to Java logging. In combination with this use following properties to define logging levels. All specified values have to be parsable byLevel.parse(String)
:io.qt.log.debug.level
- Specify level value to define the log level forQtDebugMsg
(defaults toFINEST
)io.qt.log.critical.level
- Specify level value to define the log level forQtCriticalMsg
(defaults toSEVERE
)io.qt.log.fatal.level
- Specify level value to define the log level forQtFatalMsg
(defaults toSEVERE
)io.qt.log.info.level
- Specify level value to define the log level forQtInfoMsg
(defaults toINFO
)io.qt.log.warning.level
- Specify level value to define the log level forQtWarningMsg
(defaults toWARNING
)
io.qt.exceptions-for-messages
- By specifying any combination ofALL
,CRITICAL
,DEBUG
,WARNING
,FATAL
,INFO
andSYSTEM
you can install a message handler causing exceptions to be thrown in the event of a message of given type.
io.qt.enable-event-logs
- Specifytrue
to activate logging of all handled events with their receivers.io.qt.enable-method-logs
- Specifytrue
to activate method logging. Caution! This highly decreases performance especially in combination withio.qt.log-messages
or custom message handlers.io.qt.enable-cleanup-logs
- Specifytrue
to log native object cleanup after GC.io.qt.enable-dangling-pointer-check
- Specifytrue
to activate dangling pointer checks.io.qt.enable-signal-argument-check
- Specifytrue
to activate signal argument checks where applicable.io.qt.enable-signal-emit-thread-check
- Specifytrue
to activate thread checks on signal emits. If a signal is emitted from a foreign thread a warning is given. Install custom handler withQtUtilities.installSignalEmitThreadCheckHandler
to change this behavior.io.qt.enable-concurrent-container-modification-check
- Specifytrue
to activate concurrent modification checks during container iteration.io.qt.enable-thread-affinity-check
- Specifytrue
to activate thread affinity checks when calling certain thread-affine methods.io.qt.enable-event-thread-affinity-check
- ...the same applying to access checks during event handling.- check is disabled by default since QtJambi 6.5.1.io.qt.disable-thread-affinity-check
- check is disabled by default since QtJambi 6.5.1.io.qt.disable-event-thread-affinity-check
io.qt.library-path-override
- Use this property if you want to force Java to load Qt and QtJambi libraries from other paths than given byjava.library.path
.io.qt.debug
- Specifydebug
to force Java using debug libraries of Qt and QtJambi.io.qt.pluginpath
- Specify list of paths added as plugin search path.
If you don't specify QtJambi's native location in library path QtJambi will extract libraries from native bundles to temporary directory each time at program startup. Typically, it is a process specific directory purged at program termination. This behavior can be adapted with following properties:
io.qt.deploymentdir
- Specifytemp
to let QtJambi extract libraries to temporary directory (default),user
for user's application data directory andcommon
for common program data directory. Specify a target directory (io.qt.deploymentdir=<path>
) to let them be extracted at the specified location.io.qt.keep-temp-deployment
- Specifytrue
to avoid library deletion at program termination. The libraries remain in temporary directory instead.io.qt.no-native-deployment
- Specifytrue
if you want to inhibit the search for native library bundles at all and load QtJambi from library path instead.
Library extraction only takes place if QtJambi does not detect the required libraries elsewhere (e.g. in io.qt.library-path-override
, java.library.path
or system's library path).
When using a native debugger to debug your application you need to provide debuginfo along with the native Qtjambi libraries.
This information is available in specific bundles corresponding to native bundles. QtJambi will extract the debug info files if you enable it with io.qt.provide-debuginfo=true
.
The extraction behavior can be adapted by following properties:
io.qt.provide-debuginfo
- Specifytrue
to let QtJambi extract debuginfo files.
Along with debuginfo files, source code files could be extracted. However, this is not done unless you specify a target sources directory:
io.qt.sourcesdir
- Specifytemp
to let QtJambi extract source code files to temporary directory,user
for user's application data directory andcommon
for common program data directory. Specify a target directory (io.qt.sourcesdir=<path>
) to let them be extracted at the specified location.
For development purpose it is possible to let QtJambi extract header files from native bundles.
io.qt.headersdir
- Specifyuser
to let QtJambi extract header files to user's application data directory. Specifycommon
to let them be extracted to common program data directory. Specify a target directory to let them be extracted there.
io.qt.allow-nonfinal-signals
- Specifytrue
to avoid exception to be thrown when detecting non-final signal declarations.io.qt.no-library-shutdown-hook
- Specifytrue
to avoid library shutdown at program termination.io.qt.no-app-deletion
- Specifytrue
if you combine native code with Java code and yourQCoreApplication
instance has been created elsewhere than inside Java.io.qt.no-exception-forwarding-from-meta-calls
- Specifytrue
to avoidQMetaObject
calls (like method invocation and property access) forwarding exceptions to the Qt caller in any case (it is avoided by default in case of Qml-related QObjects).io.qt.no-exception-forwarding-from-virtual-calls
- Specifytrue
to avoid virtual calls (i.e. Java overrides) forwarding exceptions to the Qt caller in any case.io.qt.avoid-jni-global-references
- Specifytrue
to avoid using JNI global references. Instead, QtJambi will use a JavaHashMap
to manage references. (default on Android)io.qt.use-jni-global-references
- Only on Android: Specifytrue
to let QtJambi use JNI global references instead ofHashMap
to manage references.
io.qt.enable-qml-debugging
- Specifytrue
to allow QML debugging for the entire runtime of the application.- Specifyio.qt.enabled-qml-debugging
true
to allow QML debugging for the entire runtime of the application.io.qt.enable-qml-debugging-nowarn
- ...also inhibits a security warning.- ...also inhibits a security warning.io.qt.enabled-qml-debugging-nowarn
Along with this use program argument -qmljsdebugger=...
to enable QML debugging for Qt.
io.qt.experimental.fast-jni
- Specifytrue
to enable improved gathering of JNI environment during native callsio.qt.experimental.fast-jni-for-overrides
- Specifytrue
to enable improved gathering of JNI environment during native virtual calls
Following properties deal with the improvement of item model performance:
io.qt.experimental.item-model-cached-virtuals
- Specifytrue
to cache the return values of item model'srowCount()
andcolumnCount()
to improve performance of item views.io.qt.experimental.enable-invalid-modelindex-singleton
- Specifytrue
to use one single Java object to represent invalidQModelIndex
instead of copies.io.qt.experimental.enable-ephemeral-modelindexes
- Specifytrue
to call virtual Java methods with an alias for QModelIndex arguments instead of submitting a copies. These alias objects are invalidated after use.