Monday, November 14, 2011

Call Symbol From Shared Object File (DLL) with QLibrary

    Shared libraries are represented with *.dll and *.so files on windows and unix platforms; respectively. Symbols in a shared object file (*.dll,*.so) are designed to be exported by the library writer. Client imports symbols from shared library. 
    In this sample there are two projects in the Qt Creator projects pane which are SharedLib and SharedLibClient. SharedLib is a C++ Library project which creates a shared object file (dll) with an exported symbol in it. SharedLibClient is a QT Console Application which calls SharedLib dll at runtime. In order to debug SharedLibClient project successfully, required “SharedLib.dll” file is placed under the debug folder of the SharedLibClient project.


SharedLib project contains a simple symbol which sums two integers and returns the result.
SharedLib.pro is the project configuration file and contains: 
QT       -= gui
TARGET = SharedLib
TEMPLATE = lib
DEFINES += SHAREDLIB_LIBRARY
SOURCES += Sharedlib.cpp
HEADERS += Sharedlib.h\
        SharedLib_global.h
SharedLib_global.h is created by the QT Creator IDE for the new C++ Library project and contains:
#ifndef SHAREDLIB_GLOBAL_H
#define SHAREDLIB_GLOBAL_H

#include <QtCore>

#if defined(SHAREDLIB_LIBRARY)
#  define SHAREDLIBSHARED_EXPORT Q_DECL_EXPORT
#else
#  define SHAREDLIBSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif

Sharedlib.h is the header file and contains implementation details for the Sharedlib.
#ifndef SHAREDLIB_H
#define SHAREDLIB_H

#include "SharedLib_global.h"

class SHAREDLIBSHARED_EXPORT SharedLib {
public:
    SharedLib();
    int addNumbers(int num1, int num2);
};

#endif
Sharedlib.cpp file contains the implementation for the addNumbers function.
#include "Sharedlib.h"

extern "C" __declspec(dllexport) int addNumbers(int no1, int no2)
{
    return no1+no2;
}
addNumbers(int,int) symbol is exported as a C function from the SharedLib library. Function is also wrapped in an extern "C" block. If you are building on Windows, the function needs to be explicitly exported from the DLL using the __declspec(dllexport) compiler directive.
In order to create "SharedLib" project in Qt Creator, create a "Qt Shared Library" project and add only one cpp file into it. Inside this cpp file sign your function that you are willing to export with dllexport macro. Then build the project in debug mode. After building the "SharedLib" project in debug mode by Qt Creator IDE, related dll file is generated under the "SharedLibClient\SharedLibClient-build-desktop" folder of SharedLib project for Windows platform.

In order to create "SharedLibClient" project in Qt Creator, create a simple client "Qt Console Application" that is going to use "addNumbers" function from "SharedLib.dll" library. SharedLibClient project contains simply a main.cpp file. In this main.cpp file QLibrary class of QT framework is used to load shared library (SharedLib.dll on Windows) at runtime. One of the amazing features of QLibrary is that it provides platform independent access to specific library at run-time. In order to load specific library at run-time, just give the name of the *.dll or *.so file without suffix to QLibrary constructor. In this case only the name of the library without suffix "SharedLib" is going to be enough to load it into memory.

SharedLibClient.pro is the project configuration file and contains :
QT       += core
QT       -= gui
TARGET = SharedLibClient
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
In main.cpp file, shared library function addNumbers from the SharedLib.dll is called by using the QLibrary resolve function.
#include <QCoreApplication>
#include <QLibrary>
#include <QtDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int result = 0;
    QLibrary myLib("SharedLib");
    typedef int (*MyPrototype)(int,int);
    MyPrototype myFunction = (MyPrototype) myLib.resolve("addNumbers");
    if( myFunction )
        result = myFunction(3,2);
    if( myFunction == 0 || result == 0 ) {
        qDebug() << myLib.errorString();
        qDebug() << "Can not get result from dll!";
        myLib.unload();
        return 0;
    }
    result = result+2;
    qDebug() << result;
    myLib.unload();
    return a.exec();
}

After running the SharedLibClient project in debug mode, the result is displayed on the console output.

main function of “SharedLibClient” project accomplishes following steps sequentially :
- Loads the library by passing the library file name in the QLibrary constructor
- Declares a function pointer to the symbol
- Calls the function of the exported library by resolve function of QLibrary

If QLibrary can not load the symbol from library, then the function pointer will be assigned to 0.

Benefits of Using QLibrary to Load Shared Object (DLL) Files at Run-Time :
- You do not need to have the header and lib files to compile the application.
- Just put your dll file that you want to load at run-time next to your executable .
- Executable can start without having dll next to it because dll is going to be loaded at run-time when it is required.
- Helps to generate a smaller executable file as a result.

Thursday, October 20, 2011

Triple Pointer Operations in C++

Double pointer can be used to point to two dimensional matrix (pointer-to-pointer). If there are more than one matrices and it is required to keep track of these matrices then an additional extra pointer will be required. At his point triple-pointers can be used to point to list of two dimensional matrices.
We assume that you have got three different two-dimensional matrices as in the below picture:
Triple pointer with size of 3 and pointing to 3 different matrices:



#include <QCoreApplication>
#include <iostream>
using namespace std;

