Wednesday, November 29, 2017

QWidget: Must construct a QApplication before a QWidget

Have you faced such error message? It's specially likely to appear if you are using Qt framework along with 3rd party libraries. The solution?

Wednesday, November 22, 2017

Emit signals of other objects...

It'll depend on the Qt version you're using. If Qt 5.x it's easy, as signals are public methods. And the 'emit' keyword expands to an empty string, so just both entries below are equivalent:
a.signal_X();
emit a.signal_X();
If using Qt 4.x, it's not that easy since signals are protected methods so you can't emit signals directly. However, there are a couple of approaches to achieve that:
1. create public method in class A, that will emit necessary signals
2. create signal in class B and connect it to a signal in class A

Given all that said, please remember that it might not be a good idea to emit signals of other objetcs directly.

IMO signals are intended to be emitted by a class as a response to some event, i.e. a QPushButton object will emit the clicked() signal when the user clicks with the mouse on such button. So if you really needs that button to emit the clicked() signal, my approach is to provoke the right event so that signal is finally emitted. Following this example, you need to call QPushButton.click() method instead of emitting the signal directly, but having the exact same behavior as a result of the object's reaction to a particular event.

Happy coding!

Tuesday, November 21, 2017

QML ListModel is not a QML List Type

As 0 be 1 cannot be (from Star Wars parlance), a QML ListModel is not a list QML Basic Type. Given that said, let's say you have a properly populated ListModel of coordinates (i.e. pairs of latitude and longitude values), and you want to use it as input for a QML MapPolyline to display a polyline in a map...

Monday, October 23, 2017

Raspbian: /opt/qt/bin/qmake: cannot execute binary file: Exec format error

If you're cross-compiling Qt for Raspberry Pi (for instance following this guide), and once everything finished and seems Ok, you may encounter the following error while running qmake:
/opt/qt/bin/qmake: cannot execute binary file: Exec format error

Friday, September 8, 2017

pkg_config and qmake

If you're developing an application with Qt which in turn relies on some library that uses the pkg-config tool, you can take advantage and simplify your Qt project file (.pro) as it will help avoiding hard-coded values for where to find headers and libraries for a particular package.

Using a custom QNetworkAccessManager in QML

The QML Image component has the capability to specify the source of the image is as a URL (using the "source" property) and QML automatically retrieving such image file from a remote location. To do so, QML uses a QNetworkAccessManager (QNAM for short) in the background, which has a lot of default values. But what if you need to set some custom values for the QNAM?

Thursday, August 10, 2017

Internationalization with Qt VS Tools

The good news for people using Qt VS Tools addin is that translations work very well from within the Visual Studio environment. The whole lifecycle of translations in Qt, that is:

  • use tr() method to wrap all translatable strings
  • create the .ts files with lupdate command
  • translate using Linguist
  • create the compiled .qm files with lrelease command
can be carried on without any issues, see more information here.

Monday, August 7, 2017

Qt on Android: browsing files from device with QFileDialog widget in Linux

I was developing an Android application with Qt using a real device connected to my laptop with Lubuntu, and I needed to open files created by such application on Android's file system directly from my PC. The thing is, the device was auto-mounted whenever I connected it, and showing properly in the file manager (something like mtp://[usb:001,007]/), but while using QFileDialog::getOpenFileName there was no way to find the device and browse its folder structure.

Wednesday, April 5, 2017

Make the compiler fail if using deprecated Qt features

Instead of making the compiler warn you if using deprecated Qt features (see previous post), by using QT_DISABLE_DEPRECATED_BEFORE it's even possible to make your code fail to compile if you use deprecated APIs.

Make the compiler warn you of deprecated Qt features

QT_DEPRECATED_WARNINGS is a keyword that can be used in your Qt's project files (.pro) so qmake makes your compiler emit warnings if you use any feature of Qt which as been marked deprecated. The exact warnings depend on your compiler, but it'll be easy to spot any location in your source base that might be affected for any Qt's API changing soon.

Monday, April 3, 2017

Ever dreamt of creating your own QML controls?

Were you in need of a control not provided in the QML controls set? Have you wondered how difficult could such an enterprise be to write and use your own QML control to fit your needs? Do you feel like contributing any new QML control back to the community?

Friday, February 10, 2017

Assign radio buttons to QButtonGroup in Qt Designer

When you use the Qt designer to design your form, you can group buttons by selecting them and choosing "Assign to button group" then "New button group" from the context menu.

Qt Enumerations to String

Have you ever been in the need of showing a more meaningful message that just the value related to a particular Qt Enumeration? I'm talking especially about logging or debugging situations, were a sentence like "Dialog modality is Qt::WindowModal" would be far far better than "Dialog modality is 1" instead.