0

I have designed the basic UI with some buttons,textareas using QtQuick application. I need to write the event handlers in C++ for the events like button click. How can I access the qml elements from C++ file?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nikhil Patil
  • 457
  • 1
  • 7
  • 15
  • Possible duplicate of http://stackoverflow.com/questions/21437841/how-to-connect-a-qt-quick-button-click-to-a-c-method/21438337#21438337 – Frank Osterfeld Mar 19 '14 at 06:37
  • I tried it out.. But i'm getting this error."main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl SomeClass::SomeClass(class QObject *)" (??0SomeClass@@QEAA@PEAVQObject@@@Z) referenced in function main" – Nikhil Patil Mar 19 '14 at 07:02
  • Have you added .h and .cpp file for SomeClass to your project, with the .cpp file containing the implementaiton of SomeClass::SomeClass(QObject*)? – Frank Osterfeld Mar 19 '14 at 07:54
  • Ya i used the same .cpp & .h file for SomeClass which was in that link. – Nikhil Patil Mar 19 '14 at 09:01
  • Sorry i made a mistake while creating the object. That error went off.. but I'm getting some other error in this line `viewer.rootContext()->setContextProperty(QStringLiteral("_someObject"), &sc);` error: "C:\Qt\Qt5.2.1\Tools\QtCreator\bin\PSC\main.cpp:14: error: C2248: 'QVariant::QVariant' : cannot access private member declared in class 'QVariant'" – Nikhil Patil Mar 19 '14 at 09:15

1 Answers1

1

Accessing QML elements from C++ is not a good practice. I'm going to show you a more favored approach with a trivial example.

  • Write a C++ class that inherits QObject (This class will implement the methods you'd want to call when the events occur) (e.g.)

[controller.h]

#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <QObject>

class Controller : public QObject
{
    Q_OBJECT
public:
    explicit Controller(QObject *parent = 0);
    Q_INVOKABLE void onItemClicked();
};

#endif // CONTROLLER_H

[controller.cpp]

#include "controller.h"
#include <QDebug>

Controller::Controller(QObject *parent) : QObject(parent)
{
}

void Controller::onItemClicked()
{
    qDebug() << "The item was clicked";
}
  • Create an instance of that class and set it as a context property in the QQmlEngine that is running your QML UI. Following a typical "main.cpp" for a Qt Quick application.

[main.cpp]

#include <QtGui/QGuiApplication>
#include <QQmlEngine>
#include <QQmlContext>
#include "qtquick2applicationviewer.h"
#include "controller.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;
    Controller c;
    viewer.engine()->rootContext()->setContextProperty("Controller", &c);
    viewer.setMainQmlFile(QStringLiteral("qml/quicktest/main.qml"));
    viewer.showExpanded();
    return app.exec();
}
  • Now you can freely call the method(s) from the QML code.

[main.qml]

import QtQuick 2.0

Rectangle {
    width: 360
    height: 360
    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Controller.onItemClicked();
        }
    }
}

I hope this helps.

mhcuervo
  • 2,610
  • 22
  • 33