C++ - Qt Framework - Using QDataStream

The QDataStream class allows to stock data from an input and give back this data to an output.

Quite interesting when you want, for example, to transfer data from a computer to another.

Let’s see that in this QDataStream tutorial.

First of all

In this tutorial we’re going to create a custom object (BadprogDS) and transfer it to another one.

It’ll involve overload stream operators such as "<<" and ">>".

The QDataStream takes in general 2 parameters:

  1. QByteArray *
  2. QIODevice::OpenMode

The first stocks the data to transfer and the second is a flag to know if we want to write or read from this data.

The QByteArray is made of bytes, so we can pass classic data types inside.

The use of a QDataStream with a QByteArray is then very useful because we don’t have to worry about the piece of data.

Indeed each time we add a data in this QDataStream it will be added one index after the previous in the list.

Very easy to handle therefore.

This mechanism can be subdivided into another process by adding a QBuffer.

So instead of having 2 parameters in the QDataStream, you’ll have only one.

But the QBuffer will take a QByteArray as parameter before setting the buffer flag.

Finally you’ll have to add this buffer as the QDataStream parameter.

You’ll have exactly the same result.

Let’s see with some lines of code.

Code

main.cpp

#include <QtCore/QCoreApplication>
#include <QByteArray>
#include <QDataStream>
#include <QBuffer>

#include "BadprogDS.h"
#include <memory>
#include <iostream>

// BadproG.com

// ============================================================================
// This dear main
// ============================================================================
int main(int argc, char *argv[])
{
// init
QCoreApplication a(argc, argv);
 
// creating an object
std::unique_ptr<BadprogDS> badprogDS1 = std::make_unique<BadprogDS>(1337, "Hello World! :D");
 
 
// building DataStream
QByteArray byteArray1;
QDataStream streamGotFromObject1(&byteArray1, QIODevice::WriteOnly);
 
// checking everything is OK
std::cout << "Sending message:" << std::endl;
std::cout << "Id1 = " << badprogDS1->getId() << std::endl;
std::cout << "Name1 = " << badprogDS1->getName().toStdString() << std::endl << std::endl;
streamGotFromObject1 << *badprogDS1;
 
//////////////////////////////////
//////////////////////////////////
 
// creating another object
BadprogDS badprogDS2;
 
// with buffer
QBuffer buffer(&byteArray1);
buffer.open(QIODevice::ReadOnly);
QDataStream streamSentToObject(&buffer);
 
// checking everything is OK
streamSentToObject >> badprogDS2;
std::cout << "Receiving message:" << std::endl;
std::cout << "Id2 = " << badprogDS2.getId() << std::endl;
std::cout << "Name2 = " << badprogDS2.getName().toStdString() << std::endl;
 
//
return a.exec();
}

 

BadprogDS.cpp

#include "BadprogDS.h"
 
// BadproG.com
 
// ============================================================================
//
// ============================================================================
BadprogDS::BadprogDS() {
}
 
// ============================================================================
//
// ============================================================================
BadprogDS::BadprogDS(int id, QString name) : _id(id), _name(name) {
}
 
// ============================================================================
//
// ============================================================================
BadprogDS::~BadprogDS() {
}
 
// ============================================================================
//
// ============================================================================
int BadprogDS::getId() const {
return _id;
}
 
// ============================================================================
//
// ============================================================================
QString BadprogDS::getName() const {
return _name;
}
 
// ============================================================================
//
// ============================================================================
void BadprogDS::setId(int id) {
_id = id;
}
 
// ============================================================================
//
// ============================================================================
void BadprogDS::setName(QString name) {
_name = name;
}

 

BadprogDS.h

#ifndef _BADPROG_BADPROG_DS_H_
#define _BADPROG_BADPROG_DS_H_
 
#include <QString>
#include <QDataStream>
 
// BadproG.com
 
class BadprogDS {
// ============================================================================
//
// ============================================================================
public:
BadprogDS();
BadprogDS(int id, QString name);
~BadprogDS();
 
// ============================================================================
//
// ============================================================================
public:
//
int getId() const;
QString getName() const;
//
void setId(int id);
void setName(QString name);
 
// ============================================================================
//
// ============================================================================
private:
int _id;
QString _name;
};
 
// ============================================================================
//
// ============================================================================
inline QDataStream &operator <<(QDataStream &streamGotFromObject, const BadprogDS &badprogDS) {
//
streamGotFromObject << badprogDS.getId() << badprogDS.getName();
//
return streamGotFromObject;
}
 
// ============================================================================
//
// ============================================================================
inline QDataStream &operator >>(QDataStream &streamSentToObject, BadprogDS &badprogDS) {
//
int id;
QString name;
//
streamSentToObject >> id >> name;
badprogDS.setId(id);
badprogDS.setName(name);
//
return streamSentToObject;
}
 
#endif // !_BADPROG_BADPROG_DS_H_

 

Result

Sending message:
Id1 = 1337
Name1 = Hello World! :D

Receiving message:
Id2 = 1337
Name2 = Hello World! :D

Conclusion

A good start to begin communication between many devices.

Great job, you did it. cool

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.