Thursday, July 28, 2011

Redirect QTest Output To Log File

QtTest classes are created by subclassing QObject class and adding private slots into derived QObject class. Every private slot is treated as a testfunction and executed by QTest::qExec() function.
Sample project started as a QT Console Application and contains main.cpp, TestString.h and TestString.cpp files.
Project Directory Structure in Qt Creator IDE : is the project configuration file and contains:

QT += testlib core
TARGET = LogQTestOutputToFile
SOURCES += main.cpp \
HEADERS += TestString.h

QT += testlib

line which is used to include testlib dependencies into project.

TestString.h file contains private slots which are aimed to test mainly toUpper() behavior of String class.

#include <QtTest/QtTest>
#include <QString>
#include <QObject>

class TestString: public QObject
private slots:
void initTestCase();
void toUpper();
void cleanupTestCase();

initTestCase() and cleanupTestCase() private slots are executed by the testing framework and used to initialize and release resources; respectively.

TestString.cpp file contains implementation details for the private slots declared in TestString.h file.

#include "TestString.h"

void TestString::initTestCase()
qDebug("called before everything else, initialize your resources");

void TestString::toUpper()
QString str = "Hello";
QCOMPARE(str.toUpper(), QString("HELLO"));

void TestString::cleanupTestCase()
qDebug("called after toUpper(), release allocated resources here");

Because this a QT Console Application, main.cpp file contains only main() function which calls QTest::qExec(&testString, testCmd) function to execute testfunctions in the specified test object.

#include <QApplication>
#include <QStringList>
#include <QDir>
#include "TestString.h"
int main(int argc, char *argv[])
QApplication a(argc, argv);
QStringList testCmd;
QDir qtestLogDir;
testCmd<<" "<<"-o"<<"UnitTest_Results/QTestString_log.txt";
TestString testString;
QTest::qExec(&testString, testCmd);
return a.exec();

When you run the application, test execution output is displayed at QT Creator Application Output Window. In order to redirect testfunction output to specified log file; output directory and log file name information are passed as argument to QTest::qExec(&testString, testCmd) function.

testCmd<<" "<<"-o"<<"UnitTest_Results/QTestString_log.txt";

command line argument redirects output to UnitTest_Results/QTestString_log.txt file.

QTestString_log.txt file can be found under LogQTestOutputToFile/UnitTest_Results folder.

Wednesday, July 27, 2011

Load QGraphicsScene From QDataStream

Qt allows you to read binary data from files into QDataStream. In this sample serialized QGprahicsItem is created on the QGraphicsScene by reading related data from a text file.
Sample project started as a QT Gui Application and contains main.cpp, LoadSceneFromQDataStream.h, LoadSceneFromQDataStream.cpp, MyGraphicsItem.h and MyGraphicsItem.cpp files.
Project Directory Structure in Qt Creator IDE : is the project configuration file and contains:

QT += core gui
TARGET = LoadSceneFromQDataStream
SOURCES += main.cpp\
LoadSceneFromQDataStream.cpp \
HEADERS += LoadSceneFromQDataStream.h \

main.cpp file contains the custom QDialog instance and shows it.

#include <QApplication>
#include "LoadSceneFromQDataStream.h"
int main(int argc, char *argv[])
QApplication a(argc, argv);
Dialog w;;
return a.exec();

LoadSceneFromQDataStream.h file contains custom QDialog which displays content of binary file in a QGraphicsScene instance.

#include <QDialog>

class QGraphicsScene;
class QGraphicsView;
class QVBoxLayout;

class Dialog : public QDialog

Dialog(QWidget *parent = 0);

public slots:
void sl_loadSceneFromFile();

QGraphicsScene* scene;
QGraphicsView* view;
QVBoxLayout* layout;
QPushButton* btnLoad;


LoadSceneFromQDataStream.cpp file contains implementation details for the custom QDialog.

#include "LoadSceneFromQDataStream.h"
#include "MyGraphicsItem.h"

#include <QVBoxLayout>
#include <QPushButton>
#include <QGraphicsView>
#include <QFile>
#include <QMessageBox>
#include <QDir>

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
scene = new QGraphicsScene(this);
view = new QGraphicsView(scene,this);
layout = new QVBoxLayout(this);
btnLoad = new QPushButton("Load Scene From File");
connect(btnLoad, SIGNAL(clicked()), this, SLOT(sl_loadSceneFromFile()));

delete btnLoad;
delete layout;
delete scene;
delete view;

void Dialog::sl_loadSceneFromFile()
QString fileName(QDir::currentPath().append("/sceneData.txt"));
QFile file(fileName);
if (!
QDataStream in(&file);
int itemListSize;
in >> itemListSize;
for( int i = 0; i< itemListSize; i++ )
qreal xCoord = 0;
qreal yCoord = 0;
in >> xCoord;
in >> yCoord;
MyGraphicsItem* item = new MyGraphicsItem();
QMessageBox::warning(this,"Success","Loaded Scene Items From File");

sl_loadSceneFromFile() opens sceneData.txt file and reads its content into QDataStream instance.
sceneData.txt file is located under LoadSceneFromQDataStream/LoadSceneFromQDataStream folder of the project. sceneData.txt file is created by the previous sample application.

First serialized data to the sceneData.txt file is the number of QGraphicsItems on the QGraphicsScene. It is read into the variable “itemListSize” from sceneData.txt file. Then for each QGraphicsItem on the QGraphicsScene, xCoordinate and yCoordinate of the QGraphicsItem is stored in the file in binary format. By reading x and y coordinate of the QGraphicsItem from sceneData.txt file, item’s position is set. After setting the pos of the QGraphicsItem, it is inserted into the list of QGraphicsItems on the QGraphicsScene by calling the addItem function of QGraphicsScene.

Regenerated QGraphicsItem instance on the QGraphicsScene is a custom/derived QGprahicsItem and is defined in MyGraphicsItem.h file.

#include <QGraphicsItem>
class MyGraphicsItem : public QGraphicsItem
MyGraphicsItem(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);

QRectF boundingRect() const;

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget);


Custom QGraphicsItem contains constructor, destructor, boundingRect() and paint() methods.

MyGraphicsItem.cpp file contains implementation details for the custom QGraphicsItem class.

#include "MyGraphicsItem.h"
#include <QPainter>
MyGraphicsItem::MyGraphicsItem(QGraphicsItem* parent, QGraphicsScene* scene) : QGraphicsItem(parent,scene)


QRectF MyGraphicsItem::boundingRect() const
qreal penWidth = 1;
return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
20 + penWidth, 20 + penWidth);

void MyGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)

QPen pen;
painter->drawRoundedRect(boundingRect(), 25, 25, Qt::RelativeSize);

When you run the application, a dialog with a "load scene from file" button is displayed.By clicking the qpushbutton, application searches for the sceneData.txt file and loads files content into the QGraphicsScene.