Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

[Prev: Creating Database Applications] [Home] [Next: Reference: Key Bindings]

Customizing and Integrating Qt Designer

Customizing Qt Designer

Qt Designer can be customized in two ways: you can add custom widgets, and you can change aspects of how Qt Designer works. Custom widgets are covered in Creating Custom Widgets. This section will focus on customizing Qt Designer itself.

Qt Designer's toolbars are all dockable so they can be dragged by their toolbar handles and arranged how you like. The Files, Object Hierarchy, Property Editor and Output Windows are also dockable so you can also drag them to the positions that you prefer. You can also make them into floating windows by dragging them outside Qt Designer's dock areas.

General preferences can be set by clicking Edit|Preferences to invoke the Preferences dialog. If you check the 'Restore Last Workspace on Startup' checkbox then Qt Designer will remember the sizes and positions of the toolbars and the dockable windows. You can change Qt Designer's main window background either by selecting a color or a pixmap. You can also switch off the grid (uncheck Show Grid) since using layouts makes the grid redundant.

The Preferences dialog may have additional tabs, depending on what plugins you have installed. We'll describe the C++ Editor tab since this is installed by default.

The C++ Editor tab is used to set your preferred fonts for syntax highlighting in Qt Designer's code editor. The base font for all elements is set in the 'Standard' element which is the last item in the list. If you want one font to be used throughout then set the 'Standard' font and all the other elements will inherit its setting.

Qt Designer's Code Editor

The code editor is available if an Editor plugin is installed. The C++ Editor plugin is installed by default.

The code editor provides the following keystrokes:

To select (mark) text hold down the Shift key whilst pressing one of the movement keystrokes, for example, Shift+Right Arrow will select the character to the right, and Shift+Ctrl+Right Arrow will select the word to the right, etc.

Pressing Alt+I starts incremental search. The characters you type will appear in the Incremental Search line edit in the Search toolbar and the cursor will be moved to the first matching text in the editor. As you type the search will continue. Press Return to move to the next match and press Esc to cancel the search at the position you've reached.

Pressing Tab after you've typed one or more characters invokes completion. Completion works like this: start typing some text then press Tab. If the editor can find another item of text that begins with the same characters it will complete your text for you; if it finds more than one possibility it will pop up a list of choices. You can use the arrow keys to choose a piece of text then press Return, or press Esc to continue typing. You can switch off completion in the Preferences dialog.

When you enter -> or . the editor will pop up a command completion list; use the arrow keys to move to the item you want and press Return, or press Esc to ignore the list.

Creating and Using Templates

Qt Designer supports two approaches to creating template forms. The simplest approach involves little more than saving a .ui file into the templates directory. The second approach involves creating a container widget class to be used as a base class for forms that use the template. We will explain both techniques.

Simple Templates

These templates are most useful when you want to create a whole set of forms which all have some common widgets. For example, you might have a project that will require many forms, all of which need to be branded with a company name and logo.

First we'll create the simple template.

  1. Click File|New to invoke the New File dialog. Click the Dialog template then click OK.

  2. Click the Text Label toolbar button, then click near the top left of the form. Change the font Point Size property to 16 and change the text property to your or your company's name. Click the Line toolbar button, then click the form below the label; click Horizontal on the pop-up menu.

  3. Select the label and the line. (Ctrl+Click the form, then drag the rubber band so that it touches or includes the line and the label.) Press Ctrl+L to lay them out vertically.

  4. Click the Save toolbar button. In the Save As dialog, navigate to Qt Designer's templates directory, e.g. (qt/tools/designer/templates. Type in the name 'Simple_Dialog.ui' and click Save.

  5. Right click the form in the Forms list, then click Remove form from project.

Now that we have the simple template we are ready to use it. Click File|New to invoke the New File dialog. One of the templates that will appear is 'Simple Dialog'. Click the simple dialog, then click OK. A new form will appear with the same widgets and layout as the template. Add any other widgets and functionality. When you attempt to save the form you will be prompted for a new form name.

Base-class Templates

These templates are useful when you want to provide some default functionality that all the forms based on the base class can inherit. In our example we'll use a class called SizeAware that remembers and restores its size as the basis of a template. We won't describe the class itself, but will focus instead on making use of it as a Qt Designer template. The source for the class is in qt/tools/designer/examples/sizeaware.

The template can either be based on a custom widget or on any existing container widget.

If you want to base the template on a custom widget you must first add it to Qt Designer's custom widgets. Click Tools|Custom|Edit Custom Widgets to invoke the Edit Custom Widgets dialog. (This dialog is explained in more detail in Simple Custom Widgets.) Click New Widget. Change the Class from 'MyCustomWidget' to 'SizeAware'. Click the Headerfile ellipsis button and select the file qt/tools/designer/examples/sizeaware/sizeaware.h. Check the Container Widget checkbox. This class provides two properties. Click the Properties tab. Click New Property and change the property name to 'company'. Click the New Property again and change the property name to 'settingsFile'. Click Close.

To create a template, based on an existing widget or on your own custom widget, click File|Create Template to invoke the Create Template dialog. Change the Template Name to 'SizeAware' and click the SizeAware base class, then click Create. The dialog will create the template and close itself immediately. Close Qt Designer and restart it.

A new template, 'SizeAware' is now available from the list of templates. Click File|New, click SizeAware and click OK. Note that the two properties, company and settingsFile, are available in the Properties window. Any forms based on this template will remember their size and resize when reloaded. (In practical applications having one settingsFile per form is not recommended, so this template would only really be useful for applications that have a single main window.)

Integrating Qt Designer with Visual Studio

Qt Designer can be integrated into Visual Studio 6.0 using the qmsdev.dll file that is supplied with Qt. Visual Studio.Net does not support this integration.

Start up Visual Studio and click File|Open Workspace. Open %QTDIR%\tools\designer\integration\qmsdev\qmsdev.dsp. Click Build|Set Active Configuration and in the list click 'QMsDev - Win32 Release', then click OK. Now click Build|Build qmsdev.dll. You should now make sure Visual Studio is closed and copy the file %QTDIR%\tools\designer\integration\qmsdev\Release\qmsdev.dll into Microsoft Visual Studio\Common\MSDev98\AddIns. Now click Tools|Customize. Click the Add-in Macro Files tab, then click the Browse button. Change the file type to 'Add-ins (.dll)' and navigate to Microsoft Visual Studio\Common\MSDev98\AddIns. Click the qmsdev.dll file, click Open, then click Close.

A new toolbar will appear in Visual Studio with the following toolbar buttons:

Double clicking a .ui file in the workspace overview will now launch Qt Designer.

If you create a .cpp file which contains the Q_OBJECT macro you will need an additional file which is generated by the moc to be included in your project. For example, if you have 'file.cpp', then the last line would be #include "file.moc" and the additional file would be called 'file.moc'. To ensure that Visual Studio executes the moc and generates this file you must create a custom dependency. Double click the .cpp file (in your project workspace) that contains the Q_OBJECT macro. Click the Add MOC toolbar button; this will create an empty .moc file in your project workspace. Right click the newly created .moc file, then click Settings from the pop-up menu to invoke the Project Settings dialog. Click the Custom Build tab. Click the Dependencies button to pop up the User Defined Dependencies dialog. Type in $(InputDir)\$(InputPath), then press Return. Click OK to leave the Dependencies dialog, then click OK to leave the Project Settings dialog.

If you wish to delete the add-in remove it from the toolbar then delete the qmsdev.dll file from the add-ins directory.

Creating Makefiles without qmake

The qmake tool provided with Qt can create Makefiles appropriate to your platform based on .pro project files. This section describes the dependencies involved in building a Qt application and gives a couple of simple example Makefiles. This section assumes that you have a good understanding of Makefiles.

Qt Designer produces .ui files which are used to generate .h and .cpp files for the compiler to compile. The .ui files are processed by uic. Classes which inherit from QObject, e.g. those which use slots and signals, require an additional .cpp file to be generated. These files are generated by the moc and are named 'moc_file.cpp' where the original .cpp file is called 'file.cpp'. If your .cpp file contains the Q_OBJECT macro an additional file 'file.moc' should be generated which must be #included in the .cpp, normally at the end. This requires an extra dependency being created.

