Added support for setRow/ColumnCount (#578)

This commit is contained in:
Adam Gastineau 2020-05-25 07:08:28 -07:00 committed by GitHub
parent a0b28d00e8
commit cab30f4822
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 18 deletions

View File

@ -53,6 +53,10 @@ class DLL_EXPORT QTableWidgetWrap : public Napi::ObjectWrap<QTableWidgetWrap> {
Napi::Value sortByColumn(const Napi::CallbackInfo& info);
Napi::Value setColumnWidth(const Napi::CallbackInfo& info);
Napi::Value setRowHeight(const Napi::CallbackInfo& info);
Napi::Value columnCount(const Napi::CallbackInfo& info);
Napi::Value rowCount(const Napi::CallbackInfo& info);
Napi::Value setColumnCount(const Napi::CallbackInfo& info);
Napi::Value setRowCount(const Napi::CallbackInfo& info);
Napi::Value setSortingEnabled(const Napi::CallbackInfo& info);
Napi::Value isSortingEnabled(const Napi::CallbackInfo& info);
};

View File

@ -52,6 +52,10 @@ Napi::Object QTableWidgetWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("sortByColumn", &QTableWidgetWrap::sortByColumn),
InstanceMethod("setColumnWidth", &QTableWidgetWrap::setColumnWidth),
InstanceMethod("setRowHeight", &QTableWidgetWrap::setRowHeight),
InstanceMethod("columnCount", &QTableWidgetWrap::columnCount),
InstanceMethod("rowCount", &QTableWidgetWrap::rowCount),
InstanceMethod("setColumnCount", &QTableWidgetWrap::setColumnCount),
InstanceMethod("setRowCount", &QTableWidgetWrap::setRowCount),
InstanceMethod("setSortingEnabled",
&QTableWidgetWrap::setSortingEnabled),
InstanceMethod("isSortingEnabled", &QTableWidgetWrap::isSortingEnabled),
@ -429,6 +433,36 @@ Napi::Value QTableWidgetWrap::setRowHeight(const Napi::CallbackInfo& info) {
this->instance->setRowHeight(row, height);
return env.Null();
}
Napi::Value QTableWidgetWrap::columnCount(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int count = static_cast<int>(this->instance->columnCount());
return Napi::Number::New(env, count);
}
Napi::Value QTableWidgetWrap::rowCount(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int count = static_cast<int>(this->instance->rowCount());
return Napi::Number::New(env, count);
}
Napi::Value QTableWidgetWrap::setColumnCount(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int count = info[0].As<Napi::Number>().Int32Value();
this->instance->setColumnCount(count);
return env.Null();
}
Napi::Value QTableWidgetWrap::setRowCount(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int count = info[0].As<Napi::Number>().Int32Value();
this->instance->setRowCount(count);
return env.Null();
}
Napi::Value QTableWidgetWrap::setSortingEnabled(
const Napi::CallbackInfo& info) {

View File

@ -1,15 +1,9 @@
import { QTime } from '.';
import { DateFormat } from './lib/QtEnums';
//const date = QDate.fromString('04132020', 'MMddyyyy');
const { QMainWindow, QTreeWidgetItem, QTreeWidget, QDate, ItemDataRole, QDateTime } = require('./index');
import { QMainWindow, QTableWidget, QTableWidgetItem, QWidget, QBoxLayout } from '.';
import { QPushButton } from './lib/QtWidgets/QPushButton';
const win = new QMainWindow();
win.resize(500, 500);
const tree = new QTreeWidget();
tree.setSortingEnabled(true);
tree.setHeaderLabels(['Date', 'Time', 'Test Column']);
tree.setColumnWidth(1, 15); //Sets the size of the selected column (index, size).
const table = new QTableWidget(0, 0);
const dates = [
'11/22/1973 02:55:43 AM',
@ -37,14 +31,41 @@ const dates = [
'11/07/1964 04:43:42 PM',
'03/26/1957 06:40:36 AM',
];
for (const date of dates) {
const item = new QTreeWidgetItem();
const dateObject = QDate.fromString(date.split(' ')[0], 'MM/dd/yyyy');
const timeObject = QTime.fromString(date.split(' ')[1] + ' ' + date.split(' ')[2], 'hh:mm:ss AP');
item.setData(0, ItemDataRole.DisplayRole, dateObject.native);
item.setData(1, ItemDataRole.DisplayRole, timeObject.native);
tree.addTopLevelItem(item);
}
win.setCentralWidget(tree);
table.setColumnCount(2);
table.setRowCount(dates.length);
table.setHorizontalHeaderLabels(['Date', 'Time']);
const buildDateStrings = (date: string) => {
const dateString = date.split(' ')[0];
const timeString = date.split(' ')[1] + ' ' + date.split(' ')[2];
return [dateString, timeString];
};
dates.forEach((date, row) => {
const [dateString, timeString] = buildDateStrings(date);
table.setItem(row, 0, new QTableWidgetItem(dateString));
table.setItem(row, 1, new QTableWidgetItem(timeString));
});
const centralWidget = new QWidget();
const boxLayout = new QBoxLayout(0);
const button = new QPushButton();
button.setText('Add row');
button.addEventListener('clicked', () => {
const rowCount = table.rowCount();
table.setRowCount(rowCount + 1);
const date = new Date().toDateString();
const [dateString, timeString] = buildDateStrings(date);
table.setItem(rowCount, 0, new QTableWidgetItem(dateString));
table.setItem(rowCount, 1, new QTableWidgetItem(timeString));
});
boxLayout.addWidget(table);
boxLayout.addWidget(button);
centralWidget.setLayout(boxLayout);
win.setCentralWidget(centralWidget);
win.show();
(global as any).win = win;

View File

@ -153,6 +153,18 @@ export class QTableWidget extends QAbstractScrollArea<QTableWidgetSignals> {
setRowHeight(row: number, height: number): void {
this.native.setRowHeight(row, height);
}
columnCount(): number {
return this.native.columnCount();
}
rowCount(): number {
return this.native.rowCount();
}
setColumnCount(count: number): void {
this.native.setColumnCount(count);
}
setRowCount(count: number): void {
this.native.setRowCount(count);
}
setSortingEnabled(enable: boolean): void {
this.native.setSortingEnabled(enable);
}