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...