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.
Eg:
java -jar ui2swt.jar MyMainWindow.uiWill 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.
Eg:
java -jar ui2swt.jar MyMainWindow.ui -o <source code path>/MyMainWindow.java
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
Eg.
java -jar ui2swt.jar -v net/sourceforge/MyMainWindow.uiGenerates the following output:
Translating UI file... input file: net/sourceforge/MyMainWindow.ui output file: MyMainWindow.java package name: net.sourceforge UI version: 4
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(); |
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:
ToolItem: |
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() |
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 ... } };
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:
last updated: 11 August 2007