Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How to initialized QApplication object.

LegacyForum
LegacyForum Posts: 1,664 ✭✭
edited December 2016 in C/C++test
Qt auto generated test case do not play
The ability to unit test with QT headers and libraries is unique. Because without a correctly constructed QApplication object. The automatic test cases generated by C++Test may not run during test case execution, thereby showing no execution results.

In order to properly test a QT application the user must create and link with an object file that has successfully initialized the QApplication object.

How to create an initialized QApplication object, and link the object file into your C++Test project ( Linux/Solaris).


1. cd into the QT examples directory

2. cd into the Qt ~examples/mainwindows/application directory

3. verify that you can compile main.cpp file using the following command

g++ -c -pipe -O2 -Wall -W -D_REENTRANT -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../mkspecs/linux-g++ -I. -I../../../include/QtCore -I../../../include/QtCore -I../../../include/QtGui -I../../../include/QtGui -I../../../include -I.moc/release-shared -I.uic/release-shared -g main.cpp


4. Place the link_obj.cpp file (below) into the ~examples/mainwindows/application directory.

------------------------------------cut-----------------------------------------
//link_obj.cpp
#include <QApplication>

int _qapp_argc = 1;

char* _qapp_argv[] = {"myapp"};

QApplication _qapp(/*argc=*/_qapp_argc, /*argv=*/_qapp_argv, /*GUIenabled=*/true);

------------------------------------cut----------------------------------------

5. If you can successfully compile the main.cpp file, use the same command line to compile the link_obj.cpp file.

g++ -c -pipe -O2 -Wall -W -D_REENTRANT -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../mkspecs/linux-g++ -I. -I../../../include/QtCore -I../../../include/QtCore -I../../../include/QtGui -I../../../include/QtGui -I../../../include -I.moc/release-shared -I.uic/release-shared -g link_obj.cpp

6. You now have an initialized QApplication object file that you will be able to link into your C++Test project.


7. Open the C++Test Project and select the Linker Tab.

8. Click on the Add Library button, and navigate to the directory where you just created the link_obj.o file.

9. Click on the link.o file, adding it to the C++Test project.

10 Click on the Apply Button, then close the Project Configuration dialog.

Note: The above steps will also work on a Windows.
Tagged: