Questions & Answers from Qt for Beginners Part 5 - Ask the Experts

Questions & Answers from Qt for Beginners Part 5 - Ask the Experts

By Jeff Tranter

Download the entire webinar series.

In QML, I have a Text item with wrapMode: Text.Wrap but the text isn't wrapping. What am I doing wrong?

You need to set the width of the Text element, either directly using the width: property or by using anchors. Please refer to slide 3 of the presentation deck for some sample code that you can try.

Would it be possible to go into more detail with kit configuration for cross-platform development and remote deployment? Preferably with device configuration and deployment demonstration. To demonstrate the concept you could use any device, but I have a particular interest in a Raspberry Pi running Linux, and a Surface Pro running Windows 10.

That is a good suggestion but it could easily be the topic of an entire webinar in itself. We'll see if we can write a blog post that steps through how to configure Qt Creator for the Raspberry Pi, as that is a common and inexpensive platform. For a Surface Pro, I would imagine most people would develop natively on it (probably with a keyboard and mouse) so it would work like any desktop and Qt Creator should work mostly out of the box. I've used a Surface Pro this way.

I am having difficulty connecting to PostgreSQL from Qt. I looked at forums, followed their recommendations and still get "Not Loaded".There's a Russian and a Spanish step-by-step guide, is there something similar in English? I'm using Windows 7 x64 and MingW. I can connect directly using pgAdmin3, Valentina Studio, MS-Visual Studio, SQL Studio for PSQL, LibreOffice .

PostgreSQL is supported by Qt. I would start with one of the Qt database demos. Is the database on the same machine or remote? Hard to say what the issue could be without seeing the code and getting access to their database.

Should I use the QML Designer in Qt Creator? I heard it was not ready for production use.

Early versions of the QML Designer were pretty rough, but as of Qt Creator 4.0 it seems to be a lot more powerful and stable. We haven't had a lot of experience with it yet as Qt Creator 4.0 has only been out for a short time, but some of our developers are now starting to use it rather than hand writing all QML code as they did in the past.

I would like to develop a simple app to open up a URL to a website and then get information from the website that will populate some fields in my app. What is the easiest way to do this? Are there Qt Quick controls that can assist?

I would look at using the Qt Network module, to pull the relevant web pages from the server. Then parse the received HTML using string and/or regular expression classes to get the information you need. You might want to look at the Qt "Network Download Example". I don't think you would want to use QtWebKit or QtWebEngine. You could use Qt's XML support to parse the HTML, but only if you are sure that that the web pages are always valid XML/HTML, which might not be the case. There are some third party libraries you could use to parse HTML that might help if the web pages are very complex. Displaying the information is a different issue. You may want to use the Qt Quick Controls to develop your UX if you are using QML. It will also depend on whether this is a desktop or touchscreen application.

Is it possible to place a button widget over top of an QOpenGLWidget to make a UX comparable to Google maps? The button widget would be like the stationary user account button in Google maps while the map would live in the QOpenGLWidget. Ideally the button would be transparent. This person (…) did something similar by making the button a separate window that tracked the main window.

This is a little too advanced for this Qt For Beginners webinar. We could refer you to one of our developers and point you in the right direction to get started, or we'd be happy to develop some or all of the application as a paid consulting effort.

What are some of the pros and cons of using Qt resources?

- Makes binary self-contained and avoids the need to ship as many files
- Avoids need to install files in platform-specific locations
- Can support different files for different platforms, screen sizes, languages, etc.
- Provides some protection against end users seeing your QML source files
- Easy to use and integrated into qmake and Qt Creator
- Makes binaries larger
- Hard to update data, like translations, if they are in resources
- Data is not encrypted and can be extracted

Can you give me some guidance on whether to use qmake, qbs, or cmake as my build system?

Some of it comes down to personal preference and experience, but I would suggest in general: For small to medium sized projects, use qmake as it is simple and stable. For large projects, particularly those with non-Qt components, you should look at cmake. If you are interested in what may eventually be the replacement for qmake, take a look at qbs.

Can I use QML to create an editable TableView that uses SQLite as the backend datastore? If so, how do I do this? If not, is there another technique I can use to achieve an editable table with a database backend?

Without a connection made in C++ to the SQLite server? No, you will need to open a connection to act as an intermediary between the QML and the server. The connection provides the initial values and, when changes are made via QML, would send queries to commit changes made to the datastore.

