SourceForge.net Logo Valid HTML 4.01 Transitional

UI to SWT Translator

A translator that converts Qt Designer UI files into SWT Java classes. Use the power of Qt Designer to generate your user interface and then automatically generate SWT Java source code.

Contents

  1. Overview
  2. References
  3. Download
  4. Usage
  5. Translations
  6. License
  7. Contact

Overview

The ui2swt translator works in a similar way to the uic application of Qt. It is used to generate source code from a .ui file created by Qt Designer.

Qt Designer is used to design a main widget (Main Window, Dialog or Form) which is then saved to a .ui file containing XML expressing all the attributes of the main widget and all its child widgets. A translator such as ui2swt or uic is then used to generate source code for a class representing the main widget. All of the child widgets are public attributes/members of the class and are instantiated when a new instance of the class is instantiated. The generated code will use a particular widget toolkit so that the main widget class will extend (be derived from) a class in the toolkit, and the child attributes will be instances of a class in the toolkit.

For uic the generated code is C++ and the widget toolkit is Qt.
For ui2swt the generated code is Java and the widget toolkit is Eclipse SWT.

The ui2swt translator supports all versions (2.x.x, 3.x.x and 4.x.x) of Qt Designer.


References


Download

The ui2swt translator source and executable JAR file can be downloaded from SourceForge.

Usage

The ui2swt translator is conveniently provided as an executable JAR file. Simply specify the name of the .ui file on the command line and the translator will generate a .java file of the same name in the current directory.

Eg:

  java -jar ui2swt.jar MyMainWindow.ui
Will generate a MyMainWindow.java file in the current directory.

NOTE: The name of the main widget in the .ui file should be the same as the name of the .ui file to confirm to the Java requirement that the name of the class corresponds to the name of the .java file. In the example above the name of the main widget should be MyMainWindow.

Output File (-o | --output)

The name of the generated file can be changed by using the -o or --output option.

Eg:

  java -jar ui2swt.jar MyMainWindow.ui -o <source code path>/MyMainWindow.java

Package Name (-p | --package)

The package name of the generated class is determined from the path name of the .ui file by joining the parent directories. For example, if the .ui file is net/sourceforge/MyMainWindow.ui then the package name of the generated class will be net.sourceforge. Note that this usually only works when relative path names are used.

The package name can be set using the -p or --package option followed by a dot '.' separated package name.

Eg:

  java -jar ui2swt.jar MyMainWindow.ui -p net.sourceforge

Verbose (-v | --verbose)

The -v or --verbose option can be used to output information about the file being translated.

Eg.

  java -jar ui2swt.jar -v net/sourceforge/MyMainWindow.ui
Generates the following output:
  Translating UI file...
  input file:   net/sourceforge/MyMainWindow.ui
  output file:  MyMainWindow.java
  package name: net.sourceforge
  UI version:   4

Translations

Main Widget

The main widget of the .ui file is translated into a Java class and the name of the class is the same as the name of the widget. The following table shows which, if any, class of the SWT toolkit the generated class extends.

Qt Widget SWT Widget Description
QWidget Composite A Qt Form (QWidget) generates an extension of the SWT Composite class with the constructor taking the same parameters as that of a composite.

For example the following is part of the code generated from a Qt Form with the name MyForm.

import org.eclipse.swt.widgets.Composite;

public class MyForm extends Composite
{
    public MyForm( Composite iParent, int iStyle )
    {
        super(iParent, iStyle);
        ...
    }
}
QDialog
QMainWindow
none Ideally the generated class for a Qt QDialog or QMainWindow would extend the SWT Shell class, however, the Shell class cannot be extended so the generated class does not extend any SWT class. Instead the shell object should be created first and then passed as a parameter to the dialog or main window constructor. Any properties/attributes of the dialog or main window will apply to the shell object and the children of the dialog or main window will be children of the shell.

The following shows part of the generated class code for a main window.

import org.eclipse.swt.widgets.Shell;

public class MyMainWindow
{
    public Label aLabel;

    public MyMainWindow( final Shell iParent )
    {
        this.aLabel = new Label(iParent,SWT.NONE);
        ...
    }
}
The following code shows how the generated main window class may be used.
Display      display    = new Display();
Shell        shell      = new Shell(display);
MyMainWindow mainWindow = new MyMainWindow(shell);

shell.open();
while ( !shell.isDisposed() )
{
    if ( !display.readAndDispatch() )
    {
        display.sleep();
    }
}
display.dispose();

Child Widgets

The generated Java class contains a public attribute for each child or descendant widget of the main widget. The name of the attribute is the same as the name of the widget and the type of the attribute is determined by the following table which shows the SWT widget corresponding to a Qt widget.

The SWT Styles column of the table shows the styles they may be applied to the generated widget depending on the attributes/properties of the corresponding Qt Widget. The styles in bold are the default styles which are always applied to the widget.

The SWT Methods column shows the methods that may be called on the generated widget depending on the attributes/properties of the Qt Widget.

