Working with Qt Applications - NetBeans IDE Tutorial

This tutorial guides you through creating a simple Qt application project in NetBeans IDE. The Qt toolkit is an open source cross-platform application development framework.

Contents

Content on this page applies to NetBeans IDE 7.3 and 7.4

Requirements

To follow this tutorial, you need the following software.

Software Version Required
NetBeans IDE (including C/C++ support) 7.3 or 7.4 with NetBeans C/C++ plugin
Java Development Kit (JDK) Version 7
GNU C/C++ compilers (GCC) Versions supported by NetBeans IDE.
On Windows, MinGW is required.
Qt Libraries Version 4.8
Note that Qt 5 does not work with this tutorial

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

Introduction

The NetBeans 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 (and probably even know) about them. This article shows how to configure NetBeans IDE to use Qt 4.8 and create a simple Qt application.

Installing the Qt Software for Use in NetBeans IDE

You must install the Qt software and configure NetBeans to use it before creating Qt applications in the IDE.

Important: You must use Qt 4.8 to follow this tutorial.

If you want to use Qt 5 see this contributed article:
How To Setup 64-Bit Qt 5 With NetBeans 7.4 On Windows

See the instructions for setting up Qt for your platform:

Setting Up Qt 4.8 for NetBeans IDE on Windows

These instructions describe the software that was installed on Windows to create this tutorial.

Download Required Software for Windows

  1. Install NetBeans IDE and the C/C++ plugin module, as explained in Configuring the NetBeans IDE for C/C++/Fortran. Do not install compilers yet.
  2. Exit NetBeans IDE.
  3. Download the MinGW installer from http://sourceforge.net/projects/mingw.
  4. Run the MinGW installer, which should have a file name similar to mingw-get-inst-20120426.exe.
  5. In the MinGW installer, select the following components to install:
    • C
    • C++
    • MSYS Basic System
    • MinGW Developer Toolkit
    The installer takes a few moments to download and extract the software packages. By default, MinGW is installed in C:\MinGW.
  6. Download and install the following Qt software from http://qt-project.org/downloads.
    • Qt libraries 4.8.5 for Windows (minGW 4.4 317 MB)
    • Qt Creator 2.8.1 for Windows (51 MB)

Edit Your Windows Path

You must edit your PATH environment variable in Windows to add the paths to the binaries for Qt, MinGW, and MSYS. If you installed to the default locations, the paths are:

  • C:\Qt\4.8.5\bin
  • C:\MinGW\bin
  • C:\MinGW\MSYS\1.0\bin

To add the paths to your Path environment variable:

  1. Open the Environment Variables window:
    • On Windows XP and Windows 2000: Right-click My Computer > Properties > Advanced tab > Environment Variables button.
    • On Windows Vista and Windows 7: Right-click My Computer > Properties > Advanced System Settings link > Environment Variables button.
  2. In the Environment Variables window, select the Path variable in the Systems Variable section and click Edit.
  3. At the end of the path, insert a semi-colon and add the paths to the executables for Qt, MinGW, and MSYS. Use semi-colons between the paths and do not use any spaces. Be careful not to remove anything already on your PATH or your computer might not work correctly.

    When you are finished, your path should look similar to the following:

    %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;
    %SYSTEMROOT%\System32\WindowsPowerShell\v1.0;C:\MinGW\bin;C:\MinGW\MSYS\1.0\bin;C:\Qt\4.8.5\bin
  4. Click OK in the Environment Variables window.

Create a Tool Collection for MinGW and Qt

You need to create a new tool collection that uses MinGW compilers, the MSYS commands, and Qt commands.

To create a new tool collection:

  1. Start NetBeans IDE and open the Services window by choosing Window > Services.
  2. Expand the C/C++ Build Hosts node and the localhost node, and right click the Tool Collections node and select Add New Tool Collection.

    Screenshot of Add New Tool Collection option
  3. Specify the MinGW installation's bin directory as the tool collection's Base Directory (for example, C:\mingw\bin).
    The Tool Collection Family and Tool Collection Name should be automatically filled in as MinGW.
  4. Change the Tool Collection Name to MinGW_Qt and click OK.
  5. Right-click the new MinGW_Qt tool collection under the Tool Collection node in the Services window and choose Properties.
    The paths to the tools should look similar to the following figure.
    Screenshot showing MinGW tool paths
  6. Check the paths for the Make Command and QMake Command and correct them if needed.
    The Make Command should be C:\MinGW\MSYS\1.0\bin\make.exe command, and the QMake Command command should be C:\Qt\4.8.5\bin\qmake.exe.
  7. Click the Default button while MinGW_Qt is selected to make it the default tool collection.
  8. Proceed to Creating a Simple Qt Application in this article.