//function prototype
int** initializeMatrix(int** tempMatrix, int row, int column, int matrixCellValue);


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int size = 3;
    int*** triplePointer;

    //Allocate memory for triplePointer
    triplePointer = new int**[size];

    int rowNum = 4;
    int columnNum = 3;
    int** newMatrix = 0;

    //Assign elements to cell values of each matrix
    for(int i=0; i<size; i++)
        triplePointer[i] = initializeMatrix(newMatrix,rowNum,columnNum,i);

    //Print elements of allocated matrices
    for(int num = 0; num<size; num++) {
        for(int i=0; i<rowNum; i++) {
            for(int j=0; j<columnNum; j++) {
                    cout<<triplePointer[num][i][j];
            }
            cout << "\n";
        }
        cout << "\n";
    }

    return a.exec();


}

/*
tempMatrix: matrix to allocate
row: number of rows of the matrix
column: number of columns of the matrix
matrixCellValue: value to assign to specified cell of matrix
 */
int** initializeMatrix(int** tempMatrix, int row, int column, int matrixCellValue)
{
    tempMatrix = new int*[row];
    for(int i=0; i<row; i++)
            tempMatrix[i] = new int[column];
    for(int i=0; i<row; i++)
            for(int j=0; j<column; j++)
                    tempMatrix[i][j] = matrixCellValue;

    return tempMatrix;
}


Sample c++ console application built with qt-creator on ubuntu and the console-output is 3 different 2x2 matrices displayed. Same output is produced when built with MSVS2008 on winXP.



Wednesday, October 12, 2011

Double Pointer Operations in C++

When initialized properly double pointers can be used as a 2 dimensional matrix. Sample qt-console application shows a way to allocate, initialize and deallocate a double pointer in C++.

Following diagrams help me to remember the subject of dynamically allocating multi-dimensional arrays in C++. 
Pointer-to-integer (simple integer array) declaration and representation: Can be considered as an array of integers.
int* ptrToInt;
pointer to integer
Pointer-to-pointer-to-integer (multi-dimensional matrix) declaration and representation: Can be considered as array of arrays of integers.
int** ptrToPtrToInt;
pointer to pointer to interger

#include <QCoreApplication>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int** myRectRegion;

    int numRows = 4;
    int numCols = 3;

    //allocate double pointer
    myRectRegion = new int* [numRows];
    for(int i=0;i<numRows;i++)
            myRectRegion[i] = new int[numCols];


    //initialize double pointer
    for(int i=0; i<numRows; i++)
            for(int j=0;j<numCols;j++)
                    myRectRegion[i][j] = 1;


    //print double pointer elements
    for(int i=0; i<numRows; i++){
        for(int j=0; j<numCols; j++){
           cout << myRectRegion[i][j];
        }
        cout << "\n";
    }


    //free deallocate double pointer
    for(int i=0;i<numRows;i++)
            delete[] myRectRegion[i];
    delete[] myRectRegion;

    return a.exec();
}

When the application is run, a 4X3 matrix is displayed in the terminal window.

There is also a helpful tutorial about dynamic allocation of multi-dimensional arrays in C at : http://c-faq.com/~scs/cclass/int/sx9b.html

Tuesday, August 23, 2011

Draw Grid on QGraphicsScene

Derived QGraphisScene class enables child class to reimplement drawBackground() function of the parent class. By reimplementing drawBackground() function, child class draws the background of the scene using painter.
Sample project started as a QT Gui Application and contains main.cpp, CustomQGraphicsScene.h, CustomQGraphicsScene.cpp, Dialog.h and Dialog.cpp files.
Project Directory Structure in Qt Creator IDE :
Qt project cpp and header files for draw grid on qgraphicsscene

DrawGridOnQGraphicsScene.pro is the project configuration file and contains:
QT       += core gui
TARGET = DrawGridOnQGraphicsScene
TEMPLATE = app
SOURCES += main.cpp \
           Dialog.cpp \
           CustomQGraphicsScene.cpp
HEADERS  += Dialog.h \
            CustomQGraphicsScene.h

main.cpp file contains the custom QDialog instance and shows it.
#include <QApplication>
#include "Dialog.h"

int main(int argc, char *argv[])
{
   QApplication a(argc, argv);
   Dialog w;
   w.show();
   return a.exec();
}


Dialog.h is derived from QDialog class which contains QGraphicsView in it.
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>

class CustomQGraphicsScene;
class QGraphicsView;
class QVBoxLayout;

class Dialog : public QDialog
{
   Q_OBJECT
public:
   Dialog(QWidget *parent = 0);
   ~Dialog();
private:
   CustomQGraphicsScene* scene;
   QGraphicsView* view;
   QVBoxLayout* layout;
};

#endif // DIALOG_H

Dialog.cpp contains implementation details for derived QDialog class.
#include "Dialog.h"
#include "CustomQGraphicsScene.h"
#include <QVBoxLayout>
#include <QGraphicsView>

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
   scene = new CustomQGraphicsScene(this);
   view = new QGraphicsView(scene,this);
   layout = new QVBoxLayout(this);
   layout->addWidget(view);
   setLayout(layout);
   resize(270,200);
}

Dialog::~Dialog()
{
   delete scene;
   delete view;
}


Custom QDialog creates an instance of derived QGraphicsScene and displays in a QGraphicsView.
CustomQGraphicsScene.h is derived from QGraphicsScene in which drawBackground() function is reimplemented to draw grid on it.
#ifndef CUSTOMQGRAPHICSSCENE_H
#define CUSTOMQGRAPHICSSCENE_H
#include <QGraphicsScene>

class CustomQGraphicsScene : public QGraphicsScene
{
public:
   CustomQGraphicsScene(QObject *parent);

protected:
   void drawBackground(QPainter * painter, const QRectF & rect );
};

#endif // CUSTOMQGRAPHICSSCENE_H

CustomQGraphicsScene.cpp contains implementation details for derived QGraphicsScene class.
#include "CustomQGraphicsScene.h"
#include <QPainter>

static const int GRID_STEP = 30;

inline qreal round(qreal val, int step) {
   int tmp = int(val) + step /2;
   tmp -= tmp % step;
   return qreal(tmp);
}

CustomQGraphicsScene::CustomQGraphicsScene(QObject *parent ) : QGraphicsScene(parent)
{}

void CustomQGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
{
   int step = GRID_STEP;
   painter->setPen(QPen(QColor(200, 200, 255, 125)));
   // draw horizontal grid
   qreal start = round(rect.top(), step);
   if (start > rect.top()) {
      start -= step;
   }
   for (qreal y = start - step; y < rect.bottom(); ) {
      y += step;
      painter->drawLine(rect.left(), y, rect.right(), y);
   }
   // now draw vertical grid
   start = round(rect.left(), step);
   if (start > rect.left()) {
      start -= step;
   }
   for (qreal x = start - step; x < rect.right(); ) {
      x += step;
      painter->drawLine(x, rect.top(), x, rect.bottom());
   }
}


Drawing grid process is divided into two substeps such as drawing vertical and drawing horizontal lines. Derived QGraphicsScene instance in which drawBackground() function is reimplemented looks like following screenShot :

qt result qgraphicsscene with a grid

Wednesday, August 3, 2011

Save QGraphicsScene to XML File By Using QXmlStreamWriter

QGraphicsItems that are residing in a QGraphicsScene can be saved to XML file by using QXMLStreamWriter class.
Sample project started as a QT Gui Application and contains main.cpp, SaveQGraphicsSceneToXML.h, SaveQGraphicsSceneToXML.cpp, MyGraphicsItem.h and MyGraphicsItem.cpp files.
Project Directory Structure in Qt Creator IDE :



SaveQGraphicsSceneToXML.pro is the project configuration file and contains:
QT       += core gui
TARGET = SaveQGraphicsSceneToXML
TEMPLATE = app
SOURCES += main.cpp\
        SaveQGraphicsSceneToXML.cpp\
        MyGraphicsItem.cpp
HEADERS  += SaveQGraphicsSceneToXML.h\
            MyGraphicsItem.h

main.cpp file contains the custom QDialog instance and shows it.
#include <QApplication>
#include "SaveQGraphicsSceneToXML.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}

MyGraphicsItem is a derived custom QGraphicsItem class that implements boundingRect() and paint() functions of the parent. MyGraphicsItem is a rectangular derived QGraphicsItem and added to the QGraphicsScene.
MyGraphicsItem.h file contains custom QGraphicsItem class declaration.
#ifndef MYGRAPHICSITEM_H
#define MYGRAPHICSITEM_H
#include <QGraphicsItem>
class MyGraphicsItem : public QGraphicsItem
{
public:
    MyGraphicsItem(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);
    ~MyGraphicsItem();

protected:
    QRectF boundingRect() const;

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
               QWidget *widget);
};
#endif // MYGRAPHICSITEM_H

MyGraphicsItem.cpp file contains implementation details for custom QGraphicsItem class.
#include "MyGraphicsItem.h"
#include <QPainter>
MyGraphicsItem::MyGraphicsItem(QGraphicsItem* parent, QGraphicsScene* scene) : QGraphicsItem(parent,scene)
{
}

MyGraphicsItem::~MyGraphicsItem(){}

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)
{
    Q_UNUSED(widget)
    Q_UNUSED(option)

    QPen pen;
    pen.setWidthF(2);
    pen.setStyle(Qt::DashLine);
    painter->setPen(pen);
    painter->setBrush(QBrush(QColor(50,120,80)));
    painter->drawRoundedRect(boundingRect(), 25, 25, Qt::RelativeSize);
}

SaveQGraphicsSceneToXML.h is derived from QDialog class which contains QGraphicsView in it.
#ifndef SAVEQGRAPHICSSCENETOXML_H
#define SAVEQGRAPHICSSCENETOXML_H
#include <QDialog>

class QGraphicsScene;
class MyGraphicsItem;
class QGraphicsView;
class QVBoxLayout;

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

public slots:
    void sl_saveSceneToXML();

private:
    QGraphicsScene* scene;
    MyGraphicsItem* item;
    QGraphicsView* view;
    QVBoxLayout* layout;
    QPushButton* btnSaveToXML;
};
#endif // SAVEQGRAPHICSSCENETOXML_H

SaveQGraphicsSceneToXML.cpp contains implementation details for derived QDialog class.
#include "SaveQGraphicsSceneToXML.h"
#include "MyGraphicsItem.h"

#include <QGraphicsScene>
#include <QGraphicsView>
#include <QVBoxLayout>
#include <QPushButton>
#include <QFile>
#include <QList>
#include <QMessageBox>
#include <QXmlStreamWriter>
#include <QDir>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    scene = new QGraphicsScene(this);
    item = new MyGraphicsItem();
    item->setPos(34,35);
    scene->addItem(item);
    view = new QGraphicsView(scene,this);
    layout = new QVBoxLayout(this);
    layout->addWidget(view);
    btnSaveToXML = new QPushButton("Save Scene To XML");
    connect(btnSaveToXML, SIGNAL(clicked()), this, SLOT(sl_saveSceneToXML()));
    layout->addWidget(btnSaveToXML);
    setLayout(layout);
    resize(270,200);
}

Dialog::~Dialog()
{
    delete btnSaveToXML;
    delete item;
    delete scene;
    delete view;
    delete layout;
}

void Dialog::sl_saveSceneToXML()
{
    QString fileName(QDir::currentPath().append("//sceneData.xml"));
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly))
    {
            return;
    }
    QXmlStreamWriter xmlWriter(&file);
    xmlWriter.setAutoFormatting(true);
    xmlWriter.writeStartDocument();
    xmlWriter.writeStartElement("SceneData");
    xmlWriter.writeAttribute("version", "v1.0");
    xmlWriter.writeStartElement("GraphicsItemList");
    foreach( QGraphicsItem* item, scene->items())
    {
        if( item->type() == MyGraphicsItem::Type )
        {
            MyGraphicsItem* myItem = (MyGraphicsItem*)item;
            xmlWriter.writeStartElement("MyGraphicsItem");
            xmlWriter.writeAttribute("xCoord", QString::number(myItem->x()));
            xmlWriter.writeAttribute("yCoord", QString::number(myItem->y()));
            xmlWriter.writeEndElement();  //end of MyGraphicsItem
        }
    }
    xmlWriter.writeEndElement();   //end of GraphicsItemList
    xmlWriter.writeEndElement();   //end of SceneData
    QMessageBox::warning(this,"Success","Saved Scene Data to XML File");
    close();
}

QGraphicsScene contains custom QGraphicsItem in it. Derived QGraphicsItem position is set and then added onto the QGraphicsScene. Different QGraphicsItems can be added onto the QGraphicsScene, too.

btnSaveToXML QPushButton is connected to sl_saveSceneToXML slot of current Dialog instance. By clicking on the QPushButton instance which has a text property set to "Save Scene To XML" sl_saveSceneToXML slot is called.

sl_saveSceneToXML slot saves the content of QGraphicsScene into the sceneData.xml file. QxmlStreamWriter is used to write data into the opened file.

xmlWriter.writeStartDocument(); line writes a document which starts with XML version number "1.0" and also writes the encoding "UTF-8" information.

xmlWriter.writeStartElement("SceneData"); line writes the start element with “SceneData”.

xmlWriter.writeAttribute("version", "v1.0"); line adds “version” attribute with “v1.0” value to “SceneData” element.

xmlWriter.writeStartElement("GraphicsItemList"); line writes "GraphicsItemList" under “SceneData” element.

Foreach custom QGraphicsItem instance that is residing in the QGraphicsScene a new element is inserted into the xml file with writeStartElement("MyGraphicsItem") function of QXmlStreamWriter. Previously setted xCoord and yCoord attributes of custom QGraphicsItem are written into the xml file with writeAttribute("yCoord", Qstring::number(myItem->y())); function of QxmlStreamWriter.
Foreach writeStartElement() function, corresponding writeEndElement() function is called to close the previous start element. Each QGraphicsItem instance is identified by its item->type() and written into the XML file.

“GraphicsItemList” and “SceneData” elements are closed by calling xmlWriter.writeEndElement(); .

Saved XML file content is :

 
 
     
         
     


Tuesday, August 2, 2011

Save Screenshot of QGraphicsScene

In order to display items that are residing in QGraphicsScene, QGraphicsView is used. In the sample application Custom QDialog contains a QGraphicsView with a QGraphicsScene inside it.

Project Directory Structure in Qt Creator IDE :



SaveQGraphicsSceneAsImage.pro is the project configuration file and contains:
QT       += core gui
TARGET = SaveQGraphicsSceneAsImage
TEMPLATE = app
SOURCES += main.cpp\
        Dialog.cpp
HEADERS  += Dialog.h


main.cpp file contains the custom QDialog instance and shows it.
#include <QApplication>
#include "Dialog.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}


Dialog.h file contains custom QDialog interface.
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class QGraphicsScene;
class QGraphicsView;
class QVBoxLayout;

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

public slots:
    void sl_saveSceneAsImage();

private:
    QGraphicsScene* scene;
    QGraphicsView* view;
    QVBoxLayout* layout;
    QPushButton* btnSaveScene;
};
#endif // DIALOG_H


Dialog.cpp file contains implementation details for the custom QDialog.
#include "Dialog.h"

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QVBoxLayout>
#include <QPushButton>
#include <QString>
#include <QFileDialog>
#include <QPixmap>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    scene = new QGraphicsScene(this);
    scene->addEllipse(22,3,12,13,QPen(QColor("red")));
    scene->addLine(1,2,8,9,QPen("blue"));
    scene->addRect(12,12,6,7,QPen("green"));
    view = new QGraphicsView(scene,this);
    layout = new QVBoxLayout(this);
    btnSaveScene = new QPushButton("Take Scene SnapShot");
    connect(btnSaveScene, SIGNAL(clicked()), this, SLOT(sl_saveSceneAsImage()));
    layout->addWidget(view);
    layout->addWidget(btnSaveScene);
    setLayout(layout);
    resize(270,200);
}

Dialog::~Dialog()
{
    delete btnSaveScene;
    delete scene;
    delete view;
    delete layout;
}

void Dialog::sl_saveSceneAsImage()
{
    QString fileName = QFileDialog::getSaveFileName(this, "Save Scene", "", "Image (*.png)");
    QPixmap pixMap = QPixmap::grabWidget(view);
    pixMap.save(fileName);
    close();
}


QGraphicsScene content can be saved as a png image file. Sample QGraphicsScene contains items of type line,rectangle and ellipse. Taken snapshot displays all these items inside of saved snapshot.

