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


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

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+"))