Add the changing data model view example

This commit is contained in:
Simon Edwards 2021-08-14 21:03:53 +02:00
parent 25026a55f7
commit b5a9f30d34
5 changed files with 72 additions and 1 deletions

View File

@ -26,7 +26,8 @@
"qode": "cross-env node ./scripts/qode.js",
"prepublishOnly": "cross-env npm run build",
"example-modelview_1_readonly": "node ./scripts/qode.js dist/examples/modelview_1_readonly.js",
"example-modelview_2_formatting": "node ./scripts/qode.js dist/examples/modelview_2_formatting.js"
"example-modelview_2_formatting": "node ./scripts/qode.js dist/examples/modelview_2_formatting.js",
"example-modelview_3_changingmodel": "node ./scripts/qode.js dist/examples/modelview_3_changingmodel.js"
},
"engines": {
"node": ">=14.x.x"

View File

@ -27,4 +27,5 @@ class DLL_EXPORT QAbstractItemModelWrap : public Napi::ObjectWrap<QAbstractItemM
Napi::Value hasIndex(const Napi::CallbackInfo& info);
Napi::Value createIndex(const Napi::CallbackInfo& info);
Napi::Value _super_flags(const Napi::CallbackInfo& info);
Napi::Value emitDataChanged(const Napi::CallbackInfo& info);
};

View File

@ -15,6 +15,7 @@ Napi::Object QAbstractItemModelWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("hasIndex", &QAbstractItemModelWrap::hasIndex),
InstanceMethod("createIndex", &QAbstractItemModelWrap::createIndex),
InstanceMethod("_super_flags", &QAbstractItemModelWrap::_super_flags),
InstanceMethod("emitDataChanged", &QAbstractItemModelWrap::emitDataChanged),
QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QAbstractItemModelWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
@ -74,3 +75,25 @@ Napi::Value QAbstractItemModelWrap::_super_flags(const Napi::CallbackInfo& info)
auto result = Napi::Value::From(env, static_cast<uint>(this->instance->QAbstractItemModel::flags(*index)));
return result;
}
Napi::Value QAbstractItemModelWrap::emitDataChanged(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QModelIndexWrap* topLeftModelIndexWrap = Napi::ObjectWrap<QModelIndexWrap>::Unwrap(info[0].As<Napi::Object>());
QModelIndex* topLeftIndex = topLeftModelIndexWrap->getInternalInstance();
QModelIndexWrap* bottomRightModelIndexWrap = Napi::ObjectWrap<QModelIndexWrap>::Unwrap(info[1].As<Napi::Object>());
QModelIndex* bottomRightIndex = bottomRightModelIndexWrap->getInternalInstance();
Napi::Array rolesNapi = info[2].As<Napi::Array>();
QVector<int> roles(rolesNapi.Length());
for (int i = 0; i < rolesNapi.Length(); i++) {
Napi::Value numberNapi = rolesNapi[i];
roles.append(numberNapi.As<Napi::Number>().Int32Value());
}
emit this->instance->dataChanged(*topLeftIndex, *bottomRightIndex, roles);
return env.Null();
}

View File

@ -0,0 +1,42 @@
import { ItemDataRole, QAbstractTableModel, QModelIndex, QTableView, QVariant } from '..';
function main(): void {
const tableView = new QTableView();
const model = new MyModel();
tableView.setModel(model);
tableView.show();
setInterval(() => {
model.timerHit();
}, 1000);
(global as any).win = tableView;
}
class MyModel extends QAbstractTableModel {
rowCount(parent = new QModelIndex()): number {
return 2;
}
columnCount(parent = new QModelIndex()): number {
return 3;
}
data(index: QModelIndex, role = ItemDataRole.DisplayRole): QVariant {
const row = index.row();
const col = index.column();
if (role == ItemDataRole.DisplayRole && row == 0 && col == 0) {
return new QVariant('' + new Date().toTimeString());
}
return new QVariant();
}
timerHit(): void {
const topLeft = this.createIndex(0, 0);
this.emitDataChanged(topLeft, topLeft, [ItemDataRole.DisplayRole]);
}
}
main();

View File

@ -110,4 +110,8 @@ export class QAbstractItemModel extends NodeObject<any> {
flags(index: QModelIndex): ItemFlag {
return this.native._super_flags(index.native);
}
emitDataChanged(topLeft: QModelIndex, bottomRight: QModelIndex, roles: ItemDataRole[]): void {
this.native.emitDataChanged(topLeft.native, bottomRight.native, roles);
}
}