Demo1 - Advanced menus

This first demo is a basic introduction to the concept of qmdiActionGroup, qmdiActionGroupList and qmdiClient.

Some applications have the concept of advanced menus: by default they only display a limited set of functionality to the user, and only after he chooses they show the full set of commands. This example shows how you can modify on the fly another set of commands into your application, and do this in your applications.

To do this using plain Qt classes, the way for you to do this is to disable a lot of commands. The setup is really hard to implement if you want to switch between several states, and implementing a state machine. With this library, those things can become trivial.

adv-menus-2.jpg

We start by defining the default menus used on this application, on a main window which derives QMainWindow and qmdiHost . Note that in this example, empty menus are declared in the beginning of the application, otherwise the new menus would have been appended to the end of the menu bar. The empty menus will not be displayed on the main window.

class MainWindow: public QMainWindow, public qmdiHost
{
Q_OBJECT
        public:
        MainWindow( QWidget *owner=NULL );
        ~MainWindow();
private:
        QAction *actionQuit;
        QAction *actionShowAll;
        QAction *actionFileNew, *actionFileSave, *actionFileSaveAs;
        QAction *actionAbout, *actionAboutQt;
        QAction *test1, *test2;
        
        qmdiClient *advanced;
};

MainWindow::MainWindow( QWidget *owner )
{
        menus["&File"]->addAction( actionFileNew );
        menus["&File"]->addAction( actionFileSave );
        menus["&File"]->addSeparator();
        menus["&File"]->addAction( actionQuit );
        menus["&Edit"];
        menus["&Test"];
        menus["&Settings"]->addAction( actionShowAll );
        menus["&Help"]->addAction( actionAbout );
        
        // toolbars
        toolbars["Main"]->addAction( actionShowAll );

and also the advanced menus:

...
        advanced = new qmdiClient;
        advanced->menus["&File"]->addAction( actionFileSaveAs );
        advanced->menus["&Test"]->addAction( test1 );
        advanced->menus["&Test"]->addAction( test2 );
        advanced->menus["&Help"]->addAction( actionAboutQt );
        
        // extra toolbars
        advanced->toolbars["Main"]->addAction( actionQuit );
        advanced->toolbars["File operations"]->addAction( test1 );
        advanced->toolbars["File operations"]->addAction( test2 );
        
        updateGUI( this );
}

Then, all you need to do is merge or un-merge the new client, to add or remove menus and toolbars from the GUI:

void MainWindow::showMenus()
{
        bool isChecked = actionShowAll->isChecked();

        if (isChecked)
                mergeClient( advanced );
        else
                unmergeClient( advanced );

        // show the stuff on screen
        updateGUI( this );
}

See also:
Next: Demo2 - Using the new tab widget to display dynamic menus

Generated on Fri Jun 29 21:56:34 2007 for qmdilib by  doxygen 1.5.1