Setting a Qml Context Property in the Qt5.7 QtQuick2 application template
<p>My goal is to populate a combo box with serial ports available on the (Windows) machine. I created a wrapper for QSerialPortInfo that I would like to publish as QML root context property. I used the QtQuick2 application template as starting point.</p>

<pre><code>#include &lt;QList&gt;
#include &lt;QObject&gt;

class QuickSerialPortInformer : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QList&lt;QString&gt; portselectionmodel READ portselectionmodel CONST)

public:
    static const QuickSerialPortInformer *getInstance();

    QList&lt;QString&gt; portselectionmodel() const;

protected:
    QuickSerialPortInformer();

private:
    static const QuickSerialPortInformer *Instance;
};

#endif // QUICKSERIALPORTINFORMER_H
</code></pre>

<p>I register the object like this:</p>

<pre><code>int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    /* here */ engine.rootContext()-&gt;setContextProperty("serialPortInformer", QuickSerialPortInformer::getInstance());
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));

    return app.exec();
}
</code></pre>

<p>The <code>QList&lt;QString&gt;</code> returned by the property shall serve as model for the combobox. My guess is that <code>QList&lt;QString&gt;</code> is converted to a JavaScript <code>array</code> of strings and then converted back to whatever the C++ part of the <code>ComboBox</code>'s model property needs. After all, the property accepts an array of strings defined in JavaScript. However, it seems that when assigning the property of the <code>QuickSerialPortInformer</code> to the <code>model</code> of the <code>ComboBox</code> no such marshalling is done. As pointed out in the comments, only some C++-types may be used as models for QtQuick views. One of them is <code>QStringList</code>, which I am using now.</p>

<pre><code>ComboBox {
    id: portSelector
    model: serialPortInformer.portselectionmodel
}
</code></pre>

<p>My problem is that <code>SerialPortInformer</code> is unknown in the qml context. I suspect that the context I am using to register the object is not visible to the script I am executing with <code>engine.load()</code>. I tried to reverse the order of <code>load()</code> and <code>setContextProperty()</code> to no avail. What am I missing?</p>

<p>After renaming <code>SerialPortInformer</code> to <code>serialPortInformer</code> as suggested in the comments and documentation another problem arises. <code>serialPortInformer</code> is a <code>boolean</code> of value <code>true</code> in QML instead of the expected object.</p>

<p>I was able to solve the "boolean"-mystery to the extent that I have been able to make my use case work. The answer is that my <code>SerialPortInformer::getInstance()</code> was returning a pointer to <code>const</code>. As soon as I removed the <code>const</code>-qualifier <code>serialPortInformer</code> was of type object and exposed the desired attribute. The only reason I can think of why this compiled in the first place is that the <code>const QVariant &amp;</code> overload of the method was called instead, using a non-<code>explicit</code> constructor. So my note-to-self is: <em>Never register pointers to constant <code>QObject</code>s in a QML context</em></p>