Working with Qt Applications - NetBeans IDE 6.7/6.8 Tutorial
Contributed by
January 2010 [Revision number: V6.8-2]
This tutorial guides you through creating a Qt application project in NetBeans IDE 6.7/6.8. The Qt toolkit
is an open source cross-platform application development framework.
NetBeans IDE 6.7 with the C/C++ module introduced the long-awaited Qt integration. The
NetBeans IDE now 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 (and probably even know) about them.
You do not need to download the Qt SDK, only Qt libraries and tools, which
the Qt download page calls the Qt Framework. Use the links labeled Download Qt 4.6.1 Libraries for
<platform> on the Qt download page.
After you install the Qt packages, make sure that Qt tools are
available from the command line.
Typing 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 following table provides some platform-specific notes.
You cannot use Cygwin because there are issues with qmake-generated Makefile under Cygwin.
You can only use MinGW compilers and MSYS environment when using Qt with NetBeans IDE.
The Qt Libraries for Windows include MinGW, so you do not need to download MinGW separately. However,
you do need to install MSYS. See the MinGW Compilers and Tools section
of the Configuring the NetBeans IDE
document for MSYS information.
Mac OS X
There is an issue with the PATH being set correctly
for Qt to work in NetBeans. For workarounds, please see information about this issue on the
NetBeans
CND users forum.
Creating a Simple Qt Application
In this tutorial we'll create a simple "Hello World" Qt application, similar to
the Hello Qt World sample, which you can find in
Samples->C/C++->Hello Qt World.
First, create a new project. Choose C/C++ Qt Application in
the New Project dialog and click Next >.
In the next dialog change project name and location if needed.
Check Create Main File and click Finish.
Our newly created project looks like this:
Right-click the project node and select Properties to open the Project Properties dialog. Click the Qt category.
Advanced users can tweak many things in the Qt project properties, but we will leave everything as is.
Next we'll create a dialog. Right-click on Resource Files and
select New->Qt Form...
In the New Qt Form dialog, type HelloForm as the Form Name, and select Dialog without Buttons as the
Form Type. Check Create C++ wrapper class, and click Finish.
Three files are created (HelloForm.ui, HelloForm.cpp,
HelloForm.h), and NetBeans automatically opens Qt Designer for you to
edit the new form (HelloForm.ui).
Use your GUI skills to create a form similar to that shown below. The form should
contain two QLineEdit widgets. The first widget should be named
nameEdit, and the second widget should be named helloEdit.
When you are finished creating the form, close Qt Designer. The project looks as follows:
All the newly created HelloForm files are placed in the same Resource Files
logical folder. If you prefer to have CPP files in Source Files and H
files in Header Files — just drag and drop them to the desired logical
folder.
There is a small question mark in the Projects tab indicating
broken #include directives. The setupUi underlined with red is a
consequence of this fact.
The broken #include directive is in HelloForm.h: #include ui_HelloForm.h.
Indeed, there is no ui_HelloForm.h yet. The include file will be
generated with the first build of the project. This is how the Qt build
system works. Just click on the Build Main Project button on the
toolbar, and the error should disappear.
Now open main.cpp and insert two lines of code responsible for
creation and displaying of HelloForm. Don't forget to include
HelloForm.h.
Run the application and see how it displays the window that you
created in Qt Designer.
Anything can be typed in the text field, but
nothing happens. Let's make our application show a greeting message that
includes the name entered in the text field.