Foreword
On Habré there was an article 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.
The essence
At the beginning, download jambi
qt.nokia.com/downloads , unpack, create a Java project in your development environment and create a Main class. Immediately bring your:
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 the simple demo of JAmbi" );
initControls ();
}
// Text field, from where we will take the text for output to the consoleprivate static final QTextEdit edit =
new QTextEdit (
"initial string" );
private void initControls ()
{
// Create a container for widgetsQLayout layout =
new QGridLayout (
this );
// Just a button.QPushButton button =
new QPushButton (
"push me" );
// Here we tell Qt that the “clicked” signal is needed// call “on_button1_clicked ()” on the “this” object.// Instead of this can be any other objectbutton.clicked.connect (
this ,
“on_button1_clicked ()” );
// add widgets to the containerlayout.addWidget (edit);
layout.addWidget (button);
}
void on_button1_clicked ()
{
System.out.println (edit.toPlainText ());
/ * Create a dialog from the generated build class * /// 1. Create an “empty” widgetQWidget dialog =
new QWidget ();
// 2. Create an instance builderUi_Dialog1 dialogProto =
new Ui_Dialog1 ();
// 3. Initialize the visual partdialogProto.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 thing is just in MainWindow.
The first thing I really liked was the simplicity of assigning actions to signals. Dzhavovskiy reflection api and the idea of slot / signal form a very convenient and easy to use binding mechanism.
The second in line was the user interface created in the designer. Here I met a small bug:
when trying to execute
jambi_home / bin / juic -d ../project ../project/test.ui , I got:
juic: not jambi fileThe problem turned out to be that juic is looking for a language = "jambi" in the ui node, but since the designer did not register it there, the validation check fails.
In the final, I received the generated
Ui_Dialog1.java , which contained not the ready-made widget code, but only the builder. To use it, it is enough to create an empty widget and set an instance method
setupUi (widget) of 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 completely thought out, because it turns out that for each widget instance there will be an instance of the builder class.
Conclusion
Jambi is a good alternative to swing / awt (in terms of performance, I think it will increase). Personally, I liked the simplicity of creating java gui. But before using it in a commercial project, I would think:
1. Is the performance gain worth the cost of a license
2. about possible problems in the distribution of the final product, because for each platform the native part of the library has its own.
The article is a reason 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 are using it in large projects.