How in QML get data with QSqlQueryModel
-
Hi!
I have model QSqlQueryModel.
Me need get data with model in QML.
This code not work:console.log("printFormatModel: " + printFormatModel.data(printFormatModel.index(1,1),1))Debug return:
qml: printFormatModel: undefinedС++ have metod toInt(), but QML not have this metod.
-
Hi,
It's explained in the Using C++ Models with Qt Quick Views chapter of Qt's documentation.
-
Second parameter of
datamethod is modelrole. According to your example you are trying to getDecorationRole. Maybe role is simply set wrong? -
It's too not works:
printFormatModel.data(printFormatModel.index(1,1),"idRole")It's my model:
#include "printformatmodel.h" PrintFormatModel::PrintFormatModel(QObject *parent) : QSqlQueryModel (parent) { } QVariant PrintFormatModel::data(const QModelIndex &index, int role) const { int columnId = role - Qt::UserRole - 1; // Создаём индекс с помощью новоиспечённого ID колонки QModelIndex modelIndex = this->index(index.row(), columnId); /* И с помощью уже метода data() базового класса * вытаскиваем данные для таблицы из модели * */ return QSqlQueryModel::data(modelIndex, Qt::DisplayRole); } QVariantMap PrintFormatModel::get(int idx) const { QVariantMap map; foreach(int k, roleNames().keys()) { map[roleNames().value(k)] = data(index(idx, 0), k); } return map; } QHash<int, QByteArray> PrintFormatModel::roleNames() const { QHash<int, QByteArray> roles; roles[idRole]="id"; roles[productIdColumn]="product_id"; roles[printFormatId]="print_format_id"; roles[x1]="x1"; roles[y1]="y1"; roles[fontSize]="font_size"; roles[printFormatKey]="print_format_key"; roles[printFormatText]="print_format_text"; roles[boolPrintFormatKey1]="bool_print_format_key_1"; roles[boolPrintFormatKey2]="bool_print_format_key_2"; roles[boolPrintFormatKey3]="bool_print_format_key_3"; roles[boolPrintFormatKey4]="bool_print_format_key_4"; roles[boolPrintFormatKey1Text]="bool_print_format_key_4_text"; return roles; } void PrintFormatModel::setId(int idNumber) { productId = idNumber; } void PrintFormatModel::updateModel(int id) { this->setQuery("select id, product_id, print_format_id, x1, y1, font_size, print_format_key," " print_format_text, bool_print_format_key_1, bool_print_format_key_2," " bool_print_format_key_3, bool_print_format_key_4, bool_print_format_key_4_text" " from ProductConfigForPrint WHERE product_id = " + QString::number(id)); } int PrintFormatModel::getId(int row) { return this->data(this->index(row, 0), idRole).toInt(); } -
Role parameter should be integer, not a string. Look at method definition. You can at least try to print what
idyou get and result of yourcolumnId, make sure they are valid. -
You cannot write
printFormatModel.data(printFormatModel.index(1,1),"idRole")Second parametr in this case for method
datawould be"idRole"which is string, while model waits for some integer value. When you use it likeprintFormatModel.data(printFormatModel.index(1,1),1)Your second parameter would be
1. But according to your codemodelIndexatPrintFormatModel::datawould be invalid, sinceQt::UserRoleis equal to 256, you will get1 - 256 - 1forcolumnId. -
New thing named "read docs"? It is your code, use debugger, we do not know what are you trying to achieve.