So here is my complicated question and I hope you know the answer 🙂
I am developing app for android using Qt Necessitas.
I have made .ui file for GUI and I didn’t find it what I really want so a lot of people suggested QML as it is very dynamic and a lot of options available.
But problem to me is as I have never been working with QML before.
I need a bit of help just with few functions so I can know how to do it later.
For example. this is my function which I call it to read data from database using a post method and calling php script.
void MainWindow::Citanje_korisnika() //read users
{
init();
QUrl params;
params.addQueryItem("action","Citanje_korisnika");
QByteArray data;
data.append(params.toString());
data.remove(0,1);
QNetworkRequest request;
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,
QVariant("application/x-www-form-urlencoded"));
reply = manager->post(request, data);
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),this, SLOT(updateDataTransferProgress(qint64,qint64)));
}
-
So my question is how to call this function from qml code? For example when user presses button?
-
After that I have this function which puts me everything into my form.
void MainWindow::Pokazi(QList< QMap<QString,QString> >& osobe) //show { i_max=osobe.count(); osobe_next=osobe; osoba = osobe.at(0); //trenutni_id=osoba["id"]; ui->label_ID->setText(osoba["id"]); ui->line_ime->insert(osoba["ime"]); ui->line_prezime->insert(osoba["prezime"]); ui->line_telefon->insert(osoba["broj"]); ui->line_adresa->insert(osoba["adresa"]); }
So this is where I want to display all persons on my list.
-
Is it possible to load this above QList into QML ListView which looks like this?
Can I populate it dynamically?ListModel { ListElement { name: "Bill Smith" number: "555 3264" } ListElement { name: "John Brown" number: "555 8426" } }
This is only example with already inserted values. My question is how to make it populate from qml code dynamically?
And I want to get my list looks like second picture in this link: http://doc.qt.nokia.com/4.7-snapshot/qml-listview.html
I managed to make it looks like this but problem is I want to load my data dynamically inside I don’t want it predefined.
If you need more code let me know.
1) calling c++ function from QML is easy.
You need to expose your object via setContextProperty
QDeclarativeContext *ctxt = view.rootContext();
ctxt->setContextProperty(“timer”, &timerObj);
Then you can call any slots of exposed object or function which is declated as Q_INVOKABLE.
This link may help you more.
2) You need to create model, you can use QStringListModel, or you can create custom model derived from QAbstractListModel.
Hope this will help you.