WebEngine is not available for MinGW on Windows. Will WebEngine support for MinGW be available soon for 5.7 ?

Unfortunately the issue is that the Chromium engine that QtWebEngine uses as its web rendering engine does not support being built with the MinGW compiler. Fixing this would have to be done by the Chromium maintainers, and it seems that they are not interested in doing this. Chromium is huge, so it would be a significant effort. So this is not likely to happen any time soon. I can offer a couple of possible solutions. If the reason to use the MinGW compiler was due to cost, Visual Studio Express is available for free. If you want to go back to QtWebKit rather than QtWEbEngine, there is now a project to continue to maintain it. Some recent news can be found here:

How do you handle QMLTextEdit that anchored to bottom of screen on iOS and Android devices?

We would need a bit more information about what the issue is with TextEdit to help you. Are you having issues anchoring the element or is it giving you issues because it's anchored to the bottom of the screen?

I have heard that widgets are obsolete? Should I not be using them?

No, widgets are not obsolete. They are a mature module in Qt where few updates are expected in the forseeable future. They are applicable for desktop applications and can be used on embedded as well. With the computer industry seeing most growth in mobile, tablet, and embedded systems, QML tends to get the most attention.

How can I contribute to the Qt project?

Originally Qt was developed only by the "Trolls," employees of Trolltech. Just before Qt was acquired by Digia (now The Qt Company) it moved to an open governance process where anyone can contribute to Qt. If you want to do more than submit bugs, for example, you can submit source code patches into the Gerrit system, where they will be reviewed by other Qt developers, and either accepted or rejected for inclusion in a future release. Part of the review is running the changes in the CI system to ensure they compile and all existing automated unit tests pass. The basic process is to create an account on Gerrit, review and agree to the Qt Project's contribution agreement, clone Qt, submit your changes to Gerrit, and wait for review comments. If accepted, you submit the patch. It may take some back and forth discussion with the reviewers and several revisions before your patch is deemed to be acceptable. This wiki article covers the process in more detail:

Are all Qt classes derived from QObject?

Surprisingly, not all of Qt's classes are derived QObject. QString, for instance, does not inherit from QObject nor does QVariant. QEvent is another. These non-QObject classes tend to be ones that are lighter weight and don't need to support signals and slots (like QString, for example).

Are there virtual slots? How about virtual signals?

Given that a slot boils down to a method, you can define one to be virtual, allowing derived classes to override its definition. Since the code for signals is generated by the moc tool, it doesn't really make sense to try to declare a signal as virtual.

Qt promises binary compatibility between releases. What does that mean?

A library is binary compatible if a program linked dynamically to a former version of the library continues running with newer versions of the library without the need to recompile. Qt maintains binary compatibility across major releases of Qt (e.g. Qt 5). Minor releases (e.g. Qt 5.5, Qt 5.6) are backwards binary compatible, that is a binary built with Qt 5.5 will run on Qt 5.6. They may not be forwards compatible, e.g. a Qt 5.6 binary may not run on Qt 5.5 because it may use features that were added in the newer version. With a patch release (e.g. Qt 5.6.0 and Qt 5.6.1), Qt aims to provide forward and backward binary compatibility. Major releases (e.g. Qt 5 and Qt 6) don't maintain binary compatibility but may be all or mostly source compatible. The implication on the Qt developers is that you need to follow certain rules when making changes (to Qt itself) to avoid breaking binary compatibility. That is why you may see people suggest certain changes and a comment that they can't happen before Qt 6.

What does the access level (public, private, protected) mean for slots?

Like any other function, it defines who has access to call an instance's slot. Public ones can obviously be called by code outside of the class, whereas protected and private slots are accessible only within a class. And even then private slots are only available to that class, not any derived classes. These rules apply only to method calls, Qt's connect() and related functions don't enforce the access levels at run-time when connecting to slots.

What is the recommended approach for fetching and displaying images on iOS using Apple's Photos framework and Qt Quick? Simply invoking the default image picker is not sufficient because the intention is to allow the user to select multiple images. On Android this is fairly straightforward: fetch a set of URLs in C++, and pass them in a model to a QML view whose delegate includes an Image. However, it does not appear to be possible to use URLs in this way on iOS unless the image is an asset in the project. Moreover, iOS image data (NSData *) cannot be fed directly into the constructor for QImage, for example, with the idea of wrapping the QImage in a sub-class of QQuickPaintedItem. So, what can we do?

