by Igor Nikiforov, August 2012 (updated by Dmitry Zharkov, June 2016)
Qt is a framework for creating Web applications that can be deployed on multiple platforms, including desktop systems and mobile devices. The Qt SDK includes the libraries, some tools, and an IDE called Qt Creator. However, you can use the Oracle Developer Studio IDE instead to create Qt applications if you have the Qt SDK and GNU compilers. The Qt version must be at least Qt4.
The Oracle Developer Studio IDE supports creating, building, running, and debugging of Qt projects without leaving the IDE. Qt tools such as qmake
, moc
, and uic
are launched automatically, as needed. You don't need to think (or probably even know) about them.
Before you create Qt applications, you must install the Qt4 SDK.
To use Qt in the Oracle Developer Studio IDE on Linux, be aware of the following:
gcc
packages from the Linux software repository.qmake -v
in a terminal should print Qt version information rather than an error message. If qmake
is not found, add /your-Qt-installation-dir/bin
to your PATH
environment variable. The path to qmake
should be something similar to /home/user/QtSDK/Desktop/Qt/474/gcc/bin/
if you download the Qt SDK.To use Qt in the Oracle Developer Studio IDE on Oracle Solaris, be aware of the following:
qmake -v
in a terminal should print Qt version information rather than an error message. If qmake
is not found, add /your-Qt-installation-dir/bin
to your PATH
environment variable.In this tutorial, you create a simple "Hello World" Qt application. When you are finished, the application will be similar to the Hello Qt World sample, which you can find in the IDE project samples.
Figure 1. New Project Wizard with C/C++ Qt Application Selected
If you are using Linux, set Tool Collection to GNU, and click Finish.
Note: In the Figure 2, Tool Collection is set to Oracle Developer Studio. This tool collection will work only if you are using Qt software that you built on Oracle Solaris with the Oracle Developer Studio compiler.
Figure 2. Project Name and Location Step
The newly created Qt_Application project opens in the IDE and looks like Figure 3:
Figure 3. Open Qt Application Project
Figure 4. Qt Application Project's Properties
Advanced users can tweak many things in the Qt project properties, but for now you should leave everything as is.
Note: If you are developing on Oracle Solaris with Oracle Developer Studio compilers, make sure that the Qmake Spec option in the Expert section of the properties contains the value solaris-cc
.
Figure 5. Context Menu for Creating a New Qt Form
HelloForm
in the Form Name field, and select Dialog without Buttons from the Form Type list. Select Create C++ wrapper class, and click Finish. Figure 6. New Qt Form Dialog Box with Selections
Three files are created (HelloForm.ui
, HelloForm.cpp
, HelloForm.h
), and the IDE automatically opens Qt Designer for you to edit the form HelloForm.ui
.
Figure 7. Qt Designer Window with a Blank New Form
You should place two QLineEdit widgets in the form, name the first widget nameEdit
, and name the second widget helloEdit
.
Figure 8. Qt Designer with Form You Created
The project looks like Figure 9:
Figure 9. New Project with Files
All the newly created HelloForm
files are placed in the same Resource Files logical folder. If you prefer to have .cpp
files in the Source Files folder and .h
files in the Header Files folder, just drag and drop them to the desired logical folder.
A question mark on the QtApplication icon in the Projects tab indicates the project has broken #include
directives. The line #include "HelloForm.h"
is highlighted to show where the problem occurs.
The broken #include
directive is in the HelloForm.h
file: #include ui_HelloForm.h
. Indeed, there is no ui_HelloForm.h
file yet. The ui_HelloForm.h
include file will be generated with the first build of the project. This is not really an error; it is how the Qt build system works. Just click the Build Main Project button on the toolbar, and the error should disappear.
main.cpp
and insert two lines of code to create and display HelloForm.In Figure 10, the new lines you need are shown in lines 17 and 18:
HelloForm form; form.show();
Don't forget to include HelloForm.h
, as shown in line 9.
Figure 10. Code Added to main.cpp
File
You should find that you can type anything in the text field, but nothing happens when you do. To see something happen, you add to the application to have it respond with a greeting message that includes the name entered in the text field.
To do this, you need to define a slot and connect it to a textChanged
signal fired by the text field.
To learn more about Qt signals and slots, see Signals and Slots.
HelloForm.h
and declare this slot, as shown in line 18 in Figure 11.Figure 11. Slot Declaration in HelloForm.h
File
HelloForm.cpp
and insert the slot definition, as shown at line 17 in the Figure 12.Figure 12. Code for Slot Definition in HelloForm.cpp
File
HelloForm
constructor.In Figure 13, the code is inserted at lines 12 through 13 of HelloForm.cpp
.
Figure 13. Code Added to HelloForm.cpp
to Connect the textChanged
Signal
Figure 14. Application Response
See the following locations for more information:
Igor Nikiforov has been working for Oracle and Sun Microsystems for more than four years. His current responsibilities include development of the Oracle Developer Studio IDE and NetBeans. Before Oracle, Igor developed enterprise applications including some municipal IT projects for the city of Saint Petersburg, Russia.
Revision 1.1, June 2016 |
Revision 1.0, August 2012 |