Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Passing C++ function parameters from QML
Forum Updated to NodeBB v4.3 + New Features

Passing C++ function parameters from QML

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndyPartridge
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 property

      public slots:
      void setTitle(const QString &title);

      signals:
      void titleChanged(const QString&);

      private:
      QString m_title;

      };
      @

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AndyPartridge
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AndyPartridge
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What do you mean ? It's a standard Q_PROPERTY usage

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AndyPartridge
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved