Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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();
}

Post a Comment for "QT HTML5 With Menu Bar Like Real Program"