Skip to content

Commit c8d5c8a

Browse files
committed
Let C++ create and own the QMLBridge instance
It's a global singleton type, doesn't make sense to let QML manage it. Before this change, it was destroyed too early, causing warnings on exit like: TypeError: Cannot read property 'myData' of null
1 parent a7e4771 commit c8d5c8a

File tree

3 files changed

+6
-12
lines changed

3 files changed

+6
-12
lines changed

main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,13 @@ int main(int argc, char **argv)
109109

110110
migrateSettings();
111111

112+
QMLBridge qmlBridge;
113+
QQmlEngine::setObjectOwnership(&qmlBridge, QQmlEngine::CppOwnership);
114+
112115
// Register QMLBridge for Keypad<->Emu communication
113-
qmlRegisterSingletonType<QMLBridge>("Firebird.Emu", 1, 0, "Emu", qmlBridgeFactory);
116+
qmlRegisterSingletonType<QMLBridge>("Firebird.Emu", 1, 0, "Emu", [](QQmlEngine *, QJSEngine *) -> QObject* {
117+
return the_qml_bridge;
118+
});
114119
// Register QtFramebuffer for QML display
115120
qmlRegisterType<QMLFramebuffer>("Firebird.Emu", 1, 0, "EmuScreen");
116121
// Register KitModel

qmlbridge.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -687,14 +687,6 @@ void QMLBridge::notifyButtonStateChanged(int row, int col, bool state)
687687
emit buttonStateChanged(col + row * KEYPAD_COLS, state);
688688
}
689689

690-
QObject *qmlBridgeFactory(QQmlEngine *engine, QJSEngine *scriptEngine)
691-
{
692-
Q_UNUSED(engine)
693-
Q_UNUSED(scriptEngine)
694-
695-
return new QMLBridge();
696-
}
697-
698690
void QMLBridge::touchpadStateChanged()
699691
{
700692
touchpadStateChanged(float(keypad.touchpad_x)/TOUCHPAD_X_MAX, 1.0f-(float(keypad.touchpad_y)/TOUCHPAD_Y_MAX), keypad.touchpad_contact, keypad.touchpad_down);

qmlbridge.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,4 @@ public slots:
180180

181181
extern QMLBridge *the_qml_bridge;
182182

183-
QObject *qmlBridgeFactory(QQmlEngine *engine, QJSEngine *scriptEngine);
184-
185-
186183
#endif // QMLBRIDGE_H

0 commit comments

Comments
 (0)