Add header model view tutorial example
This commit is contained in:
parent
3a83a7fe62
commit
588093519a
@ -27,7 +27,8 @@
|
||||
"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_3_changingmodel": "node ./scripts/qode.js dist/examples/modelview_3_changingmodel.js"
|
||||
"example-modelview_3_changingmodel": "node ./scripts/qode.js dist/examples/modelview_3_changingmodel.js",
|
||||
"example-modelview_4_headers": "node ./scripts/qode.js dist/examples/modelview_4_headers.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.x.x"
|
||||
|
||||
@ -87,6 +87,21 @@ class DLL_EXPORT NAbstractItemModel : public QAbstractItemModel, public EventWid
|
||||
return result;
|
||||
}
|
||||
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override {
|
||||
Napi::Env env = this->dispatchOnNode.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
auto sectionValue = Napi::Value::From(env, static_cast<int>(section));
|
||||
auto orientationValue = Napi::Value::From(env, static_cast<int>(orientation));
|
||||
auto roleValue = Napi::Value::From(env, static_cast<int>(role));
|
||||
|
||||
Napi::Value variantJsObject = this->dispatchOnNode.Call({Napi::String::New(env, "headerData"), sectionValue, orientationValue, roleValue});
|
||||
|
||||
QVariantWrap* variantWrap = Napi::ObjectWrap<QVariantWrap>::Unwrap(variantJsObject.As<Napi::Object>());
|
||||
QVariant* variant = variantWrap->getInternalInstance();
|
||||
return *variant;
|
||||
}
|
||||
|
||||
QModelIndex _protected_createIndex(int row, int column) const {
|
||||
return createIndex(row, column);
|
||||
}
|
||||
|
||||
44
src/examples/modelview_4_headers.ts
Normal file
44
src/examples/modelview_4_headers.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { ItemDataRole, Orientation, QAbstractTableModel, QModelIndex, QTableView, QVariant } from '..';
|
||||
|
||||
function main(): void {
|
||||
const tableView = new QTableView();
|
||||
const model = new MyModel();
|
||||
tableView.setModel(model);
|
||||
|
||||
tableView.show();
|
||||
|
||||
(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 {
|
||||
if (role === ItemDataRole.DisplayRole) {
|
||||
return new QVariant(`Row${index.row() + 1}, Column${index.column() + 1}`);
|
||||
}
|
||||
return new QVariant();
|
||||
}
|
||||
|
||||
headerData(section: number, orientation: Orientation, role: number): QVariant {
|
||||
if (role == ItemDataRole.DisplayRole && orientation == Orientation.Horizontal) {
|
||||
switch (section) {
|
||||
case 0:
|
||||
return new QVariant('first');
|
||||
case 1:
|
||||
return new QVariant('second');
|
||||
case 2:
|
||||
return new QVariant('third');
|
||||
}
|
||||
}
|
||||
return new QVariant();
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
@ -3,7 +3,7 @@ import { NativeElement } from '../core/Component';
|
||||
import { NodeObject, QObjectSignals } from '../QtCore/QObject';
|
||||
import { QModelIndex } from './QModelIndex';
|
||||
import { QVariant } from './QVariant';
|
||||
import { ItemDataRole, ItemFlag } from '../QtEnums';
|
||||
import { ItemDataRole, ItemFlag, Orientation } from '../QtEnums';
|
||||
|
||||
export interface QAbstractItemSignals extends QObjectSignals {
|
||||
// itemChanged: (item: QStandardItem) => void;
|
||||
@ -71,6 +71,15 @@ export class QAbstractItemModel extends NodeObject<any> {
|
||||
}
|
||||
return ItemFlag.NoItemFlags;
|
||||
|
||||
case 'headerData':
|
||||
try {
|
||||
return this.headerData(args[0], args[1], args[2]).native;
|
||||
} catch (e) {
|
||||
console.log(`An exception was thrown while dispatching to method 'headerData':`);
|
||||
console.log(e);
|
||||
}
|
||||
return new QVariant().native;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
@ -114,4 +123,8 @@ export class QAbstractItemModel extends NodeObject<any> {
|
||||
emitDataChanged(topLeft: QModelIndex, bottomRight: QModelIndex, roles: ItemDataRole[]): void {
|
||||
this.native.emitDataChanged(topLeft.native, bottomRight.native, roles);
|
||||
}
|
||||
|
||||
headerData(section: number, orientation: Orientation, role: number): QVariant {
|
||||
return new QVariant();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user