Can't get menu bar in Qt Quick Components, using C++ and QQmlApplicationEngine



  • Now that Ubuntu finally has Qt 5.2, I'm playing with Qt Quick Components, and right now I'm having issues trying to get the menu bar to show, when I try to implement the menubar I get this message:

    appmenu-qt: handleReparent 128 The given QWindow has no QMenuBar assigned

    This is how I'm launching my Qml from C++:
    @
    // First initialize Qt Application
    QGuiApplication app(argc, argv);
    // Second initialize config singleton
    Config::init();
    QQmlApplicationEngine engine;
    engine.load(QUrl::fromLocalFile(config->getBinDir() + "/qml/main.qml"));
    QObject *topLevel = engine.rootObjects().value(0);
    QQuickWindow mainWindow = qobject_cast<QQuickWindow>(topLevel);

    mainWindow->show();
    return app.exec();
    @

    And this is the Qml I have so far:
    @
    import QtQuick.Controls 1.1
    import QtQuick.Dialogs 1.1
    import QtQuick 2.2

    ApplicationWindow {
    visible: true
    width: 640
    height: 480
    minimumWidth: 400
    minimumHeight: 300

    title: "MyApp"

    // Actions for menu items
    Item {
    id: globalActions

    Action {
    id: quitAction
    text: i18n.tr("Quit");
    onTriggered: Qt.quit();
    }

    Action {
    id: prefAction
    text: i18n.tr("Preferences");
    //onTriggered: TODO - LAUNCH PREFERENCES
    }

    Action {
    id: aboutAction
    text: i18n.tr("About");
    //onTriggered: TODO - LAUNCH ABOUT
    }
    }

    MessageDialog {
    id: aboutBox
    title: "MyApp"
    text: "This is a test app for Qt Quick Components"
    icon: StandardIcon.Information
    }

    menuBar: MenuBar {
    Menu {
    title: "&File"
    MenuItem { action: quitAction }
    }
    Menu {
    title: "&Edit"
    MenuItem { action: prefAction }
    }
    Menu {
    title: "&Help"
    MenuItem { action: aboutAction }
    }
    }
    }
    @



  • Can you check, whether your window is really a window?

    @ QQuickWindow * window = qobject_cast<QQuickWindow *>(topLevel);
    if ( !window ) {
    qWarning("Error: Your root item has to be a Window.");
    return -1;
    }@

    My code is very similar to yours, except for the Config::init command. I previously had trouble with doing init stuff in an order that the QT framework somehow did not like.



  • Thanks for the reply, I added if(!mainWindow), but it evaluates to true, so it is really a window. For arguments sake I commented out the config->init, and it didn't make a difference.

    If you have code that works, could you post just that portion of the code?



  • I should also note that the window itself does show, and any components (such as a text area) I put in it will show as well, just not the menu bar.

    In case it makes a difference I'm on Ubuntu 14.04


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.