Demo2 - Using the new tab widget to display dynamic menus

This second demo is an introduction to the qmdiTabWidget and using it in real life applications, which load several files of the same type.

The Qt libraries have some basic concept of multi document interface, but it gives you tools for doing this using top level windows. On modern applications we display the documents in QTabWidget and not top level windows. In theory, you just need to insert the new QWidgets into a QTabWidget, but then you start getting into problems when you have to synchronize the "save action" to the document currently displayed on the tab widget. The problems get bigger and harder when you are trying to insert different widget types into the QTabWidget, and you want each one to display different menus and toolbars.

Using qmdilib you just need to follow a very minimal step to have very rich and dynamic applications. You just need to define your own QMainWindow as we did on demo1, and instead of using QTabWidget , you need to use qmdiTabWidget. The widgets inserted into the tab, need also to inherit qmdiClient.

mdi-tab-2.jpg

This demo contains 3 widget types

Please note that when you move from one tab to another, the menus and toolbars will change. Sometimes, even if you move from an editor to another editor, it will look like the toolbars did not change, but what actually happens, is that the menus and toolbars are re-created on each move, and the QActions on the toolbars belong to the active widget. This means that each QWidget inserted into the qmdiTabWidget is responsible for it's own actions.

This demo has also some other compile time options. You can compile the demo to use instead of one toolbar (the default), different toolbars per mdi client. You can also use qmdiWorkspace instead of qmdiTabWidget. The definitions are in the header of the main window. Plase feel free to play with them.

Here are the definitions of the new text editor the help viewer and the main window:

class QexHelpBrowser: public QTextBrowser, public qmdiClient
{
        ...
}

class QexTextEdit : public QTextEdit, public qmdiClient
{
        ...
}

class MainWindow2: public QMainWindow, public qmdiHost
{
        ...
}

void MainWindow2::init_gui()
{
        // create own menus
        menus["&File"]->addAction( actionFileNew );
        menus["&File"]->addSeparator();
        menus["&File"]->addAction( actionQuit );
        menus["&Edit"];
        menus["&Navigation"];
        menus["&Search"];
        menus["&Configuration"];
        menus["&Help"]->addAction( actionQtTopics );
        menus["&Help"]->addAction( actionAbout );

        // toolbars
        toolbars["main"]->addAction( actionFileNew );
        toolbars["main"]->addAction( actionQtTopics );

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

        // make the tab widget
        tabWidget = new qmdiTabWidget;
        tabNewBtn = new QToolButton(tabWidget);
        tabNewBtn->setAutoRaise( true );
        connect( tabNewBtn, SIGNAL(clicked()), this, SLOT(fileNew()));
        tabNewBtn->setIcon(QIcon(":images/addtab.png"));

        tabCloseBtn = new QToolButton(tabWidget);
        tabCloseBtn->setAutoRaise( true );
        connect( tabCloseBtn, SIGNAL(clicked()), this, SLOT(fileClose()));
        tabCloseBtn->setIcon(QIcon(":images/closetab.png"));

        tabWidget->setCornerWidget( tabNewBtn, Qt::TopLeftCorner );
        tabWidget->setCornerWidget( tabCloseBtn, Qt::TopRightCorner  );
        setCentralWidget( tabWidget );
}

See also:
Demo1 - Advanced menus

Small tutorial and introduction

Next: Demo3 - Usage of QWorkspace inside qmdiTabWidget


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