Reader small image

You're reading from  Hands-On Embedded Programming with Qt

Product typeBook
Published inJul 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789952063
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
John Werner
John Werner
author image
John Werner

John Werner is an internationally published author, engineer, consultant, and conference speaker with more than 15 years' experience. He has earned patents for inventions ranging from Inkjet printers to automotive ignition systems. John was one of the early users of Qt on QNX and contributed to the early knowledge of how to make it work. He is a contributor to the Qt-related information on Stack Exchange. He is currently a senior software engineer, specializing in Qt development, for Caliber Imaging & Diagnostics.
Read more about John Werner

Right arrow

Writing Your First Qt Application

Now that we have built our target and set up our host development system, it is time to get down to actually writing a Qt application!

In this chapter, we will develop, test, and debug a Qt application using just the host computer. Qt is a cross-platform framework—we can write, test, and debug major portions of the code on the host.

You can work through this chapter without target hardware and without having completed cross-compilation of Qt.

The following topics will be covered in this chapter:

  • Qt Creator—the Qt IDE
  • Greetings from Qt—your first Qt application
  • Who are you?—adding features to the code
  • Fixing a mistake—testing and debugging with Qt Creator
  • Digging deeper—a closer look at some topics

By the time you have finished this chapter, you should have learned how to do the following:

  • Start Qt...

Technical requirements

This chapter assumes that you have successfully installed Qt 5.12.0 on the host PC in ~/Qt/5.12.0 as described in Chapter 1, Setting Up the Environment, and have a basic understanding of C++ and its techniques.

You do not need to have completed the target setup or have built Qt 5.12.0 to achieve the objectives laid down in this chapter.

Completed code for the application in this chapter can be found in the GitHub repository at: https://github.com/PacktPublishing/Hands-On-Embedded-Programming-with-Qt/tree/master/Chapter02.

Introducing Qt Creator

There is a lot to Qt Creator, but for now, I just want to give you an overview. We will devote a section later in this book to looking at some of Qt Creator's advanced capabilities.

Qt Creator is the cross-platform IDE for developing Qt applications. It supports code editing and development in C++, JavaScript, QML, XML, HTML, and other languages. It can create and edit QMake project (.pro) files for both simple and complex projects. You can use it to visually design user interfaces. It works directly with version control systems such as Mecurial, Git, and SVN. It executes compilation, and it even does source-level debugging. It also does static C++ code analysis with Clang!

It is a very complete, fast, IDE written in Qt for developing Qt GUI and console applications along with Qt plugins and libraries.

With such a powerful IDE, you would expect that...

Greetings from Qt

Now that we have looked at some of the basics of Qt Creator, let's use it to create our first program!

There is a long tradition in programming that the first program every programming class uses is Hello World! While I love traditions, I also love mixing things up a bit, so our first program will be Greetings from Qt! Obviously, our program will be an entirely different program from Hello World!

We will start by creating a project for Qt, and then we will learn about the text edit screen and how to design a simple UI. Of course, we will need to learn how to run our code. Finally, we will learn how to make sure our code stays safe as we develop and change it. The latter will become very important once we start working on our BigProject, something that will span most of this book.

...

Who are you?

Now that we have created a project, designed a simple UI, built and run the application, and committed our code to a local repository, let's add to the project. As noted before, there really isn't much to make our little application interesting. Why don't we add something to it?

We will start by adding a place for the user to enter their name and then use the name to directly address them in the text.

Adding a place to type your name

For our next trick, let's see whether we can get the application to address us by name. We should have a line editing widget with a label saying My name is:.

Before you look further down, why don't you try adding the elements to the design yourself?

The following...

Fixing a mistake

Debugging mistakes is part of developing software. It seems like I have spent at least half of my software development career debugging mistakes in code. I'd like to say that it was other people's code, but it has mostly been in mine. Luckily, I have caught most of them while I was developing the code, but occasionally some slipped past.

Qt Creator comes with built-in C++ and QML (a Qt language) debugging support. Now is a good time to use it to find the mistake in our code.

Launching our application in the Qt Creator debugger

While developing an application in Qt Creator, it is very easy to compile and launch it in the Qt Creator debugger. Like running the application, there are several methods...

Digging deeper

In writing this chapter, I glossed over a few things for the sake of keeping the flow of the lesson. Now is a good time to look at some of them in more depth.

In particular, I want to take a little bit of time to explain the code for the push button, what these slots and signals are, and to pontificate on testing.

Explaining the push button code

The on_pushButton_clicked() handles the user clicking on pushbutton. Now, we will go through the method line by line to understand what is happening:

void MainWindow::on_pushButton_clicked()
{

The naming of this method enables the Qt GUI to automatically connect the signal emitted by the button when it is pressed to a slot in our main window that will handle it. The...

Summary

Congratulations! You made it! This was a lot of material to cover, but it really forms the basis of working with Qt Creator and developing a widgets-based application. It also got you to write and debug a very simple Qt application.

As we worked our way through this chapter, we covered a fair amount of material. We began by learning how to start Qt Creator. We then learned how to navigate it.

Having learned how to get around Qt Creator, we started to write our first Qt Widget Application. First, we created a Qt Widget project, and then we created the display form. After that, we improved on the application and found a bug in the code during testing. I had not planned to teach debugging yet, but since I made a mistake when I wrote the code, we learned how to debug using Qt Creator and use the help system (like I did) to figure out what the problem was.

In working through...

Questions

  1. Can Qt Creator interface to version control systems? If so, which ones?
  2. Give two ways you can create a new project in Qt Creator.
  3. How do you add a C++ class to a Qt Project?
  4. Do Qt and Qt Creator only support GUI development?
  5. What resources does Qt Creator provide for learning more about a Qt topic?
  6. What is the basic process for adding elements to a design?
  7. Describe ways in which can you compile and run your application.
  8. How can you start debugging an application being developed in Qt Creator.
  9. How do you set a breakpoint in the debugger?
  10. How often should we test our code, even informally? Why?

Further reading

You may want to look at the following Packt book for more information on the topics covered in this chapter:

  • Application Development with Qt Creator - Second Edition, by Ray Rischpater
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Embedded Programming with Qt
Published in: Jul 2019Publisher: PacktISBN-13: 9781789952063
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
John Werner

John Werner is an internationally published author, engineer, consultant, and conference speaker with more than 15 years' experience. He has earned patents for inventions ranging from Inkjet printers to automotive ignition systems. John was one of the early users of Qt on QNX and contributed to the early knowledge of how to make it work. He is a contributor to the Qt-related information on Stack Exchange. He is currently a senior software engineer, specializing in Qt development, for Caliber Imaging & Diagnostics.
Read more about John Werner