Passing C++ function parameters from QML
-
I wonder if anyone could help point me in the right direction with the following.
I am simply trying to call a C++ function from QML with the highlighted line of code (**)
@ Text {
width: parent.width
height: parent.height
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter** text: xmlHandler.title(hexPosition)
color: "#ffffff" font.pixelSize: 17 font.family: gothamBlack.name font.capitalization: Font.AllUppercase wrapMode: Text.WordWrap }
@
hexPosition is derived from the index of a Repeater - this is functioning just fine because hexPostion is used elsewhere.
xmlHandler.h contains:
@#ifndef XMLHANDLER_H
#define XMLHANDLER_H#include <QObject>
#include <QString>class XMLHandler : public QObject
{
Q_OBJECT
Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
Q_INVOKABLE QString title(const int index) const;public:
void setTitle(const QString &title);public slots:
signals:
void titleChanged();private:
QString m_title;};
#endif // XMLHANDLER_H@
xmlHandler.cpp contains:
#include "xmlhandler.h"
@void XMLHandler::setTitle(const QString &title) {
if ( title != m_title) {
m_title = title;
}
}QString XMLHandler::title(const int index) const {
return QString("Base Handler");
}@
main.cpp contains:
@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>#include "xmlhandler.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
XMLHandler xmlHandler;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("xmlHandler", &xmlHandler);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
@I currently get the error:
@
/Users/Andrew/app/app_build/moc_xmlhandler.cpp:134: error: too few arguments to function call, single argument 'title' was not specified
case 0: reinterpret_cast< QString>(_v) = title(); break;
~~~~~ ^
@I know that my "title" function currently does do anything with the index I am attempting to pass to it. I just want to make sure I can get it in the fore now!
Any help would be much appreciated.
-
Hi and welcome to devnet,
It seems that you are mixing several not completely related things
This version should work better:@
{
Q_OBJECT
Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)public:
Q_INVOKABLE QString title(int index) const; << before, it was private
QString title() const; << this one is for your propertypublic slots:
void setTitle(const QString &title);signals:
void titleChanged(const QString&);private:
QString m_title;};
@Hope it helps
-
SGaist, thank you for your help.
I have adjusted the header file as you suggest and have added the additional version of the title function, as:
@#include "xmlhandler.h"
void XMLHandler::setTitle(const QString &set_title)
{
if ( set_title != m_title) {
m_title = set_title;
}
}QString XMLHandler::title(int index) const
{
return QString("Base Handler with Index");
}QString XMLHandler::title() const
{
return QString("Base Handler");
}@
I now receive an error stating "Property 'title' of object XML Handler is not a function.", from the QML file.
As you can tell, I have rather thrown myself in the deep-end here! But if you have any suggestions, it would be greatly appreciated.
-
Fortunately, I've sorted the problem I have mention above. The problem was this line:
@Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)@
using 'title' for the QString and the READ - a big no-no!!
SGaist, I thank you again for you help - I continue my trek up the steep learning curve!
-
What do you mean ? It's a standard Q_PROPERTY usage
-
Being somewhat of a novice with Qt, I am at a loss to fully explain as to why. But as described, the line:
@Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)@
gave an error stating that the Property title is not a function. When I changed it to:
@Q_PROPERTY(QString title READ getTitle WRITE setTitle NOTIFY titleChanged)@
and adjusted the function name, the error vanished. No other parts of the code were changed.
I'm just reporting my experience with this, if I am attributing the error removal to the wrong piece of code, then I would be intrigued to know your thoughts on what it could me - it may help other noobs too!