PDA

View Full Version : c++ and qml binding



ganeshgladish
6th May 2013, 13:42
hi,
how to receive signal from c++ to qml....using qdeclarativeview i am able to bind qml with c++ but i don't know how to bind c++ with qml....

wysota
6th May 2013, 14:52
Expose a C++ object to QML engine using QDeclarativeContext::setContextProperty() and use one of the means available (either the Connections element or javascript object.signal.connect(slot) expression) to connect a QML function to such object's signal.

ganeshgladish
6th May 2013, 16:01
thank you for your reply,
here i am using qml for ui and c++ for database ....whenever i retrieve data from c++.... i want to give to qml..

anda_skoa
6th May 2013, 16:24
You might want to consider exposing the data as a model.
Maybe you can even use QSqlQueryModel?

Cheers,
_

ganeshgladish
7th May 2013, 13:54
thank you for your reply,
but error is comming like this,
QMetaObject::invokeMethod: No such method QDeclarativeRectangle::myQmlFunction(QVariant)
QML function returned: ""

thank you for your reply,
but error is comming like this,
QMetaObject::invokeMethod: No such method QDeclarativeRectangle::myQmlFunction(QVariant)
QML function returned: ""

wysota
7th May 2013, 14:42
Show your code please.

ganeshgladish
7th May 2013, 15:48
this is my qt-c++ code, here i am checking user name and password,if user name and password matching true means i am send string "hai" to my qml...in qml i am checking that string then i taking decision.....
that userin and passin coming from qml using qdeclarative......



Q_INVOKABLE void login_database(const QString &userin,const QString &passin)
{
QSqlQuery querylogin(db);
if(db.isOpen())
{
querylogin.exec("SELECT * FROM hospital WHERE UserName=\'"+userin+"\'");
if(!querylogin.exec())
{
QMessageBox::information(this, tr("Login Match"),tr("Sorry query is not execute"),QMessageBox::Ok);
}

else
{
QString Pass,Uname;
bool matchPass=FALSE;
bool matchUname=FALSE;
while (querylogin.next())
{
Uname=querylogin.value(1).toString();
Pass = querylogin.value(2).toString();

if(Uname==userin)
matchUname=TRUE;


}




if(!matchUname)
{

QMessageBox::information(this, tr("Login Match"),tr("Invalid Username"),QMessageBox::Ok);

}

else if(!matchPass)
{
// loginlbl->setText("Wrong Password");
QMessageBox::information(this, tr("Login Match"),tr("Invalid Password"),QMessageBox::Ok);
return;
}

else
{

QMessageBox::information(this, tr("Login Match"),tr("Sucessfully ur login to database"),QMessageBox::Ok);


QDeclarativeEngine engine1;
QDeclarativeComponent component(&engine1, "main.qml");
QObject *object = component.create();

QVariant returnedValue;
QVariant msg = "Hello from C++";
QMetaObject::invokeMethod(object, "myQmlFunction",
Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, msg));

qDebug() << "QML function returned:" << returnedValue.toString();
delete object;



}


}




}

Added after 7 minutes:

here i am using qml for gui...here i am enter username and password,then i am click login button then that text goto c++ da

// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1

Rectangle {
id:hdcs_ui_login_rect1
width: 1600
height:900
// color:"red"


Image {
id: back

anchors.fill: parent
source: "img/backg.png"
cache: true

}


Rectangle{


id:outline
width: parent.width/1.2
height: parent.height/1.2
color: "transparent"
anchors.centerIn: parent



Image {
id:login

anchors.topMargin: 11
source: "img/doctors_26.png"
cache: true


}

Rectangle{


id:outline1
width: parent.width/2
height: parent.height/1.8
color: "transparent"
anchors.centerIn: parent


Image {
id:login1

anchors.topMargin: 11
anchors.fill: parent


// anchors.top:coll.top
source: "img/newimage1.png"
cache: true


}


Column
{
id:coll
anchors.centerIn: parent
spacing: 30



Column{
spacing: 5
Text {
id: title
text: qsTr("Login")
font.pixelSize: 30
color:"white"
font.bold: true
}

}




Column{
spacing: 5
Text {
id: name
text: qsTr("User Id:")
font.pixelSize: 20
color:"white"
}
Rectangle{
id:rect
width: 300
height: 30
color: "white"

TextInput
{
id: userInputlog
width: parent.width-6
height: parent.height-6
anchors.centerIn: parent
font.pixelSize: 15
font.bold: true
maximumLength: 15

}
}
}
Column
{
spacing: 5
Text {
id: passlog
text: qsTr("Password:")
font.pixelSize: 20
// font.bold: true
color:"white"
}
Rectangle{
id:rect1
width: 300
height: 30
color: "white"

TextInput
{
id: passwordInput
width: parent.width-6
height: parent.height-6
anchors.centerIn: parent
font.pixelSize: 15
font.bold: true
maximumLength: 15
echoMode: TextInput.Password

}
}
}

Rectangle{
// spacing: 100
id:buttons
width: parent.width
color: "transparent"


}

Row{
spacing: 20
anchors.horizontalCenter: parent.horizontalCenter


Image {
id: img
source: "img/re1.png"
fillMode: Image.PreserveAspectFit
Text {
id:log
text: qsTr("Login")
anchors.centerIn: parent
color:"white"
font.pixelSize: 20
font.bold:true

}


function myQmlFunction(msg) {
console.log("Got message:", msg)
return "some return value"
}

MouseArea
{
anchors.fill: parent
onPressed:
{
myObject.login_database(userInputlog.text,passlog. text);

function myQmlFunction(msg) {
console.log("Got message:", msg);

if(msg == userInputlog.text)
{
loader.source = "HDCS_UI_DR_CHF.qml"

}


}




}


}

}


Image {
id: img1
source: "img/re1.png"
Text {
id:log2
anchors.leftMargin: 20
text: qsTr("Forgot Password")
anchors.centerIn: parent
color:"white"
font.bold: true
font.pixelSize: 20
}
}

}
}
}



}

Image {
id: shutdown
anchors.verticalCenterOffset: 350
anchors.horizontalCenterOffset: 550
anchors.horizontalCenter:parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter

source: "img/power.png"
cache: true
smooth: true

MouseArea{

anchors.fill: parent
onClicked:
{

Qt.quit();
}

}

}


Loader {
id: loader


anchors.fill: parent
focus: true
}


}

wysota
7th May 2013, 21:10
Your code is bizzare... you're creating a QML UI as a response to SQL query? I have no idea what you are trying to acomplish but it looks like you should study the difference between declarative and imperative programming. Not speaking of trying to call some function on some object that is defined in a totally different object. No wonder it doesn't work, right?

anda_skoa
8th May 2013, 15:19
If all you need is to communicate a successful login to the QtQuick UI, it would be a lot easier to have a property for that on your C++ object and to change it when login succeeds.

The QML code can then just bind to that property.

Something like


loader.source = myobject.loggedIn ? "HDCS_UI_DR_CHF.qml" : ""


Cheers,
_