Search code examples
c++htmlqtmenubar

QT HTML5 with Menu Bar like real program


I'm working on an QT HTML5 application and I was wondering how i could add an top menu just like normal program ( with the default file, tools, help... options ).

I think that I've to change something in the html5applicationviewer.cpp, but I've got 0 knowledge of that ( I'm learning this... )

Even if you could point me a little bit in the right direction, I'm grateful. I've searched around, but found absolutely nothing about this topic ( but maybe i didn't search right... )

If you need more info, please ask.


Solution

  • Simplest way to add normal "desktop"-style menus to a Qt app is to use QMainWindow which has good support for menus.

    Here's something to get you started. First I created the default HTML5 Qt application with Qt Creator (SDK version 5.2.1). Then I edited the main.cpp and added some lines. Result is below, original lines without comment and all added lines with comment.

    #include <QApplication>
    
    #include <QMainWindow> // added
    #include <QMenuBar> // added
    #include <QMenu> // added
    #include <QAction> // added
    
    #include "html5applicationviewer.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QMainWindow w; // important, viewer is in stack so w must be before it!
    
        Html5ApplicationViewer viewer;
    
        w.setCentralWidget(&viewer); // set viewer as the central widget
        QMenu *fileMenu = w.menuBar()->addMenu("&File"); // create file menu
        QAction *exitAction = fileMenu->addAction("&Exit"); // create exit action
        QObject::connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); // make the action do something
    
        viewer.setOrientation(Html5ApplicationViewer::ScreenOrientationAuto);
        //viewer.showExpanded(); // will be shown by main window
        viewer.loadFile(QLatin1String("html/index.html"));
    
        w.show(); // show main window
    
        return app.exec();
    }