This is a little too advanced for a Qt For Beginners webinar. We ran this by some of our developers and they had some ideas on how to approach it. QImage can take a char pointer and format as a source. These are probably both available from NSData if you can get the pixels. For example, NSData holds a CFData object which in turn holds CFDataGetBytePtr which is a pointer to byte array. The function CFDataGetBytePtr returns a pointer. That may be what you want but it would require more investigation.

What is the difference between Qt Quick and QML?

They are essentially the same. You can think of Qt Quick as the marketing name for the technology which includes the QML language and all of the related tools, components, etc. (like the QML compiler and components/controls).

What is a d-pointer?

Many Qt classes contain an instance variable called d_ptr. This is part of a design pattern called a d-pointer (also called opaque pointer) where the implementation details of a library may be hidden from its users and changes to the implementation can be made to a library without breaking binary compatibility. You can read about it in more detail in an article on the Qt wiki server:

I suspect I have a memory leak. How can I track it down?

Memory and resource leaks can be tough problems to solve. On Linux, the free valgrind tool is highly recommended for this. You can even run it from Qt Creator and it is supported on embedded Linux. When using the GNU C library, the GNU malloc and related functions have some support for extra checking using some environment variables and there are some functions you can call from the debugger to check memory allocation. On Windows we recommend Insure++ from Parasoft, but it is commercial and needs to be purchased.

Is there a way to make system() calls through Qt? For example, can I open the Windows notepad from within a Qt application?

You can use the standard C function system() to start another process. It is generally better though to use Qt's QProcess class to start other processes so it will not block your application and you can get information about the process through signals and slots. You could open the Windows notepad this way, although it is obviously not portable to other platforms. If want you want to do is open a text editor or viewer, you could use Qt's QDesktopServices class to do this in a portable way, as it will open whatever application is associated with a file type, such as a text file.

How can I know what Qt header files I need to include?

A good rule of thumb is to include the header file for every Qt class you use in your code. The Qt header files follow the convention of being named the same as the corresponding class. If you only need to declare a pointer to a class, you don't need to include it's header file, you can just declare it as a class, e.g "class Foo;".

What is the preferred rendering back end to use for Qt on embedded Linux?

The most commonly used, especially for QML-based applications, is eglfs which uses OpenGL/ES. Widget-based applications sometimes use xcb (X11). Another option is linuxfb, which is the Linux frame buffer. The directfb driver is not recommended as the open source DirectFB project seems to be no longer maintained and the web site is down.

Would using Qt and PostgreSQL with Visual Studio be easier to create a connection rather than MingW?

I understand that Visual Studio has some tools for building database interfaces, but the resulting code would not be portable to other platforms (which may or may not be an issue for you). Qt has database abstraction classes that I think would do much the same thing so you would not need to call the PostreSQL APIs directly, you could use classes from the Qt SQL module like QSqlDatabase that would make it easier.

You talked about using the Qt XML libraries for populating an application with website data, would this be the best option if it's a desktop application?

The Qt QML libraries are non-graphical so they would work with widget-based desktop applications. The issue may be that while HTML is technically XML, many web sites do not have well-formed HTML pages and parsing them as XML might result in errors. For simple web pages, string and regular expression functions might be enough to parse the web pages, or for more complex pages you could use a third party HTML parsing library.

What do you think about designing a complex application, for example, PhotoShop using QML. Is this really a good way to go ?

Definitely doable. You may need to rethink the GUI if you aren't using Qt Quick Controls, as then you'll need to make your buttons and the like. I have definitely worked on projects as complex and Qt does provide you with all of the separate parts of something like Photoshop, so there's no reason why you could have it in one package.

Is it possible to create a "runtime' app, that is similar to user created PDFs as a data input form?

Sure, you could make an app that acts like an editable pdf and then save it into a non-editable format.

Is it possible to get a cross-reference of function usage when compiling a desktop application?

There is nothing built in to Qt or Qt Creator but there are lots of third party tools for creating cross-references. One example is LXR, see You could integrate this into your build process. If want you really want to do is locate certain symbols in source code, Qt Creator can do this for you.