Merge pull request #30 from nodegui/feature/QTextEdit

Add textChange event for QPlainTextEdit
This commit is contained in:
Atul R 2019-08-19 22:13:04 +02:00 committed by GitHub
commit c0cc5da3a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 3 deletions

View File

@ -30,4 +30,14 @@ QPlainTextEdit can access all the instance properties defined in [NodeWidget](ap
### Instance Methods
QPlainTextEdit can access all the instance methods defined in [NodeWidget](api/NodeWidget.md).
QPlainTextEdit can access all the instance methods defined in [NodeWidget](api/NodeWidget.md).
#### [`plainTextEdit.setPlainText(text)`](https://doc.qt.io/qt-5/qplaintextedit.html#setPlainText)
Sets the given text to the plainTextEdit.
- `text` string
#### [`plainTextEdit.toPlainText()`](https://doc.qt.io/qt-5/qplaintextedit.html#toPlainText)
Returns the text of the text edit as plain text.

View File

@ -1,5 +1,6 @@
#pragma once
#include "deps/spdlog/spdlog.h"
#include <QPlainTextEdit>
#include "src/cpp/core/NodeWidget/nodewidget.h"
@ -8,4 +9,13 @@ class NPlainTextEdit : public QPlainTextEdit, public NodeWidget
NODEWIDGET_IMPLEMENTATIONS
public:
using QPlainTextEdit::QPlainTextEdit; //inherit all constructors of QPlainTextEdit
void connectWidgetSignalsToEventEmitter() {
// Qt Connects: Implement all signal connects here
QObject::connect(this, &QPlainTextEdit::textChanged, [=]() {
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
this->emitOnNode.Call({Napi::String::New(env, "textChanged")});
});
}
};

View File

@ -12,6 +12,7 @@ Napi::Object QPlainTextEditWrap::init(Napi::Env env, Napi::Object exports) {
char CLASSNAME[] = "QPlainTextEdit";
Napi::Function func = DefineClass(env, CLASSNAME, {
InstanceMethod("setPlainText",&QPlainTextEditWrap::setPlainText),
InstanceMethod("toPlainText",&QPlainTextEditWrap::toPlainText),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QPlainTextEditWrap)
});
constructor = Napi::Persistent(func);
@ -51,3 +52,9 @@ Napi::Value QPlainTextEditWrap::setPlainText(const Napi::CallbackInfo& info){
this->instance->setPlainText(plainText.Utf8Value().c_str());
return env.Null();
}
Napi::Value QPlainTextEditWrap::toPlainText(const Napi::CallbackInfo &info){
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Value::From(env, this->instance->toPlainText().toStdString());
}

View File

@ -19,5 +19,6 @@ class QPlainTextEditWrap : public Napi::ObjectWrap<QPlainTextEditWrap>{
QWIDGET_WRAPPED_METHODS_DECLARATION
Napi::Value setPlainText(const Napi::CallbackInfo& info);
Napi::Value toPlainText(const Napi::CallbackInfo &info);
};

View File

@ -9,7 +9,8 @@ import {
FlexLayout,
QWidget,
QIcon,
QPlainTextEdit
QPlainTextEdit,
QPlainTextEditEvents
} from "./index";
const path = require("path");
@ -28,6 +29,7 @@ const lineEdit = new QLineEdit();
lineEdit.setPlaceholderText("Enter your thoughts here");
lineEdit.setObjectName("editable");
const button = new QPushButton();
button.setText("Push Push Push!");
button.setObjectName("btn");
@ -49,9 +51,17 @@ radioButton.setText("Roger that!");
const rootView = new QWidget();
rootView.setObjectName("root");
rootView.setLayout(new FlexLayout());
const lineEditLabel = new QLabel();
lineEditLabel.setInlineStyle("font-size: 12px;");
lineEditLabel.setText("PlainTextEdit's bound Value");
const textEdit = new QPlainTextEdit();
textEdit.setPlainText("Hello");
textEdit.addEventListener(QPlainTextEditEvents.textChanged, (value: string) => {
lineEditLabel.setText(textEdit.toPlainText());
});
if (rootView.layout) {
rootView.layout.addWidget(label);
rootView.layout.addWidget(checkbox);
@ -60,6 +70,7 @@ if (rootView.layout) {
rootView.layout.addWidget(button);
rootView.layout.addWidget(progressbar);
rootView.layout.addWidget(textEdit);
rootView.layout.addWidget(lineEditLabel);
}
win.setCentralWidget(rootView);

View File

@ -4,8 +4,10 @@ import { BaseWidgetEvents } from "../../core/EventWidget";
import { NativeElement } from "../../core/Component";
export const QPlainTextEditEvents = Object.freeze({
...BaseWidgetEvents
...BaseWidgetEvents,
textChanged: "textChanged",
});
export class QPlainTextEdit extends NodeWidget {
native: NativeElement;
constructor(parent?: NodeWidget) {
@ -20,8 +22,12 @@ export class QPlainTextEdit extends NodeWidget {
this.parent = parent;
// bind member functions
this.setPlainText.bind(this);
this.toPlainText.bind(this);
}
setPlainText(text: string | number) {
this.native.setPlainText(`${text}`);
}
toPlainText() {
return this.native.toPlainText();
}
}