Tuesday, May 31, 2016

New method QHostAddress::isMulticast() in Qt 5.6

Among new features of Qt 5.6 new method was added for QHostAddress:


This bool method returns true if IPv4 or IPv6 address is multicast.

From wikipedia a multicast address is a logical identifier for a group of hosts in a network available to process datagrams or frames intended to be multicast for a network service.

Here is the test program which shows the output for different IPv4 addresses:

#include <QCoreApplication> 
#include <QDebug>
#include <QHostAddress>

int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    QHostAddress ipv4false("");
    qDebug() << ipv4false.isMulticast(); // false
    // The All Hosts multicast group addresses all 
    // hosts on the same network segment.
    QHostAddress ipv4true("");
    qDebug() << ipv4true.isMulticast(); // true

    return a.exec();

It works the same way for IPv6 addresses.

Tuesday, May 24, 2016

QtWebEngine: Printing to PDF

Among new features in Qt 5.7 for QtWebEngine there is printing to PDF. There are several samples which are available for QtWebEngine you can start from this folder in your Qt installation:


We will look at demobrowser sample. Let's start from BrowserMainWindow::slotFilePrintToPDF(). Unfortunately Qt still has problem with QPrintDialog not working on Windows described here:


So for sample purposes we will remove QPrintDialog and just hardcode filename:

void BrowserMainWindow::slotFilePrintToPDF()
    if (!currentTab())

    QPrinter printer;
    if (printer.outputFileName().isEmpty() || 
    m_printerOutputFileName = printer.outputFileName();
                                     invoke(this, &BrowserMainWindow::slotHandlePdfPrinted));
#endif // QT_NO_PRINTER

void BrowserMainWindow::slotHandlePdfPrinted(const QByteArray& result)
    if (!result.size())

    QFile file(m_printerOutputFileName);
    if (!file.open(QFile::WriteOnly))
    file.write(result.data(), result.size());

So what was actually added in Qt 5.7?

Two overloads of printToPdf() function for QWebEnginePage are here.

They allow to render page content into a PDF document.

Monday, May 16, 2016


In Qt 5.6 there is a new feature in Qt Core: QDir::listSeparator

    QChar QDir::listSeparator

It returns ';' on Windows and ':' on Unix systems, this is native path list separator, i.e symbol which separates paths in a list.

The aim of introducing this QDir::listSeparator is to replace possible ifdefs in your code and maintain good-looking code on various platforms.