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.