Qt supports a very nice abstraction layer between the Graphical representation and the control logic. This separation makes the code much cleaner, much more debuggable (in terms of unit testing) and increases confidence (you can be sure that changing the graphical representation should have no effect on the logic behind). QtDesigner lets the programmer design their user interfaces in an xml format (User interface or .ui files as they are called) which can be linked with any programming language including C++, Python, Java, Ruby, etc. This has an obvious scalability factor for reusing and maintaining the same look and feel across several platforms.
This tutorial covers a Hello World example working with JUI files.Creating a Java Project
- Create a new Java Project File -> New -> Project -> Java -> Java Project
- Give the project a suitable name
- Click Finish
- Create a new package. Right click the Project -> New -> Package
- Give the package a suitable name
- Click Finish
- Right click the created Package -> New -> Class
- Give the class a suitable Name
- Click Finish
Converting an existing .ui file to .java class
For QtJambi the usual .ui files have to be modified first to Java specific .jui files. This requires a conversion process. I intend to automate it since it is really straight forward.
.ui file to .jui file
- Open the .ui file using a text editor (I used gedit)
- Remove the first line of the file which looks something like this:
<?xml version="1.0" encoding="UTF-8"?> - Delete the whitespace on the top of the file.
- Modify this line of code : <ui version="4.0"> to look like this <ui version="4.0" language="jambi"> This line should be at the top of the file.
- Modify the file extension to be .jui
.jui file to .java file
- Go to the QtJambi Installation path e.g. C:\qtjambi-4.8.6\bin
- Open the command prompt and enter the following line. Replace the <variable> with suitable names according to your needs.
juic.exe <filename.jui> -d <output_dir> -p <eclipse package name> - Alternatively, you could also add the QtJambi Installation path to the windows $PATH so you dont have to navigate to the bin folder.
Add Qt libraries to the project
- Right click package -> Build Path -> Configure Build Path …
- Click Library -> Add External Jar
- Browse to C:\qtjambi-4.8.6 and select qtjambi-4.8.6.jar file
- Select the added jar file. Open the drop down list -> Native library location -> Edit
- Click External Folder and Browse to the QtJambi Installation path e.g. C:\qtjambi-4.8.6\lib
- Click Ok
- The package should now find the references
- Make sure the class name matches with the .java file
Reference the ui file in the Main Java class
After we have the generated Ui_<ui_class_name>.java file, we can integrate it in the java class library. Here is a template on how to do that:
class_name.java
package <package_name>; import com.trolltech.qt.gui.*; public class <class_name> { public static void main(String[] args) { // TODO code application logic here QApplication.initialize(args); QMainWindow window = new QMainWindow(); Ui_<ui_class_name> ui = new Ui_<ui_class_name>(); ui.setupUi(window); window.show(); QApplication.execStatic(); } }
QtJambiCls.java
package QtJambiJuiPck; import com.trolltech.qt.gui.*; public class QtJambiCls { public static void main(String[] args) { // TODO code application logic here QApplication.initialize(args); QMainWindow window = new QMainWindow(); Ui_TestJuiForm ui = new Ui_TestJuiForm(); ui.setupUi(window); window.show(); QApplication.execStatic(); } }
Congratulations, now you can develop GUI applications with .ui files
No comments:
Post a Comment