Reader small image

You're reading from  Qt 6 C++ GUI Programming Cookbook - Third Edition

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781805122630
Edition3rd Edition
Right arrow
Author (1)
Lee Zhi Eng
Lee Zhi Eng
author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng

Right arrow

Conversion Library

Data kept within our computer environment is encoded in a variety of ways. Sometimes, it can be used directly for a certain purpose; other times, it needs to be converted into another format in order to fit the context of the task. The process of converting data from one format into another also varies depending on the source format as well as the target format.

Sometimes, the process can be very complex, especially when dealing with data that is feature-rich and sensitive, such as image or video conversion. Even a small error during the conversion process may render a file unusable.

This chapter will cover the following recipes:

  • Converting data
  • Converting images
  • Converting videos
  • Converting currency

Technical requirements

The technical requirements for this chapter include Qt 6.6.1 MinGW-64-bit and Qt Creator 12.0.2. All the code used in this chapter can be downloaded from the following GitHub repository: https://github.com/PacktPublishing/QT6-C-GUI-Programming-Cookbook---Third-Edition-/tree/main/Chapter11.

Converting data

Qt provides a set of classes and functions for easily converting between different types of data. This makes Qt more than just a GUI library; it is a complete platform for software development. The QVariant class, which we will use in the following example, makes Qt even more flexible and powerful compared to the similar conversion functionalities provided by the C++ standard library.

How to do it…

Let’s learn how to convert various data types in Qt by following these steps:

  1. Open Qt Creator and create a new Qt Console Application project by going to File | New Project…:
Figure 11.1 – Create a Qt Console Application project

Figure 11.1 – Create a Qt Console Application project

  1. Open main.cpp and add the following headers to it:
    #include <QCoreApplication>
    #include <QDebug>
    #include <QtMath>
    #include <QDateTime>
    #include <QTextCodec>
    #include <iostream>
  2. In the main() function, add the following code to convert...

Converting images

In this section, we will learn how to build a simple image converter that converts an image from one format into another. Qt supports reading and writing different types of image formats, and this support comes in the form of external DLL files due to licensing issues.

However, you don’t have to worry about that because as long as you include those DLL files in your project, it will work seamlessly across different formats. There are certain formats that only support reading and not writing, and some that support both.

Note

You can check out the full details about converting images at http://doc.qt.io/qt-6/qtimageformats-index.html.

How to do it…

Qt’s built-in image libraries make image conversion really simple:

  1. Open Qt Creator and create a new Qt Widgets Application project.
  2. Open mainwindow.ui and add a line edit and push button to the canvas to select image files, a combo box to select the desired file format, and...

Converting videos

In this recipe, we will create a simple video converter using Qt and FFmpeg, a leading multimedia framework that is free and open source. Although Qt supports playing video files through its widget, it does not currently support video conversion. Fear not! You can still achieve the same goal by making your program cooperate with another standalone program, through the QProcess class provided by Qt.

How to do it…

Let’s make a simple video converter with the following steps:

  1. Download FFmpeg (a static package) from http://ffmpeg.zeranoe.com/builds and extract the contents to your preferred location – for example, C:/FFmpeg/.
  2. Open Qt Creator and create a new Qt Widgets Application project by going to File | New Project....
  3. Open mainwindow.ui – we’re going to work on the program’s user interface. Its UI is very similar to the previous example, except that we add an extra text-edit widget to the canvas, just...

Converting currency

In this example, we will learn how to create a simple currency converter using Qt, with the help of an external service provider called Fixer.io.

How to do it…

Make yourself a currency converter by following these simple steps:

  1. Open Qt Creator and create a new Qt Widgets Application project from File | New Project....
  2. Open the project file (.pro) and add the network module to our project:
    QT += core gui network
  3. Open mainwindow.ui and remove the menu bar, toolbar, and status bar from the UI.
  4. Add three horizontal layouts, a horizontal line, and a push button to the canvas. Left-click on the canvas, and continue by clicking the Lay Out Vertically button on top of the canvas. Change the label of the push button to Convert. The UI should look something like this:
Figure 11.10 – Place three vertical layouts above the Convert button

Figure 11.10 – Place three vertical layouts above the Convert button

  1. Add two labels to the top layout, and set the text of the left...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Qt 6 C++ GUI Programming Cookbook - Third Edition
Published in: Apr 2024Publisher: PacktISBN-13: 9781805122630
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng