Update QTreeWidget (#474)

* Update QTreeWidget

Fixed a bug with QTreeWidget::currentItem() crashing when nothing is selected and added QTreeWidget::takeTopLevelItem() and QTreeWidget::clear()

* Fix garbage collection

* Fix garbage collection
This commit is contained in:
mspencer92 2020-03-30 14:03:24 -04:00 committed by GitHub
parent 3e0d2c35cd
commit 117be556cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 11 deletions

View File

@ -36,10 +36,9 @@ class DLL_EXPORT QTreeWidgetWrap : public Napi::ObjectWrap<QTreeWidgetWrap> {
Napi::Value setItemWidget(const Napi::CallbackInfo &info);
Napi::Value currentItem(const Napi::CallbackInfo &info);
Napi::Value findItems(const Napi::CallbackInfo &info);
Napi::Value takeTopLevelItem(const Napi::CallbackInfo &info);
Napi::Value clear(const Napi::CallbackInfo &info);
// Napi::Value addTopLevelItems(const Napi::CallbackInfo& info);
// Napi::Value setHorizontalScrollBarPolicy(const Napi::CallbackInfo& info);
// Napi::Value setVerticalScrollBarPolicy(const Napi::CallbackInfo& info);
// Napi::Value takeTopLevelItem(const Napi::CallbackInfo& info);
// Napi::Value findItems(const Napi::CallbackInfo& info);
};

View File

@ -26,6 +26,8 @@ Napi::Object QTreeWidgetWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("setItemWidget", &QTreeWidgetWrap::setItemWidget),
InstanceMethod("currentItem", &QTreeWidgetWrap::currentItem),
InstanceMethod("findItems", &QTreeWidgetWrap::findItems),
InstanceMethod("takeTopLevelItem", &QTreeWidgetWrap::takeTopLevelItem),
InstanceMethod("clear", &QTreeWidgetWrap::clear),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QTreeWidgetWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
@ -213,12 +215,15 @@ Napi::Value QTreeWidgetWrap::currentItem(const Napi::CallbackInfo& info) {
Napi::HandleScope scope(env);
QTreeWidgetItem* currentItem = this->instance->currentItem();
if (currentItem != nullptr) {
Napi::Object value = QTreeWidgetItemWrap::constructor.New(
{Napi::External<QTreeWidgetItem>::New(
env, new QTreeWidgetItem(*currentItem))});
Napi::Object value = QTreeWidgetItemWrap::constructor.New(
{Napi::External<QTreeWidgetItem>::New(
env, new QTreeWidgetItem(*currentItem))});
return value;
return value;
} else {
return env.Null();
}
}
Napi::Value QTreeWidgetWrap::findItems(const Napi::CallbackInfo& info) {
@ -242,3 +247,29 @@ Napi::Value QTreeWidgetWrap::findItems(const Napi::CallbackInfo& info) {
}
return napiItems;
}
Napi::Value QTreeWidgetWrap::takeTopLevelItem(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int index = info[0].As<Napi::Number>().Int32Value();
QTreeWidgetItem* itemRemoved = this->instance->takeTopLevelItem(index);
if (itemRemoved != nullptr) {
Napi::Object value = QTreeWidgetItemWrap::constructor.New(
{Napi::External<QTreeWidgetItem>::New(
env, new QTreeWidgetItem(*itemRemoved))});
return value;
} else {
return env.Null();
}
}
Napi::Value QTreeWidgetWrap::clear(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
this->instance->clear();
return env.Null();
}

View File

@ -100,6 +100,7 @@ export { QShortcut, QShortcutSignals } from './lib/QtWidgets/QShortcut';
export { QGroupBox, QGroupBoxSignals } from './lib/QtWidgets/QGroupBox';
export { QStatusBar, QStatusBarSignals } from './lib/QtWidgets/QStatusBar';
export { QStandardItemModel, QStandardItemModelSignals } from './lib/QtWidgets/QStandardItemModel';
export { QStandardItem } from './lib/QtWidgets/QStandardItem';
// Core
export { QDate } from './lib/QtCore/QDate';
export { QDateTime } from './lib/QtCore/QDateTime';

View File

@ -83,7 +83,7 @@ export class QTreeWidget extends QAbstractScrollArea<QTreeWidgetSignals> {
}
insertTopLevelItem(index: number, item: QTreeWidgetItem): void {
this.topLevelItems.add(item);
this.topLevelItems.add(item)
this.native.insertTopLevelItem(index, item.native);
}
@ -145,8 +145,13 @@ export class QTreeWidget extends QAbstractScrollArea<QTreeWidgetSignals> {
/**
* Returns the current item in the tree widget.
*/
currentItem(): QTreeWidgetItem {
return new QTreeWidgetItem(this.native.currentItem());
currentItem(): QTreeWidgetItem | void {
const item = this.native.currentItem();
if (item) {
return new QTreeWidgetItem(item);
} else {
return undefined;
}
}
/**
@ -163,6 +168,20 @@ export class QTreeWidget extends QAbstractScrollArea<QTreeWidgetSignals> {
return new QTreeWidgetItem(eachItem);
});
}
takeTopLevelItem(index: number): QTreeWidgetItem | void {
const item = this.native.takeTopLevelItem(index);
if (item) {
return new QTreeWidgetItem(item);
} else {
return undefined;
}
}
clear(): void {
this.topLevelItems.clear();
this.native.clear();
}
}
export interface QTreeWidgetSignals extends QAbstractScrollAreaSignals {