Showing posts with label Qt Programming Examples. Show all posts
Showing posts with label Qt Programming Examples. Show all posts

Saturday, May 7, 2016

Bit Operations: Qt Example

In this post i'm going to show you some bit operations with &, |, <<, >>, ~ using Qt example. This example include operations like set a particular bit in the given number, clear bit, check bit is set or not and to check whether the number is even or odd.

Following demonstrates the psuedo code of the operations.

> To Set a bit in the given number : perform Betwise OR (|) operation with  1 left shift(<<) by index of the bit to set
     num = num | (1<<index);

>To clear a bit in the given number: perform Betwise AND (&) operation with the inverted(~) 1 left shift(<<) by index of the bit to clear
    num = num & ~(1<<index);

Monday, May 27, 2013

Qt QML Introduction and Simple Example Program



QML is declarative language used to develop user interface, is a part of the Qt Quick module. Qt Quick consists of set of technologies including Qml, C++ API's for integrating Qml with Qt application, Qt creator IDE, rum time support.
Qt Quick module comes with the Qt with 4.7 or later versions.

Qml we use elements to draw something on the screen(output) like text, image, rectangle etc.Every element in the qml is like a object. We cal also implement javascript code in side the qml file. Item is the base type for the all the elements.

To start a Qml Application we need to import Qt Quick module with version in the beginning of the every qml file, because the elements we are going to use included in this module. For example the following element displat a rectangle.
Import QtQuick 1.0
Rectangle {
    id: rect
    width: 300
    height: 300
    color: "red"
}

Friday, December 7, 2012

How to write a UDP socket program in Qt


We have seen how to write a simple UDP socket program in C (Linux) in the earlier post..

Now we are going to learn how to write a simple UDP socket program in Qt..

In the following example we used to send and receive data on localhost address.
And the readyRead() signal and slot is used whenever the data receives on the given socket (i.e. socket reday for read data), the control goes to slot method.
writeDatagram() and readDatagram() API's used for sending and receiving data on the given udp socket.

Before starting the Program to write, create a Project using following steps 
  • open Qt creator
  • Create Project
  • Select Other Project in Projects tab then select Qt Console Application, then click on choose...
  • Then give the Project name
  • And then click on Next, Finish in the following dialog boxes
  • Then open the .pro file and write QT += network 
  • To create the cpp file Right click on the project then choose Add new

Thursday, October 4, 2012

QGraphicsView Example Program by inheriting QGraphicsView class


In the prevoius qgraphicsview example we created the view (QgraphicsView) and placed the scene in this view.Now in this example we created a class and inherit this class from QGraphicsView class, so the class being inherited acts like view and we will set the scene for this inherited class.

Check the example program and the output below...
  • graphics.h

#ifndef GRAPHICS_H
#define GRAPHICS_H
#include <QtGui/QWidget>
#include <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <QGraphicsView>
class graphics : public QGraphicsView
{
    Q_OBJECT
    QGraphicsScene *scene;
    QGraphicsEllipseItem *itm_ellipse;
    QGraphicsTextItem *itm_txt;
    QGraphicsLineItem *itm_line;
    QGraphicsPolygonItem *itm_poly;
    
public:
    graphics(QWidget *parent = 0);
    ~graphics();
};
#endif // GRAPHICS_H

Monday, October 1, 2012

QgraphicsView simple example program


Here is a simple QgraphicsView example program in Qt.

In this example we created a scene (QGraphicsScene) and placed all the items  in this scene by specifying the scene object while creating the graphics item.Andat last we created the view (QGraphicsView) and scene is placed in this view by using setScene() method of QgraphicsView.



#include <QtGui/QApplication>
#include<QGraphicsView>
#include<QGraphicsScene>
#include<QGraphicsSimpleTextItem>
#include<QGraphicsRectItem>
#include<QGraphicsEllipseItem>
#include<QVector>
#include<QGraphicsPolygonItem>

int main( int argc, char **argv )
{
    QApplication app( argc, argv );
    QGraphicsScene scene( QRect( -100, -100, 400, 400 ) );
    scene.setBackgroundBrush(Qt::cyan);


    QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(
                "technoyouth10", 0, &scene );
    textItem->setPos( -50, -50 );
    textItem->setFlag(QGraphicsItem::ItemIsMovable);


    QGraphicsRectItem *rectItem = new QGraphicsRectItem(
                QRect( 0, -0, 200, 40 ), 0, &scene );
    rectItem->setPen( QPen( Qt::red, 3, Qt::DashDotLine ) );
    rectItem->setBrush( Qt::green );


    QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(
                QRect( 80, -80, 100, 75 ),
                0, &scene );
    ellipseItem->setPen( QPen(Qt::yellow) );
    ellipseItem->setBrush( Qt::magenta );


    QVector<QPointF> points;
    points << QPointF( 20, 50 ) << QPointF( 80, 50 ) << QPointF( 110, 80 )
           << QPointF( 50, 130 )<< QPointF( -10, 80 ) ;
    QGraphicsPolygonItem *polygonItem = new QGraphicsPolygonItem(
                QPolygonF( points ), 0, &scene );
    polygonItem->setPen( QPen(Qt::red) );
    polygonItem->setBrush( Qt::yellow );

    QGraphicsView view;
    view.setScene( &scene );
    view.show();
    return app.exec();

}



Output:
Run the program by pressing Ctrl+r...