Jambi Note (Qt Java bindings)
Foreword
There was an article on Habr about creating a Qt application in C ++, but there was no example for Java. I decided to spend 10 minutes of free time and write the simplest application. I was interested in:
- Creating the visual part
- Using slots and signals
Under the cut, a brief description of what I did.
Essence
First , download jambi qt.nokia.com/downloads , unpack it, create a java project in your development environment and create the Main class. I’ll bring my own
right away : package jambi.demo;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QGridLayout;
import com.trolltech.qt.gui.QLayout;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QTextEdit;
import com.trolltech.qt.gui.QWidget;
public class Main
{
/ *
* The class of our window
* /
public static class MainWindow
extends QWidget
{
public MainWindow ()
{
setWindowTitle ("This is simple demo of JAmbi");
initControls ();
}
// Text field, from where we will take the text for output to the console
private static final QTextEdit edit = new QTextEdit ("initial string");
private void initControls ()
{
// Create a container for widgets
QLayout layout = new QGridLayout (this);
// Just a button.
QPushButton button = new QPushButton ("push me");
// Here we tell Qt that for the “clicked” signal you need
// call “on_button1_clicked ()” on the “this” object.
// Instead of this, there can be any other
button.clicked.connect object (this, “on_button1_clicked ()”);
// add widgets to the container
layout.addWidget (edit);
layout.addWidget (button);
}
void on_button1_clicked ()
{
System.out.println (edit.toPlainText ());
/ * Creating a dialog from the generated builder class * /
// 1. Create an "empty" widget
QWidget dialog = new QWidget ();
// 2. Create an instance of the builder
Ui_Dialog1 dialogProto = new Ui_Dialog1 ();
// 3. Initialize the visual part of
dialogProto.setupUi (dialog);
// 4. Do the rest (link slots and signals)
dialogProto.pushButton.clicked.connect (this, “on_pushButton_clicked ()”);
dialog.show ();
}
void on_pushButton_clicked ()
{
System.out.println ("Push button clicked");
}
}
public static void main (String [] args)
{
// Create a Qt application to initialize the library
// and the graphics subsystem.
QApplication app = new QApplication (args);
QWidget mainWidget = new MainWindow ();
mainWidget.show ();
QApplication.exec ();
}
}
To initialize Qt, we create QApplication . But the most interesting is just in MainWindow.
The first thing I really liked was the ease of assigning actions to signals. The Java reflection api and the idea of slot / signal form a very convenient and easy to use linking mechanism.
The second in line was the user interface created in designer. Here I met a small bug:
when I tried to execute jambi_home / bin / juic -d ../project ../project/test.ui , I got:
juic: not jambi file
The problem turned out to be that juic is looking for the language = "jambi" property in the ui node, but since designer did not register it there, the validation check fails.
In the final, I got the generated Ui_Dialog1.java , which did not contain the widget code, but only the builder. To use it, just create an empty widget and set the installer method setupUi (widget) on the builder class on it.
All this happens during the execution of the program, which is probably not very good when creating windows with a large number of elements.
All interface elements are available as public members of the builder class, which makes the decision not very deliberate, because it turns out that for each instance of the widget there will be an instance of the builder class.
Conclusion
Jambi is a good alternative to swing / awt (in terms of performance, I think there will be an increase). Personally, I liked the simplicity of creating java gui. But before using it in a commercial project, I would have thought:
1. is the performance gain worth the license cost
2. about possible problems when distributing the final product, because each platform has its own native library.
The article is an occasion to discuss Jambi on Habré. I'd like to hear comments from people who have been working with this library for a long time and use it in large projects.