Processing .ui files with uic is done twice:

uic myform.ui -o myform.h
uic myform.ui -i myform.h -o myform.cpp

The first execution creates the header file, the second creates the .cpp file. If you wish to subclass a form you can use uic to generate subclass skeletons:

uic formbase.ui -o formbase.h
uic formbase.ui -i formbase.h -o formbase.cpp
uic -subdecl Form formbase.h formbase.ui -o form.h
uic -subimpl Form form.h formbase.ui -o form.cpp

First we generate the header and implementation file for our base class. Then we generate the header and implementation skeletons for our subclass. Note that the use of uic to generate skeletons is not something that would be done in a Makefile, we mention it here because it can be useful for command line users. Note also that the command line for -subdecl and for -subimpl are subtly different.

For implementation files that contain classes which inherit from QObject we must create moc files:

moc myform.h -o moc_myform.cpp

We'll look at a simple Makefile to see the dependencies in practice.

myapp: moc_myform.o myform.o main.o
        g++ -lqt -o myapp moc_myform.o myform.o main.o

main.o: main.cpp
        g++ -o main.o main.cpp

moc_myform.o: moc_myform.cpp
        g++ -o moc_myform.o moc_myform.cpp

moc_myform.cpp: myform.h
        moc myform.h -o moc_myform.cpp

myform.o: myform.cpp
        g++ -o myform.o myform.cpp

myform.cpp: myform.h myform.ui
        uic myform.ui -i myform.h -o myform.cpp

myform.h: myform.ui
        uic myform.ui -o myform.h

Note that you may need to include the full path to the commands in your Makefile, and under Windows the filenames are moc.exe and uic.exe.

In Unix/Linux environments the make command may be able to do more for us, so we should be able to use a simpler Makefile like this:

myapp: moc_myform.o myform.o main.o
        g++ -lq -o $@ $^

%.o: %.cpp
        g++ -o $^ $@

moc_%.cpp: %.h
        moc $^ -o $@

myform.cpp: myform.h myform.ui
        uic myform.ui -i myform.h -o myform.cpp

myform.h: myform.ui
        uic myform.ui -o myform.h

To see more sophisticated Makefiles simply generate them using qmake on any of your Qt projects or any of the examples supplied with Qt.

Importing Foreign File Formats

To import a file in a supported foreign file format click File|Open, then click the File Type combobox to choose the file type you wish to load. Click the required file and Qt Designer will convert and load the file.

The filters that Qt Designer uses to read foreign file formats are 'works in progress'. You may have different filters available in your version of Qt Designer than those described here. The easiest way to see which filters are available is to invoke the file open dialog; all your filters are listed in the File Type combobox.

Importing Qt Architect Files

Qt Architect is a free GUI builder for Qt written by Jeff Harris and Klaus Ebner. The .dlg extension is associated with Qt Architect dialog files.

Qt Designer can read files generated by Qt Architect version 2.1 and above. When given a .dlg file from a previous version of Qt Architect, Qt Designer tells you how to convert it to the file format of version 2.1. (The conversion procedure varies depending on the version of the .dlg file.)

The import filter does a good job of importing .dlg files; the result is almost identical to what you get in Qt Architect. However, the C++ code that uses the dialogs will probably need some adaptation.

There are a few drawbacks to converting Qt Architect files to Qt Designer's format due to differences between the two tools; these are listed below:

Importing Glade Files

Glade is a free GUI builder for GTK+ and GNOME written by Damon Chaplin. The .glade extension is associated with Glade files.

Qt Designer has been tested with Glade files up to version 0.6.0 and might work with later versions as well.

Although Glade does not target Qt, the layout system and the widget set of GTK+ are similar to those of Qt, so the filter will retain most of the information in the .glade file.

There are some considerations regarding the conversion of Glade files, as listed below:

[Prev: Creating Database Applications] [Home] [Next: Reference: Key Bindings]


Copyright © 2003 TrolltechTrademarks
Qt version 3.2.0b2