<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Linking error with qmlRegisterType]]></title><description><![CDATA[<p dir="auto">Hi all,</p>
<p dir="auto">I am learning exchanging data between qml and C++ class.  So I write this simple test program.</p>
<p dir="auto">I wrote a simple QML file</p>
<pre><code>import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

//import testing 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    property string count: "hello"

    Rectangle
    {
        width: parent.width
        height: parent.height
        Text {
            id: text
            text: qsTr("text")
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
        }
    }
    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            console.log("count " + count)
        }
    }
}
</code></pre>
<p dir="auto">and simple main.cpp</p>
<pre><code>#include &lt;QGuiApplication&gt;
#include &lt;QQmlApplicationEngine&gt;
#include "updatecounter.h"

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

//    qmlRegisterType&lt;UpdateCounter&gt;("testing", 1, 0, "UpdateCounter");

    QQmlApplicationEngine engine;
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));

    return app.exec();
}
</code></pre>
<p dir="auto">Everything is fine up to here.  Then I added my C++ class</p>
<pre><code>#ifndef UPDATECOUNTER_H
#define UPDATECOUNTER_H

#include &lt;QObject&gt;

class UpdateCounter : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString counter READ counter WRITE setCounter NOTIFY counterChanged)
public:
    explicit UpdateCounter(QObject *parent = 0);

    QString counter() const {return mcounter;}
    void setCounter(const QString &amp;counter);
    Q_INVOKABLE void sendCounterUpdate(const QString &amp;counter);

signals:
    void counterChanged();

private:
    QString mcounter;
};

#endif // UPDATECOUNTER_H
</code></pre>
<pre><code>#include "updatecounter.h"

UpdateCounter::UpdateCounter(QObject *parent) : QObject(parent)
{

}

void UpdateCounter::setCounter(const QString &amp;counter)
{
    if(counter == mcounter)
        return;
    mcounter = counter;
}

void UpdateCounter::sendCounterUpdate(const QString &amp;counter)
{
    if(counter == mcounter)
        return;
    mcounter = counter;
    emit counterChanged();
}
</code></pre>
<p dir="auto">The program behaves as expected.  Then I register my class using qmlRegisterType.  I get linking errors.</p>
<pre><code>main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl UpdateCounter::metaObject(void)const " (?metaObject@UpdateCounter@@UEBAPEBUQMetaObject@@XZ)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __cdecl UpdateCounter::qt_metacast(char const *)" (?qt_metacast@UpdateCounter@@UEAAPEAXPEBD@Z)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __cdecl UpdateCounter::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@UpdateCounter@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl UpdateCounter::UpdateCounter(class QObject *)" (??0UpdateCounter@@QEAA@PEAVQObject@@@Z) referenced in function "public: __cdecl QQmlPrivate::QQmlElement&lt;class UpdateCounter&gt;::QQmlElement&lt;class UpdateCounter&gt;(void)" (??0?$QQmlElement@VUpdateCounter@@@QQmlPrivate@@QEAA@XZ)
main.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const UpdateCounter::staticMetaObject" (?staticMetaObject@UpdateCounter@@2UQMetaObject@@B)
</code></pre>
<p dir="auto">Please let me know what I did wrong.</p>
<p dir="auto">Thank you,<br />
Anthony</p>
<p dir="auto"><em>[Added code tags ~kshegunov]</em></p>
]]></description><link>https://forum.qt.io/topic/75493/linking-error-with-qmlregistertype</link><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Apr 2026 16:26:48 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/75493.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 25 Jan 2017 20:03:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Linking error with qmlRegisterType on Thu, 26 Jan 2017 20:41:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/att_">@<bdi>att_</bdi></a> The <a href="https://forum.qt.io/topic/71830">Hitchhiker's Visual Guide to the Qt Forum</a> explains how to mark threads as solved.</p>
]]></description><link>https://forum.qt.io/post/371783</link><guid isPermaLink="true">https://forum.qt.io/post/371783</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Thu, 26 Jan 2017 20:41:40 GMT</pubDate></item><item><title><![CDATA[Reply to Linking error with qmlRegisterType on Thu, 26 Jan 2017 20:41:02 GMT]]></title><description><![CDATA[<p dir="auto">HI,</p>
<p dir="auto">Use the "Topic Tools" button.</p>
]]></description><link>https://forum.qt.io/post/371781</link><guid isPermaLink="true">https://forum.qt.io/post/371781</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 26 Jan 2017 20:41:02 GMT</pubDate></item><item><title><![CDATA[Reply to Linking error with qmlRegisterType on Thu, 26 Jan 2017 17:57:55 GMT]]></title><description><![CDATA[<p dir="auto">Thank you for your advices.  It works now.  I achieved what I want to do.  Here is the code for anyone interested.</p>
<p dir="auto">H file<br />
class UpdateCounter : public QObject<br />
{<br />
Q_OBJECT<br />
Q_PROPERTY(double counter READ counter WRITE setCounter NOTIFY counterChanged)<br />
public:<br />
double counter() const {return mcounter;}<br />
void setCounter(const double);</p>
<pre><code>Q_INVOKABLE void resetCounter(const double);
</code></pre>
<p dir="auto">signals:<br />
void counterChanged();</p>
<p dir="auto">private:<br />
double mcounter;<br />
};</p>
<p dir="auto">CPP file<br />
void UpdateCounter::setCounter(const double counter)<br />
{<br />
if(counter == mcounter)<br />
return;<br />
qDebug() &lt;&lt; "UpdateCounter.setCounter" &lt;&lt; counter;<br />
mcounter = counter + 2;<br />
emit counterChanged();<br />
}</p>
<p dir="auto">void UpdateCounter::resetCounter(const double counter)<br />
{<br />
if(counter == mcounter)<br />
return;<br />
qDebug() &lt;&lt; "UpdateCounter.resetCounter" &lt;&lt; counter;<br />
mcounter = counter;<br />
emit counterChanged();<br />
}</p>
<p dir="auto">int main(int argc, char *argv[])<br />
{<br />
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);<br />
QGuiApplication app(argc, argv);</p>
<pre><code>qmlRegisterType&lt;UpdateCounter&gt;("testing", 1, 0, "UpdateCounter");

QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));

return app.exec();
</code></pre>
<p dir="auto">}</p>
<p dir="auto">and qml file<br />
import testing 1.0</p>
<p dir="auto">ApplicationWindow {<br />
visible: true<br />
width: 640<br />
height: 480<br />
title: qsTr("Hello World")</p>
<pre><code>property double count: 0
UpdateCounter
{
    id: updateCounter
    onCounterChanged:
    {
        count = counter;
        console.log("QML UpdateCounter " + count)
        text.text = qsTr("Counter %1").arg(count)
    }
}

Rectangle
{
    width: parent.width
    height: parent.height/2
    color: "red"
    Text
    {
        text: qsTr("Reset")
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
    }
    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            console.log("QML reset to 10 " + count)
            updateCounter.resetCounter(10)
        }
    }
}
Rectangle
{
    y: parent.height/2
    width: parent.width
    height: parent.height/2
    color: "yellow"
    Text
    {
        id: text
        text: qsTr("text")
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
    }
    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            console.log("QML count " + count)
            count = count + 1
            updateCounter.counter = count
        }
    }
}
</code></pre>
<p dir="auto">}</p>
<p dir="auto">now how do I mark this topic as solved?</p>
]]></description><link>https://forum.qt.io/post/371755</link><guid isPermaLink="true">https://forum.qt.io/post/371755</guid><dc:creator><![CDATA[att_]]></dc:creator><pubDate>Thu, 26 Jan 2017 17:57:55 GMT</pubDate></item><item><title><![CDATA[Reply to Linking error with qmlRegisterType on Thu, 26 Jan 2017 05:27:19 GMT]]></title><description><![CDATA[<p dir="auto">setCounter cannot be called directly. Either make the setCounter(..) function as Q_INVOKABLE or move the function under slot. It will be called. Looking at your code you idea is to set the value of counter. Since counter is exposed via Q_PROPERTY directly set it like updateCounter.counter = 10. Now setCounter function will be called automatically.</p>
]]></description><link>https://forum.qt.io/post/371631</link><guid isPermaLink="true">https://forum.qt.io/post/371631</guid><dc:creator><![CDATA[dheerendra]]></dc:creator><pubDate>Thu, 26 Jan 2017 05:27:19 GMT</pubDate></item><item><title><![CDATA[Reply to Linking error with qmlRegisterType on Wed, 25 Jan 2017 22:31:50 GMT]]></title><description><![CDATA[<p dir="auto">Thank you.  I have to remember to do that all the time.</p>
<p dir="auto">Now I have a different error.  I have<br />
declared setCounter in the .h file<br />
Q_PROPERTY(QString counter READ counter WRITE setCounter NOTIFY counterChanged)<br />
void setCounter(const QString &amp;counter);</p>
<p dir="auto">implemented the code in the cpp file<br />
void UpdateCounter::setCounter(const QString &amp;counter)<br />
{<br />
if(counter == mcounter)<br />
return;<br />
mcounter = counter;<br />
}<br />
registered the class in main.cpp<br />
qmlRegisterType&lt;UpdateCounter&gt;("testing", 1, 0, "UpdateCounter");</p>
<p dir="auto">imported the class and assigned an object in qml<br />
import testing 1.0<br />
UpdateCounter{id: updateCounter}</p>
<p dir="auto">But when I call the function I get this error at run time.<br />
updateCounter.setCounter(count)<br />
qrc:/main.qml:33: TypeError: Property 'setCounter' of object [object Object] is not a function</p>
<p dir="auto">How can I make setCounter visible?</p>
<p dir="auto">Thanks,<br />
Anthony</p>
]]></description><link>https://forum.qt.io/post/371597</link><guid isPermaLink="true">https://forum.qt.io/post/371597</guid><dc:creator><![CDATA[att_]]></dc:creator><pubDate>Wed, 25 Jan 2017 22:31:50 GMT</pubDate></item><item><title><![CDATA[Reply to Linking error with qmlRegisterType on Wed, 25 Jan 2017 21:48:05 GMT]]></title><description><![CDATA[<p dir="auto">Hi! Try "clean project", "run qmake" and then rebuild the project.</p>
]]></description><link>https://forum.qt.io/post/371584</link><guid isPermaLink="true">https://forum.qt.io/post/371584</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 25 Jan 2017 21:48:05 GMT</pubDate></item></channel></rss>