View Full Version : importing C++ objects in QML, import PackageName VersionNumber not working

10th November 2010, 03:26

What I am trying to do here is create a communication from the QML front end to the C++ backend.

I have a class in C++ named Dialpad with corresponding .h and .c files you can see here:

#ifndef DIALPAD_H
#define DIALPAD_H

#include <QObject>

class Dialpad : public QObject
explicit Dialpad(QObject *parent = 0);

void dialSignal(const QString &inNumberToDial);
public slots:
void dialSlot(const QString &inNumberToDial);


#endif // DIALPAD_H

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

Dialpad::Dialpad(QObject *parent) : QObject(parent)


Name: dialSlot().
Description: this function is called from QML front end and sends the number to be dialed.
It will then emit the number to any functions who may need to number.
@param inNumberToDial this is the number that will be dialed
@return void
void Dialpad::dialSlot(const QString &inNumberToDial)

qDebug() << "Number recieved from QML frontend: " << inNumberToDial << endl;
emit dialSignal(inNumberToDial);


I have a main.cpp that is doing the following:

#include <QtGui/QApplication>

#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDebug>
#include <QList>

#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QGraphicsDropShadowEffect>

#include "callinfo/callsonholdmodel.h"
#include "dialpad/dialpad.h"

int main(int argc, char *argv[])
QApplication a(argc, argv);

qmlRegisterType<Dialpad>("DialpadPkg", 1, 0, "aDialpad");

QDeclarativeView view;
QDeclarativeContext *ctxt = view.rootContext();
//ignore the setContext, this was for something else
ctxt->setContextProperty("callsonholdModel", QVariant::fromValue(callsonholdList));


return a.exec();

Now when I try to import the object DialpadPkg so I can use aDialpad item to send a message from QML to C++ with the aDialpad.dialSlot("12345"); it says aDialpad is not a type, and it also says it can't import DialpadPkg 1.0

here is the background.qml

import Qt 4.7
import "Dialpad"
import "callinfo"
import "buttontray"
import "calendar"
import DialpadPkg 1.0 //doesn't work

Rectangle {

//doesn't work
id: interfaceDialpad



The directory stucture is the following:


Any ideas?


10th November 2010, 09:44
never mind this post - something weird happened when editing.:p

10th November 2010, 10:16
qmlRegisterType only registers the type in qml so that you can operate on its instances. It doesn't create a module, so you can't say "import DialpadPkg" in your code.

10th November 2010, 15:07
How would I create an instance of it in QML so i can send info from QML to C++ what would I need to do if its not import? I already have a model that is sending information to QML to updating the QML but I need QML now to send info to C++. Such as pressing a button on QML I need it to send a string down to C++ end so I can do further processing.


10th November 2010, 21:45
I got it to work but not sure if its the right way of doing it:

QDeclarativeView view;
QDeclarativeContext *ctxt = view.rootContext();
ctxt->setContextProperty("myDialpad", new Dialpad());

I have a C++ class called Dialpad and you can access the object by refering to myDialpad in the QML code. So to send say a string to the C++ backend you would write the following in QML


11th November 2010, 10:41
Well, that's the way I did it first, too. You can also declare your signal&slot as Q_PROPERTY() for your C++ QObject. If you define the READ and WRITE and NOTIFY methods for the Q_PROPERTY, you can then treat it as a "normal" object in QML. So write in C++ class declaration something like

Q_PROPERTY(QString numberToDial READ getNumberToDial WRITE dialSlot NOTIFY dialSlignal)

then you can write in QML:

myDialpad.numberToDial = "12345"