corner imagecorner image
IDEPlatformPluginsDocs & SupportCommunityPartners

Working with Qt Applications - NetBeans IDE 6.7/6.8 Tutorial

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.

Contents

Content on this page applies to NetBeans IDE 6.7 and 6.8

Requirements

To follow this tutorial, you need the following software.

Software Version Required
NetBeans IDE (including C/C++ support) Version 6.7 or 6.8
Java Development Kit (JDK) Version 6 or version 5
GNU C/C++ compilers (GCC) Versions supported by NetBeans IDE.
On Windows, MinGW is required. There are issues with qmake-generated Makefile under Cygwin.
Qt Libraries Version 4.4, 4.5, or 4.6.1

See the NetBeans IDE 6.8 Installation Instructions for information on downloading and installing the required NetBeans software.

See Configuring the NetBeans IDE for C/C++/Fortran for instructions on installing and configuring the compilers.

Introduction

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.

Installing the Qt Libraries

You can download the Qt libraries for your Windows, Linux, or Mac OS X platform from www.qtsoftware.com/downloads. Use the Qt Installation Instructions to install.

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.

Solaris Binaries for Qt are not available for the Solaris platform. However, it is possible to build the Qt libraries from sources that you can get from the official Qt source code repository.
Other helpful links for building Qt from source are Git Installation and Get The Source.
Windows 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.

  1. First, create a new project. Choose C/C++ Qt Application in the New Project dialog and click Next >.

    New Qt project dialog

  2. In the next dialog change project name and location if needed. Check Create Main File and click Finish.
  3. New Qt project dialog

    Our newly created project looks like this:

    Qt project and main.cpp file

  4. 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.
  5. Qt project properties dialog

  6. Next we'll create a dialog. Right-click on Resource Files and select New->Qt Form...
  7. Adding resource files to the Qt project

  8. 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.
  9. Qt new form

    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).

    Qt Designer automatically opens

  10. 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.
  11. Qt Designer with form your created

  12. When you are finished creating the form, close Qt Designer. The project looks as follows:
  13. Hello form source files

    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.

  14. Now open main.cpp and insert two lines of code responsible for creation and displaying of HelloForm. Don't forget to include HelloForm.h.
  15. Editing the Hello form source files

  16. 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.

  17. We need to define a slot and connect it to textChanged signal fired by the text field. To learn more about Qt signals and slots read http://doc.qtsoftware.com/4.5/signalsandslots.html

  18. Go to HelloForm.h and declare this slot:
  19. Editing the Hello form include file

  20. Then go to HelloForm.cpp and insert the slot definition:
  21. Adding slot to form source files

  22. And finally connect the signal with the slot by inserting some code into HelloForm constructor:
  23. Adding slot to Hello form constructor

  24. Now run the application and have some fun!
  25. Qt Hello World app running

Conclusion

Please create and develop Qt applications with NetBeans and send your feedback to the CND mailing list .

Found a bug or want to suggest an enhancement? Please file them at http://netbeans.org/bugzilla/enter_bug.cgi?component=cnd (netbeans.org registration is required)