Unsolved C++ updating ListView
-
Hi,
I need to know how can I change the MainForm.ui.qml objects using C++.
In this case I have a access in a database and I want to add items in my ListView using C++.MainForm.ui.qml
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.2Item {
id: item1
width: 640
height: 480property alias buttonD: buttonDespensa property alias buttonL: buttonLista property alias buttonP: buttonProdutos ..... ..... ..... ListView { id: listView1 width: 110 height: 160 anchors.left: parent.left anchors.leftMargin: 10 anchors.horizontalCenter: parent.horizontalCenter anchors.top: parent.top anchors.topMargin: 35 delegate: Item { x: 5 width: 80 height: 40 Row { id: row1 Rectangle { width: 40 height: 40 color: colorCode } Text { text: name font.bold: true anchors.verticalCenter: parent.verticalCenter } spacing: 10 } } model: ListModel { ListElement { name: "Grey" colorCode: "grey" } ListElement { name: "Red" colorCode: "red" } ListElement { name: "Blue" colorCode: "blue" } ListElement { name: "Green" colorCode: "green" } } }
}
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include "qdebug.h"
#include "database.h"
#include <QtQuick>int main(int argc, char *argv[])
{
QApplication app(argc, argv);QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); DataBase banco; return app.exec();
}
database.cpp
#include "database.h"
#include <QtSql>
#include "qdebug.h"DataBase::DataBase()
{
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("despensa.sqlite");
if (db.open()) {
QSqlQuery query("SELECT * FROM produtos;");
while (query.next()){
// listView1.item << query.value(1).toString();
qDebug() << query.value(1).toString();
}
db.close();
}
}I need add items (ListElement) using query.value(1).toString() from C++. How can I do that?
-
Hi, and welcome to the Qt forum! Instead of creating the model with QtQuick you could create it in C++. The documentation has example code for this, see: Using C++ Models with Qt Quick Views.
-
@Wieland
I read this document and I tried this code:
main.cpp
QStringList dataList;
dataList.append("item 1");
dataList.append("item 2");
dataList.append("item 3");QQuickView view; QQmlContext *ctxt = view.rootContext(); ctxt->setContextProperty("myModel",QVariant::fromValue(dataList)); view.setSource(QUrl("qrc:/MainForm.ui.qml")); view.show();
MainForm.ui.qml
...
ListView {
id: listView2
width: 110;
height: 160
anchors.left: parent.left
anchors.leftMargin: 10
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: 35model: myModel delegate: Rectangle { height: 25 width: 100 Text { text: modelData } } }
...
That code works, but it broke my layout.
In the design mode, my layout is perfect. -
@Giba I think there are several options to achieve your goal :
1 - you can have you model designed in the C++ side as @Wieland proposed
2 - give a name to your QML model, so that you can retrieve it from C++ (despite this will works and it is - unfortunately - well documented, it is a very bad option as it will break the QtQuick design principle to keep the C++ agnostic of the QML)
3 - you can have a kind of controler object in C++ that would be passed to the QML engine to drive the model using signals connection (I mean you would - in the QML - react on specific signal such as removeRow(id) to handle the removal of a row in the model...) -
@Charby
I want just manipulate the ui objects using C++. I don't want create ui objects using C++.
The most exemples that I can see in internet is creating and using object from C++.
I can't understand the difference in MainForm.ui.qml and main.qml file.
My C++ starts only main.qml (engine) but the layout uses the both files. -
@Giba Manipulating qml objects from c++ (this can be done by iterating every object from the root element of the engine finding a specific name) should be avoided as it won't be working if you change the name of QML element.
if you really want to : you can find information about how to acheive this hereThe ui.qml is generated by the UI designer.
-
@Charby About manipulating qml object thank you. I thought it was move easy like create a class with same qml object name, but it isn't.
My question about MainForm.ui.qml and main.qml is how theses files works?
My application is designed in MainForm.ui.qml, but I have some elements in mail.qml (like menuBar) that can't use in ui, but there is in my application. The source code in C++ call only main.qml, where is the MainForm.ui.qml file call?