Setting Up Qt for NetBeans IDE on Linux or Mac OS X Platforms

  1. Download the Qt libraries for your Linux or Mac OS X platform from http://qt-project.org/downloads or get them from your Linux package repository.
  2. If compilers are not already present on your system, install them as explained in Configuring the NetBeans IDE for C/C++/Fortran.
  3. Add the paths to the compilers and Qt tools to your PATH environment variable
  4. In a terminal window, type qmake -v and gcc --version to make sure the commands are in your path.
  5. When the tools are available from the command line, start NetBeans IDE, and check the Tool Collections properties to make sure the paths to the compilers, make command, and qmake command are correct.
  6. Proceed to Creating a Simple Qt Application in this article.

Setting Up Qt for NetBeans IDE on Oracle Solaris Platforms

Binaries for Qt are not available for Oracle Solaris platforms. However, it is possible to build Qt from source and then use it with NetBeans IDE.

  1. Install your compilers as explained in Configuring the NetBeans IDE for C/C++/Fortran.
  2. Download the Qt source from ftp://ftp.qt-project.org/qt/source/
  3. Refer to the Qt documentation Installing Qt for X11 Platforms for instructions for building.
  4. After you successfully build Qt binaries, edit your PATH environment variable to add the path to the Qt binaries.
  5. In a terminal window, type qmake -v and gcc --version to make sure the commands are on your path.
  6. When the GNU compilers and Qt tools are available from your command line, start NetBeans IDE.
  7. Check the Tool Collections properties to make sure the paths to the compilers and qmake command are correct.
  8. Proceed to Creating a Simple Qt Application in this article.

Creating a Simple Qt Application

In this tutorial you 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.

These instructions are written for Windows platforms. Users on Linux, Mac, and Solaris platforms can perform the same steps using a GNU tool collection where the path to qmake is correctly specified.

  1. First, create a new project. Choose C/C++ Qt Application in the New Project dialog and click Next.
    New project dialog
  2. In the next dialog change project name and location if needed.

    Check Create Main File and select the MinGW_qt tool collection, then click Finish.

    New Qt project dialog

    Your newly created project should look similar to the following figure:

    Qt project and main.cpp file
  3. 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 for this tutorial you should leave everything as it is.
    Qt project properties dialog
  4. Click the Run category and set the Console Type property to External Terminal, then click OK.
  5. Next you can create a form. Right-click on Resource Files and select New->Qt Form
    Adding resource files to the Qt project
  6. 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.
    Qt new form

    Three files are created (HelloForm.ui, HelloForm.cpp, HelloForm.h), and the IDE automatically opens Qt Designer for you to edit the new form, HelloForm.ui.

    Qt Designer automatically opens
  7. Use your GUI skills to create a form similar to that shown below. Add to the form two QLineEdit widgets and change the name of the first widget to nameEdit, and change the name of the second widget to helloEdit.
    Qt Designer with form your created
  8. When you are finished creating the form, save it and close Qt Designer. The project looks as shown in the following figure.
    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.

    You should see a small question mark in the Projects tab to indicate the presence of broken #include directives. The setupUi underlined with red is a consequence of the broken #include ui_HelloForm.h directive, which occurs in the HelloForm.h file. The ui_HelloForm.h include file does not actually exist yet and it will be generated with the first build of the project. This is how the Qt build system works.

  9. Click the Build Project button on the toolbar and the error should disappear when the ui_HelloForm.h file is generated.
  10. Open main.cpp and insert two lines of code that are responsible for creation and displaying of HelloForm as shown in lines 17 and 18 in the figure below. Don't forget to include HelloForm.h as shown in line 9.
    Editing the Hello form source files
  11. Click the Run button in the tool bar to run the application.
    First you see a terminal window and then the dialog box that you created in Qt Designer.
  12. Type something in the text field and press Enter, and you should see that nothing happens.

    To enable the application to respond, you must define a slot and connect it to a textChanged signal fired by the text field. To learn more about Qt signals and slots read http://doc.qt.digia.com/4.6/signalsandslots.html

  13. Go to HelloForm.h and declare this slot as shown in lines 18 and 19 in the following figure:
    Editing the Hello form include file
  14. Then go to HelloForm.cpp and insert the slot definition as shown in lines 17 through 23 in the following figure:
    Adding slot to form source files
  15. Finally connect the signal with the slot by inserting some code into HelloForm constructor as shown in lines 12 and 13 in the following figure:
    Adding slot to Hello form constructor
  16. Now run the application and when the dialog is displayed, click the top input field, type your name, and the bottom field will echo Hello to you as shown below.
    Qt Hello World app running

Conclusion

This article showed how to set up your environment so you can use NetBeans IDE with Qt libraries and tools.

Please create and develop Qt applications with NetBeans and send your feedback to the and NetBeans C/C++ User's forum.

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

See Also

Please see the C/C++ Learning Trail for more articles about developing with C/C++/Fortran in NetBeans IDE.


get support for the NetBeans

Support


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2015, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo