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

Qt Best Practices

We have reached the last chapter of this book!

As we have worked our way through the chapters in this book, we have fulfilled 23 requirements for an imaginary product. In the process, we had a chance to learn the Qt Way by implementing, testing, and debugging code.

In the last two chapters, we left the project behind, but I hope that, as you learned about more Qt capabilities, you have thought of ways you could not only apply them to the project, but also to future projects you might have a chance to work on. The nice part is that you have a premade Target system that you can use. As I completed this book, I already thought of the next thing I want to build with my Raspberry Pi, Sense HAT, and Qt—an in-car performance monitor that can show and record how hard the car is turning, accelerating, and braking when I am competing in motorsports. I know there...

Technical requirements

Like all of the chapters in this book, it is expected that you have worked through the previous chapters.

This chapter will be primarily about information sharing, but where there are code snippets, you should feel free to try them both on your Host and Target. For me, I don't really learn about something until I have tried it myself.

Understanding the Why? of Qt

Several decades ago, as a young teenager, I had a chance to watch a very interesting video series called You Are What You Were When. The series was made to teach managers how to understand how the people on their teams thought and behaved. Beyond watching the teacher work his way around a room filling every white board that surrounded it with notes and drawing, I found the idea incredibly powerful—everyone (and everything) is shaped by where they came from.

With that in mind, let's look at where Qt came from so that we can understand the Qt we can see today.

The time was 1991. Personal computers were just starting to be something you might see in a regular office. Development of graphical interfaces was incredibly fragmented. There were competing display technologies, all with their own programming methods.

The C++ language was fragmented...

Choosing between C++ features and the Qt Way

Throughout this book, I have pretty much stuck to teaching you the Qt Way of doing things. Does that mean it is right for all cases? Well, that depends.

If you are finding it hard to do something that should be simple in Qt, then (a) you aren't doing it the right (Qt) way or (b) you really shouldn't be doing it at all.

We saw this in the last chapter when we learned about Qt Concurrent. I learned about threading in Qt before Qt Concurrent was developed. As I learned about Qt Concurrent, I discovered that it was easier to do certain tasks using Qt Concurrent rather than QThreads. Still, I tend to resort to QThread and QThreadPool out of habit.

Should I use the pthread library or QThread?

It's a valid question that I have seen more than once, so let's look at it and see whether we can get some guidance that we can...

Increasing efficiency in Qt

Originally, I was going to call this section Writing faster Qt code, but as I was developing the outline, I realized that what I was really talking about is writing efficient Qt code. Let me explain.

When I was young, unmarried, and living with my parents, I had money to play with so that I could build faster cars. One time, I decided to make the motor of my car more powerful without having to increase its displacement. When I was finished, the car had 50% more horsepower, and when I used the horsepower, it went through fuel 50% faster.

However, I also got something else out of this. I took the car on a long (several hour) expressway trip. What I found was that the fuel economy of the car (when driven like a sane person) went up by 50%. Why? Because in order to get more power out of the engine, I have to increase the efficiency of the engine. I used...

Summary

In this final chapter, we started by trying to understand why Qt has its own ways for doing some things that are now considered standard parts of modern C++11 or other libraries. That led us to trying to come up with some guidelines on when we should use Qt or other libraries. From there, we discussed how to increase the efficiency of our Qt code and found that some methods have applications outside of just Qt programming.

While this is the end of the teaching section of this book, I sincerely hope that this is not the end of your learning when it comes to how to develop embedded applications using Qt. We have only hit the high level of some of the topics that could be discussed. There are many things I wish I could have added to the discussion and discussed more in depth since the technology is continuing to grow. At the time of writing, Qt 6 is actively being developed...

Questions

  1. In a Qt application, is it better to use std::string or QString?
  2. Why does Qt have its own QSharedPointer class?
  3. Efficiency-wise, is it better to pass by reference or value?
  4. If you were to pass by reference, but are not going to modify what was referenced, what decorations should you use in the function prototype?
  5. Why can QString::replace() negatively affect the performance of an application?
  6. Why do I like code reviews, and why should you like them too?
  7. What aid can Qt Creator offer in helping you to create more efficient code?
  8. Now that you have finished the learning section of this book, what should you do?

Further reading

What is it you want to learn next?

Packt has a variety of books on many subjects. In fact, while writing this book, I purchased one on CUDA programming for an image processing project I was working on. Here are some books that caught my attention as a learner/teacher of C++ and Qt:

  • C++17 By Example, by Stefan Björnander
  • Hands-On Embedded Programming with C++17, by Maya Posch
  • Computer Vision with OpenCV 3 and Qt5, by Amin Ahmadi Tazehkandi
  • Raspberry Pi 3 Home Automation Projects, by Ruben Oliva Ramos and Shantanu Bhadoria

Find a book about something you are interested, get it, and learn something!

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