importing C++ objects in QML, import PackageName VersionNumber not working
Hi,
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:
Code:
#ifndef DIALPAD_H
#define DIALPAD_H
#include <QObject>
{
Q_OBJECT
public:
explicit Dialpad
(QObject *parent
= 0);
signals:
void dialSignal
(const QString &inNumberToDial
);
public slots:
void dialSlot
(const QString &inNumberToDial
);
};
#endif // DIALPAD_H
#include "dialpad.h"
#include <QDebug>
{
}
/**
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:
Code:
#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[])
{
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
));
view.
setSource(QUrl("qrc:qml/background.qml"));
view.show();
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
Code:
import Qt 4.7
import "Dialpad"
import "callinfo"
import "buttontray"
import "calendar"
import DialpadPkg 1.0 //doesn't work
Rectangle {
//doesn't work
aDialpad
{
id: interfaceDialpad
//interfaceDialpad.dialSlot("12345")
}
...
...
The directory stucture is the following:
MainProject/main.cpp
background.qml
MainProject/Dialpad/dialpad.cpp
dialpad.h
Any ideas?
Thanks
Re: importing C++ objects in QML, import PackageName VersionNumber not working
never mind this post - something weird happened when editing.:p
Re: importing C++ objects in QML, import PackageName VersionNumber not working
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.
Re: importing C++ objects in QML, import PackageName VersionNumber not working
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.
Thanks
Re: importing C++ objects in QML, import PackageName VersionNumber not working
I got it to work but not sure if its the right way of doing it:
Code:
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
myDialpad.dialSlot(dialArea.text)
Re: importing C++ objects in QML, import PackageName VersionNumber not working
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
Code:
Q_PROPERTY(QString numberToDial READ getNumberToDial WRITE dialSlot NOTIFY dialSlignal
)
then you can write in QML:
Code:
myDialpad.numberToDial = "12345"