Added a center method on QMainWindow (#175)

Closes #134
This commit is contained in:
Dimitar Nestorov 2019-11-03 21:11:04 +02:00 committed by Atul R
parent ca1ef6540b
commit ca2977f3e3
4 changed files with 55 additions and 8 deletions

View File

@ -20,6 +20,7 @@ public:
Napi::Value setMenuBar(const Napi::CallbackInfo& info);
Napi::Value menuBar(const Napi::CallbackInfo& info);
Napi::Value setMenuWidget(const Napi::CallbackInfo& info);
Napi::Value center(const Napi::CallbackInfo &info);
QWIDGET_WRAPPED_METHODS_DECLARATION
};

View File

@ -1,3 +1,5 @@
#include <QApplication>
#include <QDesktopWidget>
#include "QtWidgets/QMainWindow/qmainwindow_wrap.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
#include "QtWidgets/QMenuBar/qmenubar_wrap.h"
@ -13,6 +15,7 @@ Napi::Object QMainWindowWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("setCentralWidget",&QMainWindowWrap::setCentralWidget),
InstanceMethod("setMenuBar",&QMainWindowWrap::setMenuBar),
InstanceMethod("setMenuWidget",&QMainWindowWrap::setMenuWidget),
InstanceMethod("center",&QMainWindowWrap::center),
});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
@ -84,3 +87,20 @@ Napi::Value QMainWindowWrap::setMenuWidget(const Napi::CallbackInfo& info){
return env.Null();
}
Napi::Value QMainWindowWrap::center(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto window = this->getInternalInstance();
// https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen
window->setGeometry(
QStyle::alignedRect(
Qt::LeftToRight,
Qt::AlignCenter,
window->size(),
QApplication::desktop()->availableGeometry(window)
)
);
return env.Null();
}

View File

@ -10,6 +10,9 @@ import { QKeySequence } from "./lib/QtGui/QKeySequence";
import { ShortcutContext } from "./lib/QtEnums";
import { QMenuBar } from "./lib/QtWidgets/QMenuBar";
import { QShortcut, QShortcutEvents } from "./lib/QtWidgets/QShortcut";
import { QPushButton } from "./lib/QtWidgets/QPushButton";
import { FlexLayout } from "./lib/core/FlexLayout";
import { QWidget } from "./lib/QtWidgets/QWidget";
const win = new QMainWindow();
const shortcut = new QShortcut(win);
@ -93,16 +96,36 @@ quitAction.addEventListener("triggered", () => {
menu.addAction(quitAction);
menu.setTitle("TestMenu");
const label = new QLabel();
label.setOpenExternalLinks(true);
label.setText(`<a href="https://www.google.com">Google</a>`);
label.show();
(global as any).label = label;
win.setWindowTitle("NodeGUI Demo");
win.resize(400, 700);
win.show();
// menuBar.addMenu(menu);
const button = new QPushButton();
button.setText("Center");
button.addEventListener("clicked", () => {
win.center();
});
const rootView = new QWidget();
rootView.setObjectName("root");
rootView.setLayout(new FlexLayout());
if (rootView.layout) {
rootView.layout.addWidget(button);
}
win.setCentralWidget(rootView);
win.setStyleSheet(`
#root {
flex: 1;
height: '100%';
align-items: 'center';
justify-content: 'center';
}
`);
win.setWindowTitle("NodeGUI Demo");
win.show();
win.resize(400, 700);
const qApp = QApplication.instance();
qApp.setQuitOnLastWindowClosed(false);

View File

@ -56,4 +56,7 @@ export class QMainWindow extends NodeWidget {
return super.layout;
}
}
center() {
this.native.center();
}
}