C / C++ / C# Tutorials



Netbeans Qt GUI Tutorials

Working with Qt Applications - NetBeans IDE 

To follow this tutorial, you need the following software.
SoftwareVersion Required
NetBeans IDE (including C/C++ support)Version 6.9
Java Development Kit (JDK)Version 6
GNU C/C++ compilers (GCC)Versions supported by NetBeans IDE.
On Windows, MinGW is required and is bundled with the Qt SDK.
Qt LibrariesVersion 4.6.2

See the NetBeans IDE 6.9 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.

Installing the Qt Software

When using the NetBeans IDE with Qt, you do not need the full Qt SDK on non-Windows platforms. You can install your compilers as described in Configuring the NetBeans IDE for C/C++/Fortran, and then you can download the Qt libraries and tools, also known as the Qt framework.
On Windows, however, you should install the Qt SDK to avoid potential problems.

Setting Up Qt for NetBeans IDE on Windows

The Qt 4.6.2 SDK includes the MinGW environment and gcc 4.4 compiler. This bundled MinGW works best with Qt, so you should use it instead of another MinGW version or Cygwin.
  1. Install NetBeans IDE and the C/C++ plugin module, as explained in Configuring the NetBeans IDE for C/C++/Fortran, but do not install Cygwin or MinGW as described in that document.
  2. Download and install MSYS-1.0.10.exe in the default location.
  3. Download and install the Qt 4.6.2 SDK which includes MinGW and gcc. If Qt Creator starts automatically, you can close it.
  4. In the NetBeans IDE, select Tools > Options > C/C++ to open the C/C++ properties.
  5. Click Add in the Tool Collections area.
  6. Specify the Qt MinGW installation's bin directory as the tool collection's Base Directory (for example, c:\Qt\2010.02.1\mingw\bin)
  7. Set the Tool Collection Name to MinGW_Qt and click OK, then click OK in the Options dialog box.
  8. Proceed to Creating a Simple Qt Application in this article.

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

You can download the full Qt SDK, or just the Qt framework for your Linux or Mac OS X platform from http://qt.nokia.com/downloads.
You should install your compilers separately. Compilers are not included in the Qt SDK as they are on Windows.
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 path to qmake should be something similar to /home/user/qtsdk-2010.02/qt/bin if you download the SDK.
When the Qt tools are available from the command line, proceed to Creating a Simple Qt Application in this article.

Setting Up Qt for NetBeans IDE on Solaris Platforms

Binaries for Qt are not available for Solaris platforms. 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.


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.
    New Qt project dialogOur newly created project looks like this:
    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 we will leave everything as is.
    Qt project properties dialog
  4. Next we'll create a dialog. Right-click on Resource Files and select New->Qt Form...
    Adding resource files to the Qt project
  5. 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 formThree files are created (HelloForm.uiHelloForm.cppHelloForm.h), and NetBeans automatically opens Qt Designer for you to edit the new form (HelloForm.ui).
    Qt Designer automatically opens
  6. 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.
    Qt Designer with form your created
  7. When you are finished creating the form, close Qt Designer. The project looks as follows:
    Hello form source filesAll 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.
  8. Now open main.cpp and insert two lines of code responsible for creation and displaying of HelloForm. Don't forget to include HelloForm.h.
    Editing the Hello form source files
  9. 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.
    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.qt.nokia.com/4.6/signalsandslots.html
  10. Go to HelloForm.h and declare this slot:
    Editing the Hello form include file
  11. Then go to HelloForm.cpp and insert the slot definition:
    Adding slot to form source files
  12. And finally connect the signal with the slot by inserting some code into HelloForm constructor:
    Adding slot to Hello form constructor
  13. Now run the application and have some fun!
    Qt Hello World app running

No comments:

Post a Comment