Developing Graphics Applications Using QT C++
Rate this article
0 out of 5

Graphics applications are one of the indispensable part of Computer Software. Right from developing small applications like charting applications to developing interactive Game applications, Graphics programming challenges every aspects of programming including algorithm development, technology and other aspects which will push to the limits of the computer hardware.

In this Video Tutorial I will show you how to develop a Basic graphics application using QT C++ Libraries. The tutorial assumes that know know Object Oriented Programming Concepts.. Watch the Video From the YouTube Link below..




Summary of the Main Source code used in the Project..

MatrixGrid.h

#ifndef MATRIXGRID_H
#define MATRIXGRID_H

#include <QMainWindow>

namespace Ui {
    class MatrixGrid;
}

class MatrixGrid : public QMainWindow {
    Q_OBJECT
public:
    int m_width;
    int m_Height;
    int m_NoOfRows;
    int m_NoOfCols;
    int m_XOffset; //Offset from which drawing start
    int m_YOffset;

    MatrixGrid(QWidget *parent = 0);
    ~MatrixGrid();

protected:
    void changeEvent(QEvent *e);
    void paintEvent(QPaintEvent *);
    void Initialize();

private:
    Ui::MatrixGrid *ui;

private slots:
    void on_action200_activated();
    void on_action100_activated();
    void on_action50_activated();
    void on_action25_activated();
};

#endif // MATRIXGRID_H

MatrixGrid.cpp

#include "matrixgrid.h"
#include "ui_matrixgrid.h"

#include <QtGui>
 #include <QPalette>

const int DEFAULT_X_OFFSET= 100;
 const int DEFAULT_Y_OFFSET= 50;
 const int DEFAULT_NO_ROWS = 3;
 const int DEFAULT_NO_COLS=  3;
 const int DEFAULT_WIDTH  =  60;
 const int DEFAULT_HEIGHT =  60;

MatrixGrid::MatrixGrid(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MatrixGrid)
{
    ui->setupUi(this);
    Initialize();
}

MatrixGrid::~MatrixGrid()
{
    delete ui;
}

void MatrixGrid::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MatrixGrid::on_action25_activated()
{
    m_width=DEFAULT_WIDTH/4;
    m_Height=DEFAULT_HEIGHT/4;
    this->repaint();

}

void MatrixGrid::on_action50_activated()
{
    m_width=DEFAULT_WIDTH/2;
    m_Height=DEFAULT_HEIGHT/2;
    this->repaint();
}

void MatrixGrid::on_action100_activated()
{
    m_width=DEFAULT_WIDTH;
    m_Height=DEFAULT_HEIGHT;
    this->repaint();
}

void MatrixGrid::on_action200_activated()
{
    m_width=DEFAULT_WIDTH*2;
    m_Height=DEFAULT_HEIGHT*2;
    this->repaint();
}

void MatrixGrid::paintEvent(QPaintEvent *pEvent)
{
    int X=DEFAULT_X_OFFSET;
    int Y=DEFAULT_Y_OFFSET;

    QWidget::paintEvent(pEvent);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::red);


    for(int i=0;i<=m_NoOfRows;i++)
    {
        painter.drawLine(X, Y,X+ m_width* m_NoOfCols , Y);
        Y=Y+ m_Height;
    }

    X=DEFAULT_X_OFFSET;
    Y=DEFAULT_Y_OFFSET;
    for(int j=0;j<=m_NoOfCols;j++)
    {
        painter.drawLine(X, Y,X,Y+m_Height*m_NoOfRows );

        X=X+ m_width;
    }
}

void MatrixGrid ::Initialize()
{
     m_NoOfRows=DEFAULT_NO_ROWS;
     m_NoOfCols=DEFAULT_NO_COLS;
     m_width=DEFAULT_WIDTH;
     m_Height=DEFAULT_HEIGHT;
     m_XOffset=DEFAULT_X_OFFSET;
     m_YOffset=DEFAULT_Y_OFFSET;

}

main.cpp


#include <QtGui/QApplication>
#include "matrixgrid.h"

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



Download Source Code
Download QT C++ 4.6 Source code developed in Ubuntu Linux Environment from the link below.

About The Author
Tom Thomas
Mr. Tom is the Co-Founder and CTO of  KTS InfoTech.  He started his career as a C/C++ programmer in the late 90's and has been very active and passionate in programming since then. He had worked both in start-ups ( Infortech software Pvt Ltd) as well as in CMM Level 5 companies (NeST and Wipro Technologies) for clients like General Electric, Agilent, Hitachi, Toshiba, Fujitsu, Alcatel, Insurance Service Corporation etc. His experience as an Engineer, Architect, Project Manager, Chief Technical Officer and as a Teacher makes him ideal for any type of jobs related to Information Technology.

His role with his present employer includes exploring new business opportunities and partnerships, Developing software product frameworks, developing and executing marketing strategies for company products etc.  

He holds Masters degree in Physics and Computer Science form CUSAT , one of the premier Science and Technology Institutions in India .

His major interests are in Optical Networking, Robotics, Device drivers, Database, Graphics, web applications, Software product Engineering, Open source Technologies and Sports physics.

He resides in Kerala ,India, with his mother, wife and his little angel.

He can be reached on Skype at this ID thomas_tom99 or at his E Mail address tom.thomas (at)ktsinfotech.com

         
Number Of articles Posted
9
Number Of Messages Posted
207
Joined Date
12/08/2011
   
Author
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android