sl_saveSceneAsImage() slot of custom QDialog uses Qpixmap::grabWidget function to create a pixmap and paints the QGraphicsView inside it. Because all the child items are also painted in then QGraphicsScene becomes painted inside snapshot, too.

As a result Qpixmap instance is saved to specified location with :
pixMap.save(fileName);


Sample QGraphicsScene content :




Saved QGraphicsScene Snapshot :



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 :



LogQTestOutputToFile.pro is the project configuration file and contains:
QT       += testlib core
TARGET = LogQTestOutputToFile
TEMPLATE = app
SOURCES += main.cpp \
    TestString.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
{
    Q_OBJECT
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;
    qtestLogDir.mkdir("UnitTest_Results");
    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 :


LoadSceneFromQDataStream.pro is the project configuration file and contains:
QT       += core gui
TARGET = LoadSceneFromQDataStream
TEMPLATE = app
SOURCES += main.cpp\
        LoadSceneFromQDataStream.cpp \
    MyGraphicsItem.cpp
HEADERS  += LoadSceneFromQDataStream.h \
    MyGraphicsItem.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;
    w.show();
    return a.exec();
}

LoadSceneFromQDataStream.h file contains custom QDialog which displays content of binary file in a QGraphicsScene instance.
#ifndef LOADSCENEFROMQDATASTREAM_H
#define LOADSCENEFROMQDATASTREAM_H
#include <QDialog>

class QGraphicsScene;
class QGraphicsView;
class QVBoxLayout;

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

public slots:
    void sl_loadSceneFromFile();

private:
    QGraphicsScene* scene;
    QGraphicsView* view;
    QVBoxLayout* layout;
    QPushButton* btnLoad;
};

#endif // LOADSCENEFROMQDATASTREAM_H

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);
    layout->addWidget(view);
    btnLoad = new QPushButton("Load Scene From File");
    connect(btnLoad, SIGNAL(clicked()), this, SLOT(sl_loadSceneFromFile()));
    layout->addWidget(btnLoad);
    setLayout(layout);
    resize(270,200);
}

Dialog::~Dialog()
{
    delete btnLoad;
    delete layout;
    delete scene;
    delete view;
}

void Dialog::sl_loadSceneFromFile()
{
    QString fileName(QDir::currentPath().append("/sceneData.txt"));
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
    {
            return;
    }
    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();
        item->setPos(xCoord,yCoord);
        scene->addItem(item);
    }
    QMessageBox::warning(this,"Success","Loaded Scene Items From File");
    close();
}

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.
#ifndef MYGRAPHICSITEM_H
#define MYGRAPHICSITEM_H
#include <QGraphicsItem>
class MyGraphicsItem : public QGraphicsItem
{
public:
    MyGraphicsItem(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);
    ~MyGraphicsItem();

protected:
    QRectF boundingRect() const;

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

#endif // MYGRAPHICSITEM_H

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)
{
}

MyGraphicsItem::~MyGraphicsItem(){}

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)
{
    Q_UNUSED(widget)
    Q_UNUSED(option)

    QPen pen;
    pen.setWidthF(2);
    pen.setStyle(Qt::DashLine);
    painter->setPen(pen);
    painter->setBrush(QBrush(QColor(50,120,80)));
    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.



Tuesday, June 21, 2011

Serialize QGraphicsScene Binary Data By Using QDataStream

QGraphicsScene class enables easy management of QGraphicsItem instances in a 2D environment. QGraphicsScene acts as a container for custom QGraphicsItems.
Custom QDialog with a QGraphicsView helps to visualize QGraphicsItems that are included in a QGraphicsScene. By using addItem() function of QGraphicsScene, custom QGraphicsItems can be added to the QGraphicsScene instance. QGprahicsItem binary data such as x() and y() coordinates can be serialized to a QFile by using QDataStream.
Sample project started as a QT Gui Application and contains main.cpp, SceneToDataStream.h, SceneToDataStream.cpp, MyGraphicsItem.h and MyGraphicsItem.cpp files.
Project Directory Structure in Qt Creator IDE :

SceneToDataStream.pro is the project configuration file and contains:
QT       += core gui
TARGET = SceneToDataStream
TEMPLATE = app
SOURCES += main.cpp\
        SceneToDataStream.cpp \
    MyGraphicsItem.cpp
HEADERS  += SceneToDataStream.h \
    MyGraphicsItem.h

main.cpp file contains the custom QDialog instance and shows it.
#include <QApplication>
#include "SceneToDataStream.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}

SceneToDataStream.h file is the extended QDialog class header file.
#ifndef SCENETODATASTREAM_H
#define SCENETODATASTREAM_H
#include <QDialog>

class QGraphicsScene;
class MyGraphicsItem;
class QGraphicsView;
class QVBoxLayout;

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

public slots:
    void sl_saveSceneToFile();

private:
    QGraphicsScene* scene;
    MyGraphicsItem* item;
    QGraphicsView* view;
    QVBoxLayout* layout;
    QPushButton* btnSave;
};

#endif // SCENETODATASTREAM_H

SceneToDataStream.cpp file contains implementation details for extended QDialog class.
#include "SceneToDataStream.h"
#include "MyGraphicsItem.h"

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

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    scene = new QGraphicsScene(this);
    item = new MyGraphicsItem();
    item->setPos(34,35);
    scene->addItem(item);
    view = new QGraphicsView(scene,this);
    layout = new QVBoxLayout(this);
    layout->addWidget(view);
    btnSave = new QPushButton("Save Scene To File");
    connect(btnSave, SIGNAL(clicked()), this, SLOT(sl_saveSceneToFile()));
    layout->addWidget(btnSave);
    setLayout(layout);
    resize(270,200);
}

Dialog::~Dialog()
{
    delete btnSave;
    delete layout;
    delete item;
    delete scene;
    delete view;
}

void Dialog::sl_saveSceneToFile()
{
    QString fileName = QDir::currentPath().append("/sceneData.txt");
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly))
    {
            return;
    }
    QDataStream out(&file);
    QList<QGraphicsItem*> itemList = scene->items();
    int itemListSize = itemList.size();
    out << itemListSize;
    foreach( QGraphicsItem* item, itemList)
    {
        out << item->x();
        out << item->y();
    }
    QMessageBox::warning(this,"Success","Saved Scene Data to File");
    close();
}

sl_saveSceneToFile() slot of Dialog class is connected to QPushButton clicked signal and serializes number of all the items on the scene with item x() and y() coordinates respectively.

MyGraphicsItem is a derived custom QGraphicsItem class that implements boundingRect() and paint() functions of the parent. MyGraphicsItem is a rectangular derived QGraphicsItem and added to the QGraphicsScene.
MyGraphicsItem.h file contains custom QGraphicsItem class declaration.
#ifndef MYGRAPHICSITEM_H
#define MYGRAPHICSITEM_H
#include <QGraphicsItem>
class MyGraphicsItem : public QGraphicsItem
{
public:
    MyGraphicsItem(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);
    ~MyGraphicsItem();

protected:
    QRectF boundingRect() const;

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

#endif // MYGRAPHICSITEM_H

MyGraphicsItem.cpp file contains implementation details for custom QGraphicsItem class.
#include "MyGraphicsItem.h"
#include <QPainter>
MyGraphicsItem::MyGraphicsItem(QGraphicsItem* parent, QGraphicsScene* scene) : QGraphicsItem(parent,scene)
{
}

MyGraphicsItem::~MyGraphicsItem(){}

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)
{
    Q_UNUSED(widget)
    Q_UNUSED(option)

    QPen pen;
    pen.setWidthF(2);
    pen.setStyle(Qt::DashLine);
    painter->setPen(pen);
    painter->setBrush(QBrush(QColor(50,120,80)));
    painter->drawRoundedRect(boundingRect(), 25, 25, Qt::RelativeSize);
}

sceneData.txt is the produced file that contains serialized binary data.

Wednesday, June 8, 2011

Reimplementing closeEvent of QDialog

It is always required to ask the user before closing the dialog whether he/she is sure about that. closeEvent event handler can also be used to save the position of the dialog before it is being closed. closeEvent of QDialog is being called before than the destructor of the extended QDialog instance.
Sample extended QDialog shows a QMessageBox instance when the user clicks on the close button that is placed at the top right corner of the dialog and asks for confirmation before closing the dialog. Depending on the user’s selection, dialog closes or stays open.
Sample project started as a QT Gui Application and contains main.cpp, QDialogCloseEvent.h and QDialogCloseEvent.cpp files.
Project Directory Structure in Qt Creator IDE :

QDialogCloseEvent.pro is the project configuration file and contains:
QT       += core gui
TARGET    = QDialogCloseEvent
TEMPLATE  = app
SOURCES  += main.cpp\
         QDialogCloseEvent.cpp
HEADERS  += QDialogCloseEvent.h

main.cpp file contains the custom QDialog instance and shows it.
#include <QApplication>
#include "QDialogCloseEvent.h"
int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}

QDialogCloseEvent.h file is the extended QDialog class header file.
#ifndef QDIALOGCLOSEEVENT_H
#define QDIALOGCLOSEEVENT_H
#include <QDialog>
class Dialog : public QDialog
{
    Q_OBJECT
public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
protected:
    void closeEvent(QCloseEvent * event);
};
#endif // QDIALOGCLOSEEVENT_H

QDialogCloseEvent.cpp file contains implementation details for extended QDialog class.
#include "QDialogCloseEvent.h"
#include <QMessageBox>
#include <QCloseEvent>
Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
    resize(300,90);
}
Dialog::~Dialog(){}
void Dialog::closeEvent(QCloseEvent *event)
{
    QMessageBox msgBox;
    msgBox.setText("Are you sure you want to close?");
    msgBox.setStandardButtons(QMessageBox::Close | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Close);
    int result = msgBox.exec();
    switch (result) {
      case QMessageBox::Close:
          event->accept();
          break;
      case QMessageBox::Cancel:
          event->ignore();
          break;
      default:
          QDialog::closeEvent(event);
          break;
    }
}

Custom QDialog with QMessageBox:

Application created by QT Creator and uses Qt4.7 .

Tuesday, June 7, 2011

Show QImage in a QLabel

QImage enables to construct images from file paths on the disk. Constructed QImages can be displayed in a QLabel. By setting the pixmap value of QLabel from setPixmap function, QLabel displays content of QImage.
Sample project started as a QT Gui Application and contains main.cpp, ShowQImageDialog.h and ShowQImageDialog.cpp files.
ShowQImageDialog constructor creates a QGridLayout instance that is containing a QScrollArea inside it and displays the image in this area.
Project Directory Structure in Qt Creator IDE :


ShowQImageDialog.pro is the project configuration file and contains:
TARGET = ShowQImageDialog
CONFIG   += core gui
TEMPLATE = app
SOURCES += main.cpp \
    ShowQImageDialog.cpp
HEADERS += \
    ShowQImageDialog.h

main.cpp file contains the custom QDialog instance and shows it.
#include <QApplication>
#include "ShowQImageDialog.h"
int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    ShowQImageDialog w;
    w.show();
    return a.exec();
}

