diff --git a/package.json b/package.json index 91bf442cb..87c8cc51d 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/cpp/include/nodegui/QtCore/QAbstractItemModel/qabstractitemmodel_wrap.h b/src/cpp/include/nodegui/QtCore/QAbstractItemModel/qabstractitemmodel_wrap.h index f391b95a9..1d703fb3f 100644 --- a/src/cpp/include/nodegui/QtCore/QAbstractItemModel/qabstractitemmodel_wrap.h +++ b/src/cpp/include/nodegui/QtCore/QAbstractItemModel/qabstractitemmodel_wrap.h @@ -27,4 +27,5 @@ class DLL_EXPORT QAbstractItemModelWrap : public Napi::ObjectWrap(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::Unwrap(info[0].As()); + QModelIndex* topLeftIndex = topLeftModelIndexWrap->getInternalInstance(); + + QModelIndexWrap* bottomRightModelIndexWrap = Napi::ObjectWrap::Unwrap(info[1].As()); + QModelIndex* bottomRightIndex = bottomRightModelIndexWrap->getInternalInstance(); + + Napi::Array rolesNapi = info[2].As(); + QVector roles(rolesNapi.Length()); + for (int i = 0; i < rolesNapi.Length(); i++) { + Napi::Value numberNapi = rolesNapi[i]; + roles.append(numberNapi.As().Int32Value()); + } + + emit this->instance->dataChanged(*topLeftIndex, *bottomRightIndex, roles); + + return env.Null(); +} \ No newline at end of file diff --git a/src/examples/modelview_3_changingmodel.ts b/src/examples/modelview_3_changingmodel.ts new file mode 100644 index 000000000..61cbd5080 --- /dev/null +++ b/src/examples/modelview_3_changingmodel.ts @@ -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(); diff --git a/src/lib/QtCore/QAbstractItemModel.ts b/src/lib/QtCore/QAbstractItemModel.ts index 2c2f932e9..5c046f722 100644 --- a/src/lib/QtCore/QAbstractItemModel.ts +++ b/src/lib/QtCore/QAbstractItemModel.ts @@ -110,4 +110,8 @@ export class QAbstractItemModel extends NodeObject { 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); + } }