Qt Widget SWT Widget SWT Styles SWT Methods
QButtonGroup Group   setEnabled(), setSize(), setToolTipText(), setText(), setFont()
QCheckBox Button SWT.CHECK setEnabled(), setSize(), setToolTipText(), setFont(), setText(), setImage(), setSelection()
QComboBox Combo SWT.BORDER
SWT.READ_ONLY
setEnabled(), setSize(), setToolTipText(), setFont()
QFrame Composite SWT.BORDER setEnabled(), setSize(), setToolTipText()
QGroupBox Group   setEnabled(), setSize(), setToolTipText(), setText(), setFont()
QLabel Label SWT.LEFT
SWT.CENTER
SWT.RIGHT
setEnabled(), setSize(), setToolTipText(), setFont(), setText(), setImage()
QLayoutWidget Composite   setEnabled(), setSize(), setToolTipText()
QLine Label SWT.SEPARATOR
SWT.HORIZONTAL
SWT.VERTICAL
setEnabled()
QLineEdit Text SWT.SINGLE
SWT.BORDER
SWT.PASSWORD
SWT.READ_ONLY
SWT.LEFT
SWT.CENTER
SWT.RIGHT
setEnabled(), setSize(), setToolTipText(), setFont(), setText(), setEchoChar(), setSelection()
QListBox
QListView
QListWidget
List SWT.BORDER
SWT.SINGLE
SWT.MULTI
SWT.H_SCROLL
SWT.V_SCROLL
setEnabled(), setSize(), setToolTipText(), setFont()
QMenu Menu SWT.DROP_DOWN  
QMenuBar Menu SWT.BAR  
QMultiLineEdit Text SWT.MULTI
SWT.BORDER
SWT.PASSWORD
SWT.READ_ONLY
SWT.LEFT
SWT.CENTER
SWT.RIGHT
setEnabled(), setSize(), setToolTipText(), setFont(), setText(), setEchoChar(), setSelection()
QProgressBar ProgressBar SWT.HORIZONTAL
or
SWT.VERTICAL
setEnabled(), setSize(), setToolTipText(), setFont(), setMinimum(), setMaximum(), setSelection()
QPushButton Button SWT.PUSH
or
SWT.TOGGLE
setEnabled(), setSize(), setToolTipText(), setFont(), setText(), setImage(), setSelection()
QRadioButton Button SWT.RADIO setEnabled(), setSize(), setToolTipText(), setFont(), setText(), setImage(), setSelection()
QSlider Scale SWT.HORIZONTAL
or
SWT.VERTICAL
setEnabled(), setSize(), setToolTipText(), setFont(), setMinimum(), setMaximum(), setSelection(), setIncrement(), setPageIncrement()
QSpacer Label    
QSpinBox Spinner SWT.BORDER
SWT.READ_ONLY
setEnabled(), setSize(), setToolTipText(), setFont(), setMinimum(), setMaximum(), setSelection(), setIncrement(), setPageIncrement()
QSplitter SashForm SWT.SMOOTH
SWT.HORIZONTAL
or
SWT.VERTICAL
setEnabled(), setSize(), setToolTipText()
QTabWidget TabFolder SWT.TOP setEnabled(), setSize(), setToolTipText()
QTable
QTableWidget
QTableView
Table SWT.BORDER
SWT.SINGLE
SWT.MULTI
SWT.H_SCROLL
SWT.V_SCROLL
setEnabled(), setSize(), setToolTipText(), setFont()
QTextBrowser Browser SWT.BORDER setEnabled(), setSize(), setToolTipText(), setText()
QTextEdit Text SWT.MULTI
SWT.BORDER
SWT.PASSWORD
SWT.READ_ONLY
SWT.LEFT
SWT.CENTER
SWT.RIGHT
SWT.H_SCROLL
SWT.V_SCROLL
setEnabled(), setSize(), setToolTipText(), setFont(), setText(), setEchoChar(), setSelection()
QTextView Text SWT.MULTI
SWT.READ_ONLY
SWT.BORDER
SWT.LEFT
SWT.CENTER
SWT.RIGHT
SWT.H_SCROLL
SWT.V_SCROLL
setEnabled(), setSize(), setToolTipText(), setFont(), setText(), setSelection()
QToolBar ToolBar SWT.FLAT All toolbars are created within a CoolBar.

ToolBar:
setEnabled()

ToolItem:
setText() or setImage()

QToolBox ExpandBar SWT.V_SCROLL setEnabled(), setSize(), setToolTipText()
QToolButton Button SWT.TOGGLE setEnabled(), setSize(), setToolTipText(), setFont(), setText(), setImage(), setSelection()
QTree
QTreeWidget
QTreeView
Tree SWT.BORDER
SWT.SINGLE
SWT.MULTI
SWT.H_SCROLL
SWT.V_SCROLL
setEnabled(), setSize(), setToolTipText(), setFont()
QWidget Composite   setEnabled(), setSize(), setToolTipText()

Layouts

The SWT Grid layout, and its corresponding GridData, is used to mimic the behaviour of the Qt QGridLayout, QHBoxLayout and QVBoxLayout layouts. The margin and spacings attributes of the Grid class correspond to the similar attributes of the Qt layout widgets. The grabExcessSpace and alignment attributes of the GridData are used to capture the size type (Fixed, Expanding, etc.) attributes of the Qt widgets so that the layout behaviour of the translated widgets is as close to the Qt widgets as possible.

Actions

The generated Java class contains a public Runnable attribute for each action in the UI file and the name of the attribute is the same as that of the action. For menu or toolbar items which correspond to an action, a SelectionListener is added to the item that will run the action Runnable attribute when the item is selected (provided the attribute is not null). The runnable action attributes should be set after the main widget object is created to perform the specific task for the action.

The following example shows a main window that has a fileOpenAction. The translator generates a fileOpenAction Runnable attribute for the action which in this case is assigned a new anonymous Runnable object to perform the appropriate action when it is run.

  MyMainWindow mainWindow = new MyMainWindow(shell);

  mainWindow.fileOpenAction =
      new Runnable()
      {
          public void run()
          {
              // perform file open action
              ...
          }
      };

License

ui2swt is distributed under the zlib/libpng license, which is OSS (Open Source Software) compliant.

See:

Copyright (c) 2006-2007 James Forbes, All Rights Reserved.

This software is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

Contact

For all comments, problems and suggestions regarding the ui2swt translator contact the author at:


last updated: 11 August 2007