There are scenarios where you would need to find the sum or the average of all values in a QByteArray. This can specially happen if you are trying to send data using COM ports to devices that require the average or sum (or a similar combination) of all elements in order to check the integrity of data.
Continue reading “Quick Way to Find the Sum or Average of All Elements in QByteArray”How to Convert Data to/from Base64 in Qt
Qt Framework implements many convenience functions inside QByteArray for quickly handling of raw data. One of the widely used encoding methods is Base64 and if you’re with Qt, you can easily convert any string (QString) to/from Base64.
Continue reading “How to Convert Data to/from Base64 in Qt”How to Convert QBuffer to QString
You can convert QBuffer to QString using buffer function of the QBuffer. Here is how you can do it.
QBuffer b;
b.open(QBuffer::ReadWrite);
b.write("http://amin-ahmadi.com");
QString str = b.buffer();
qDebug() << str; // "http://amin-ahmadi.com"
b.close();
How to Create and Use Buffers in Qt
Qt Framework includes a class called QBuffer that can be used to access a QByteArray (internal QByteArray to be precise) as if it is a file. So you can use Stream readers and writers to access data in it similar to a QFile.
Continue reading “How to Create and Use Buffers in Qt”Fix ModSecurity Issues in Qt Network Module Download Functionality
You might have come across the following (Not Acceptable) error when trying to download files from some websites using Qt. This error message is generated by ModSecurity and it usually means that for some reason the web server does not allow downloading of the file using the method you are trying. In my case it was because of the User-Agent Header set to a value not allowed by the server. So here is how I fixed it:
Continue reading “Fix ModSecurity Issues in Qt Network Module Download Functionality”How to Create an Empty QImage to Play Around With
You can create a QPixmap and convert it to QImage if you just want to have a QImage for modification and manipulation. Here, you can use the following code to achieve this:
int width = 100;
int height = 200;
QImage image = QPixmap(width, height).toImage();
How to Change Hue Level of an Image in Qt
Qt offers convenient classes for basic image processing and pixel manipulation. In this article I am going to describe how you can change the hue level of a whole image using QImage class in Qt.
Continue reading “How to Change Hue Level of an Image in Qt”How to Reduce Your SQLite Database File Size Using Qt
Below is what I do for clearing and compressing my SQLite database files in Qt. Usually when you delete some entries from a SQLite database, it does not get deleted physically but instead it just gets marked as deleted and is not retrieved in queries. The good thing about this is that it works fast. But the obvious downside is the fact that the free space is not released. To release the space occupied by deleted files you need to do the following:
Continue reading “How to Reduce Your SQLite Database File Size Using Qt”OpenGL in Qt for Beginners
In this post I am going to share the source code and explain a very simple example project that can be used as a starting point for anyone who wants to begin using OpenGL in Qt. As it is seen in the picture, this program draws a single place on the screen and allows you to rotate it in all directions.
Continue reading “OpenGL in Qt for Beginners”How to Split a QString by White Spaces
You need to use a QRegExp to achieve this. “\s” means any white space character and “+” means any number of that pattern. So you can use the following line to find any number of consecutive white spaces and split you string:
line.split(QRegExp("\s+"))