ShowQImageDialog.h file is the extended QDialog class header file.
#ifndef SHOWQIMAGEDIALOG_H
#define SHOWQIMAGEDIALOG_H
#include <QDialog>
#include <QGridLayout>
#include <QScrollArea>
#include <QLabel>
#include <QImage>

class ShowQImageDialog : public QDialog
{
    Q_OBJECT

public:
    ShowQImageDialog(QWidget *parent = 0);
    ~ShowQImageDialog();
private:
    QGridLayout* gridLayout;
    QImage* inputImg;
    QLabel* imgDisplayLabel;
    QScrollArea* scrollArea;
};

#endif // SHOWQIMAGEDIALOG_H

ShowQImageDialog.cpp file contains implementation details for extended QDialog class.
#include "ShowQImageDialog.h"
ShowQImageDialog::ShowQImageDialog(QWidget *parent) : QDialog(parent)
{
    gridLayout = new QGridLayout();
    inputImg = new QImage("/home/tufan/wallpaper.png");
    imgDisplayLabel = new QLabel("");
    imgDisplayLabel->setPixmap(QPixmap::fromImage(*inputImg));
    imgDisplayLabel->adjustSize();
    scrollArea = new QScrollArea();
    scrollArea->setWidget(imgDisplayLabel);
    scrollArea->setMinimumSize(256,256);
    scrollArea->setMaximumSize(512,512);
    gridLayout->addWidget(scrollArea,0,0);
    setLayout(gridLayout);
}

ShowQImageDialog::~ShowQImageDialog()
{
    delete inputImg;
    delete imgDisplayLabel;
    delete scrollArea;
    delete gridLayout;
}

ShowQImageDialog:

Application created by QT Creator and uses Qt4.7 .

Check for valid file extension provided by QFileDialog

QFileDialog is used to select files from a specific directory. getOpenFileName() static function of QFileDialog returns the name of the selected file. Also, a filter can be set for the type of the file selected by the user by separating types with ';;'.
Ex:
"All Files (*.*);;JPEG (*.jpeg *.jpg);;PNG (*.png)"
In order to check for file extension of a file name provided by BrowseDialog which is extending QDialog and containing QFileDialog in its sl_browseImage() slot,
bool isValidImageFile(const QString& str)
function is implemented.
isValidImageFile function takes the full path of the file and splits it into pieces to find the file extension from the original QString instance.
File extension is placed after the last period character, so the last member of the QStringList contains the file extension that we are looking for.
By getting the upper case of the file extension which is provided by the client, we can search for a suitable match in our accepted file extension list. If the provided file extension is not in our list, then the derived custom QDialog opens a QMessageBox and gives a warning to the client.
Sample project started as a QT Gui Application and contains main.cpp, BrowseFileDialog.h and BrowseFileDialog.cpp files.

Project Directory Structure in Qt Creator IDE :

BrowseFileDialog.pro is the project configuration file contains:
TARGET = BrowseFileDialog
CONFIG   += core gui
TEMPLATE = app
SOURCES += main.cpp \
    BrowseFileDialog.cpp
HEADERS += \
    BrowseFileDialog.h

main.cpp file contains the custom QDialog instance and shows it.
#include <QApplication>
#include "BrowseFileDialog.h"
int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    BrowseFileDialog w;
    w.show();
    return a.exec();
}

BrowseFileDialog.h file is the derived QDialog class header file. Custom QDialog contains signals and slots to be associated with buttons on the dialog.
#ifndef BROWSEFILEDIALOG_H
#define BROWSEFILEDIALOG_H
#include <QDialog>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>

class BrowseFileDialog : public QDialog
{
    Q_OBJECT
public:
    BrowseFileDialog(QWidget *parent = 0);
    ~BrowseFileDialog();
public slots:
    void sl_browseImage();
private:
    void initLayout();
    void initConnections();
    bool isValidImageFile(const QString& str);
    QLabel* label;
    QLineEdit* lineEdit;
    QPushButton* pushButtonBrowse;
    QPushButton* pushButtonOK;
    QPushButton* pushButtonCancel;
};
#endif // BROWSEFILEDIALOG_H

BrowseFileDialog.cpp file contains implementation details for extended QDialog class.
#include "BrowseFileDialog.h"
#include <QHBoxLayout>
#include <QFileDialog>
#include <QString>
#include <QMessageBox>
#include <QSpacerItem>
BrowseFileDialog::BrowseFileDialog(QWidget *parent) : QDialog(parent)
{
    initLayout();
    initConnections();
}

BrowseFileDialog::~BrowseFileDialog()
{
    delete label;
    delete lineEdit;
    delete pushButtonBrowse;
    delete pushButtonOK;
    delete pushButtonCancel;
}

void BrowseFileDialog::initLayout()
{
    QGridLayout* mainLayout = new QGridLayout(this);
    QHBoxLayout* hBoxLayout = new QHBoxLayout();
    hBoxLayout->setSpacing(0);
    hBoxLayout->setMargin(0);
    label = new QLabel("Image File Path :");
    lineEdit = new QLineEdit(this);
    lineEdit->setReadOnly(true);
    pushButtonBrowse = new QPushButton("Browse...");
    hBoxLayout->addWidget(label);
    hBoxLayout->addWidget(lineEdit);
    hBoxLayout->addWidget(pushButtonBrowse);
    QHBoxLayout* actionWidgetsHBoxLayout = new QHBoxLayout();
    pushButtonCancel = new QPushButton("Cancel");
    pushButtonOK = new QPushButton("OK");
    actionWidgetsHBoxLayout->addStretch();
    actionWidgetsHBoxLayout->addWidget(pushButtonCancel);
    actionWidgetsHBoxLayout->addWidget(pushButtonOK);
    mainLayout->addLayout(hBoxLayout,0,0);
    mainLayout->addLayout(actionWidgetsHBoxLayout,1,0);
    setLayout(mainLayout);
}

void BrowseFileDialog::initConnections()
{
    connect(pushButtonBrowse, SIGNAL(clicked()), this, SLOT(sl_browseImage()));
    connect(pushButtonOK, SIGNAL(clicked()), this, SLOT(accept()));
    connect(pushButtonCancel, SIGNAL(clicked()), this, SLOT(reject()));
}

void BrowseFileDialog::sl_browseImage()
{
    QString inputImagePath=QFileDialog::getOpenFileName(this,tr("Select Image File"),QDir::currentPath(),
                                                        tr("All Files (*.*);;JPEG (*.jpeg *.jpg);;PNG (*.png)"));
    if(isValidImageFile(inputImagePath))
        lineEdit->setText(inputImagePath);
    else {
            QMessageBox::information(this, tr("Error!"),
                    tr("Please Provide a valid input image with JPG,JPEG or PNG extension!"));
    }
}

bool BrowseFileDialog::isValidImageFile(const QString& strImageFileName)
{
    QStringList acceptedImageFileTypeList;
    acceptedImageFileTypeList << "JPG"<< "JPEG" << "PNG";
    QStringList splittedStrList = strImageFileName.split(".");
    if( ((splittedStrList.size())-1) >= 0) {
        QString fileExtensionToCheck = splittedStrList[((splittedStrList.size())-1)];
        if( acceptedImageFileTypeList.contains(fileExtensionToCheck.toUpper()) )
            return true;
        else
            return false;
    }
    else
        return false;
}

BrowseFileDialog:

Application created by QT Creator and uses Qt4.7 .

Sunday, May 8, 2011

Create Custom QDialogs With QLayout Classes

Qt Designer is used to design user interfaces for QT applications. In addition to using QT Designer to generate user interfaces, you can populate your dialogs with widgets on them dynamically at runtime by using layout classes. To achieve this, you need to extend qdialog class and add widgets to your derived qdialog instance dynamically at runtime.
Qt's layout classes are used to place widgets on parent dialogs or widgets. Instead of manually placing widgets on main containers and creating static user interfaces, layout classes help to place widgets easily. There are different QT layout classes but QHBoxLayout and QVBoxLayout are the most popular of them. By using the addWidget() method of layout class, you place items on the layout. After adding widgets to the layout properly, it is required to call setLayout() method of the main container to make layout visible on it.
Sample project started as a QT Gui Application and contains main.cpp, DynamicQDialog.h and DynamicQDialog.cpp files.
DynamicDialog.pro is the project configuration file contains:
TARGET = DynamicDialog
CONFIG   += qt gui
TEMPLATE = app
SOURCES += main.cpp \
    DynamicQDialog.cpp
HEADERS += \
    DynamicQDialog.h

main.cpp file contains the custom QDialog instance and shows it.
#include <QApplication>
#include "DynamicQDialog.h"
int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    DynamicQDialog dialog;
    dialog.show();
    return app.exec();
}

DynamicQDialog.h file is the derived QDialog class header file. Custom QDialog contains signals and slots to be associated with buttons on the dialog.
#ifndef DYNAMICQDIALOG_H
#define DYNAMICQDIALOG_H
#include <QDialog>
class DynamicQDialog : public QDialog
{
    Q_OBJECT
public:
    DynamicQDialog(QWidget * parent = 0, Qt::WindowFlags f = 0);
    ~DynamicQDialog();
public slots:
    void sl_accept();
    void sl_cancel();
signals:
};
#endif // DYNAMICQDIALOG_H

DynamicQDialog.cpp file contains implementation details for derived QDialog class. 2 QPushButtons added to the QHBoxLayout dynamically at runtime and displayed on the DynamicQDialog instance.
#include "DynamicQDialog.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QTextEdit>
#include <QMessageBox>

DynamicQDialog::DynamicQDialog(QWidget* parent, Qt::WindowFlags flags): QDialog( parent, flags )
{
    QGridLayout* mainGrid = new QGridLayout;
    QVBoxLayout* topLayout = new QVBoxLayout;
    topLayout->addWidget(new QTextEdit);
    mainGrid->addLayout(topLayout,0,0);

    QHBoxLayout* hLayout = new QHBoxLayout;
    QPushButton* btn;
    for( int i=0; i<2 br="" i="">        if( i == 0 ) {
            btn = new QPushButton("OK");
            connect(btn, SIGNAL(clicked()), this, SLOT(sl_accept()));
        }
        else if( i == 1 ) {
            btn = new QPushButton("Cancel");
            connect(btn, SIGNAL(clicked()), this, SLOT(sl_cancel()));
        }
        hLayout->addWidget(btn);
    }
    mainGrid->addLayout(hLayout,1,0);

    setLayout(mainGrid);
}

DynamicQDialog::~DynamicQDialog(){}

void DynamicQDialog::sl_accept(){
    QMessageBox msgBox;
    msgBox.setText("OK Clicked!");
    msgBox.exec();
}

void DynamicQDialog::sl_cancel(){
    QMessageBox msgBox;
    msgBox.setText("Cancel Clicked!");
    msgBox.exec();
}



Application created by QT Creator and uses Qt4.7 .