QT C++ Graphics Programming Tutorial Video
Posted Date Unknown 0 Comment


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..


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();
}


See All Responses Below...
Author
Message Rating
Posted on:
Please Login to Post Your Comments
Name (Max. 100 characters)
Please post your comments here
Select Language
Comments
Attach File(Max. Size: 2 MB)
A few simple rules when posting your Comments,
  1. Please post only answers relevant to the topic of discussion.
  2. Please dont misuse this site or do not be abusive, offensive, inappropriate,harass anyone on the boards or post ads or spam. Doing so will delete your inappropriate messages and will block or delete your account on this site. 

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud