Tips on Implementing Blazing Fast Qt Applications

Qt Logo

Qt Logo

By Chongchen Saelee

I want to share with you my personal tips on how to implement blazing fast Qt applications. I’m just a hobbyist programmer, but I still understand the importance of programming principles such as KISS (keep it simple-stupid), OOP (object-oriented programming), optimization, utility, and user-friendliness. In the end, you’re writing a program that people want to use. Therefore, if even you can’t use the program, then you’ve done it wrong.

So the first thing you want to do is pick your poison. I started programming with high-level program languages such as Visual Basic and HTML. Eventually I learned enough intermediate knowledge and experience that I wasn’t able to accomplish what I wanted. This led me to c++ and really opened my eyes to how programming and computer science really worked. Now if I wanted to really get to the actual computer science, I’d learn c or machine language, but I’m not that devoted. c++ is more than suitable for the non-professional. I would even say that’s the minimum for anyone interested in making any program of any sort, just because it’s more robust. Moving from Visual Basic to c++ can be hard.

  1. Use c++. I’m currently using Qt. Why c++? Because it’s optimized for speed.
  2. KISS (Keep it simple-stupid). I also interpret this concept by another concept called “brute force” programming. The idea is that a computer is just a machine that does what we humans tell it to do. Therefore you shouldn’t try to “talk” to a computer like another human with fancy language and concepts. Talk to a computer like a computer. For example, don’t do this: “Add(1+1)” when you can do this “1+1”. If the computer already has internal function to add objects (arithmetically or abstractly), then just code it inline. Writing out an Add() function is a convenience for yourself, but the computer doesn’t know the difference and couldn’t care less if you can read the code better.
  3. OOP (object-oriented programming) keeps things organized. It’s not a requirement, it’s just a technique. You want the code and structures to be setup in a way that you know where everything is. This isn’t limited to just c++. Just do it.
  4. Code for utility first, fancy graphics last. At the heart of any program is utility. Make sure your core functionality is implemented first. When you’re satisfied with the utility, that your program does at least what it’s suppose to do, then you can add another layer on top of that so it’s more user-friendly and easier to use. Nothing’s wrong with command-line programs, but there’s also definitely nothing wrong with a more user-friendly graphical interface program.
  5. Repaint graphics less. Painting graphics to the output device takes a lot of power. You should repaint only when necessary. This includes changing graphics that aren’t painted to the output too because it requires CPU or GPU cycles to calculate and place new data, which eats up time.
  6. Avoid recursive loops. But if you must use a recursive loop, make sure there is a way out.
  7. Make sure you’re code is error-free as possible. In c++, it’s known for case-sensitive syntax and strongly-typed language. But sometimes the error has to do with logic or just outright human error. The smoother it runs, obviously faster it runs.
  8. Use QPixmap for simple, read-only images. Use QImage for any kind of image manipulation, such as scaling, pixel-level, etc.
  9. Avoid mouseDoubleClickEvent(QMouseEvent *) if you can. By design, it repeats mousePressEvent(QMouseEvent *) and mouseReleaseEvent(QMouseEvent *) twice to accomplish a double-click, logically. So if you have heavy processing in those event handlers already, expect to see catching double click event to take exponentially longer to happen. Thus, it’s probably best to implement your own double click functionality.

That’s the basics of implementing faster Qt programs. I could go into more details, but your program is probably not the same as mine, so you have to go hunting and pecking for very specific classes and features to use. But otherwise, keep these tips in mind and you’ll save yourself a lot of hair-pulling later. Hope it helps!

Leave a Reply