therecipe / qt

Qt binding for Go (Golang) with support for Windows / macOS / Linux / FreeBSD / Android / iOS / Sailfish OS / Raspberry Pi / AsteroidOS / Ubuntu Touch / JavaScript / WebAssembly
GNU Lesser General Public License v3.0
10.48k stars 748 forks source link

Application crashed after adding a menu bar? #760

Open quantonganh opened 5 years ago

quantonganh commented 5 years ago

My application is working fine. Then I tried to add a menu bar like this:

 import QtQuick 2.8
 import QtQuick.Controls 2.1
 import QtQuick.Window 2.2

 Window {
     id: root
     visible: true
     width: UI.windowWidth
     height: 570
     minimumHeight: height
     maximumWidth: width
     maximumHeight: height
+    menuBar: MenuBar {
+        Menu {
+            id: helpMenu
+            title: "Help"
+
+            MenuItem {
+                text: "Check for Updates..."
+                onTriggered: __controller.checkForUpdates()
+            }
+        }
+    }
     Component.onCompleted: {
         UI.window = root
         UI.pageStack = pageStack

rebuild and my app crashed without any logs even enabled:

Why? How can I debug this?

quantonganh commented 5 years ago

I found the reason:

  1. ApplicationWindow type must be used instead of Window: http://doc.qt.io/qt-5/qml-qtquick-controls-applicationwindow.html

ApplicationWindow is a Window that adds convenience for positioning items, such as MenuBar, ToolBar, and StatusBar in a platform independent manner.

  1. Don't know why I have to use import QtQuick.Controls 1.4 to make menu bar works.
arknable commented 5 years ago

@quantonganh it shouldn't crashed though, usually just a javascript error. @therecipe probably there's a bug in the binding ?

therecipe commented 5 years ago

@quantonganh

You can probably also use QtQuick.Controls 2.4

This patch should make the menuBar work for https://github.com/therecipe/examples/tree/master/basic/qml as well

diff --git a/basic/qml/qml/main.qml b/basic/qml/qml/main.qml
index b7d4682..5aff65b 100644
--- a/basic/qml/qml/main.qml
+++ b/basic/qml/qml/main.qml
@@ -1,5 +1,5 @@
 import QtQuick 2.7         //ApplicationWindow
-import QtQuick.Controls 2.1    //Dialog
+import QtQuick.Controls 2.4    //Dialog

 ApplicationWindow {
    id: window
@@ -9,6 +9,18 @@ ApplicationWindow {
    minimumWidth: 250
    minimumHeight: 200

+   menuBar: MenuBar {
+       Menu {
+           id: helpMenu
+           title: "Help"
+
+           MenuItem {
+               text: "Check for Updates..."
+               onTriggered: console.log("test")
+           }
+       }
+   }
+
    Column {
        anchors.centerIn: parent

rebuild and my app crashed without any logs even enabled: QT_DEBUG QT_DEBUG_CONSOLE QT_DEBUG_PLUGINS Why? How can I debug this?

Usually the need to explicitly export QT_DEBUG_CONSOLE=true on windows should be gone now for qtdeploy -fast or plain go run/build builds. It is only necessary now, if you want to have the debug console for "full" deployment builds like this qtdeploy build && .\deploy\windows\${APPNAME}.exe

Can you get "println" to print something to the console before it crashes?

@arknable

I'm not sure, but as you said this usually should just result in an js error. For example if I run the code from above with import QtQuick.Controls 2.1 instead of import QtQuick.Controls 2.4 I simply get

QQmlApplicationEngine failed to load component
qrc:/qml/main.qml:12 MenuBar is not a type

as expected.