add setFont() and font() to QWidget and font() to QAction (#327)

* add setFont() and font() to QWidget and font() to QAction

* fix QAction constructor weight type

* Fix for native element constructor QFont

Co-authored-by: Atul R <atulanand94@gmail.com>
This commit is contained in:
slidinghotdog 2020-01-05 11:31:50 -03:00 committed by Atul R
parent fd270c18ac
commit 1ed3172f9e
4 changed files with 46 additions and 23 deletions

View File

@ -1,23 +1,32 @@
import { QMessageBox } from './index';
import { QToolButton } from './lib/QtWidgets/QToolButton';
import { FlexLayout, QMainWindow, QWidget } from './index';
import { QLabel } from './lib/QtWidgets/QLabel';
import { QFont, QFontCapitalization } from './lib/QtGui/QFont';
import { QPushButton } from './lib/QtWidgets/QPushButton';
const msgBox = new QMessageBox();
// const msgBoxButton = msgBox.addButton('Another button');
// console.log(msgBoxButton);
msgBox.setText('hellllooo');
const btn = new QToolButton();
btn.setText('yolo');
msgBox.addButton(btn);
msgBox.addEventListener('buttonClicked', rawButtonPtr => {
console.log(rawButtonPtr);
const btn2 = new QToolButton(rawButtonPtr);
btn2.setText('Helloooo');
const newMsg = new QMessageBox();
newMsg.exec();
const win = new QMainWindow();
const center = new QWidget();
const layout = new FlexLayout();
center.setLayout(layout);
const label = new QLabel();
const font = new QFont('Mono', 14);
label.setFont(font);
label.setText('label 1');
const btn = new QPushButton();
btn.setText('Change font');
btn.setFont(new QFont('Helvetica', 20));
btn.addEventListener('clicked', () => {
const font2 = label.font();
font2.setCapitalization(QFontCapitalization.AllUppercase);
font2.setItalic(true);
font2.setFamily('Times');
console.log('point', font2.pointSize());
label.setFont(font2);
});
msgBox.exec();
layout.addWidget(label);
layout.addWidget(btn);
(global as any).msg = msgBox;
win.setCentralWidget(center);
win.resize(400, 400);
setInterval(() => null, 1000);
win.show();
(global as any).win = win;

View File

@ -1,6 +1,7 @@
import { Component, NativeElement } from '../core/Component';
import addon from '../utils/addon';
import { QVariant } from '../QtCore/QVariant';
import { checkIfNativeElement } from '../utils/helpers';
export enum QFontStretch {
AnyStretch = 0,
@ -36,11 +37,14 @@ export enum QFontWeight {
export class QFont extends Component {
native: NativeElement;
constructor();
constructor(font?: QFont);
constructor(family?: string, pointSize?: number, weight?: number, italic?: boolean);
constructor(arg?: QFont | string, pointSize = -1, weight = -1, italic = false) {
constructor(font: QFont);
constructor(native: NativeElement);
constructor(family: string, pointSize?: number, weight?: QFontWeight, italic?: boolean);
constructor(arg?: QFont | string | NativeElement, pointSize = -1, weight = -1, italic = false) {
super();
if (arg instanceof QFont) {
if (checkIfNativeElement(arg)) {
this.native = arg as NativeElement;
} else if (arg instanceof QFont) {
this.native = arg.native;
} else if (typeof arg === 'string') {
this.native = new addon.QFont(arg, pointSize, weight, italic);

View File

@ -96,4 +96,7 @@ export class QAction extends NodeObject<QActionSignals> {
setFont(font: QFont): void {
this.native.setFont(font.native);
}
font(): QFont {
return QFont.fromQVariant(this.property('font'));
}
}

View File

@ -12,6 +12,7 @@ import { YogaWidget } from '../core/YogaWidget';
import { QSize } from '../QtCore/QSize';
import { QRect } from '../QtCore/QRect';
import { QObjectSignals } from '../QtCore/QObject';
import { QFont } from '../QtGui/QFont';
/**
@ -205,6 +206,12 @@ export abstract class NodeWidget<Signals extends QWidgetSignals> extends YogaWid
showNormal(): void {
this.native.showNormal();
}
setFont(font: QFont): void {
this.native.setProperty('font', font.native);
}
font(): QFont {
return QFont.fromQVariant(this.property('font'));
}
}
export interface QWidgetSignals extends QObjectSignals {