CopperSpice API  1.9.1
Application Main Window

List of widgets used to create the main window. More...

Classes

class  QAction
 Stores information about a process or command which can be added to a menu or a toolbar More...
 
class  QActionGroup
 Groups actions together More...
 
class  QDockWidget
 Widget that can be docked inside a QMainWindow or floated as a top-level window More...
 
class  QMainWindow
 QMainWindow class provides the main GUI application window More...
 
class  QMdiArea
 Area in which MDI windows are displayed More...
 
class  QMdiSubWindow
 Subwindow class for QMdiArea More...
 
class  QMenu
 Provides a menu widget for use in menu bars, context menus, and other popup menus More...
 
class  QMenuBar
 Horizontal menu bar More...
 
class  QSizeGrip
 Resize top level windows More...
 
class  QStatusBar
 Horizontal area for presenting status information More...
 
class  QToolBar
 Provides a movable panel that contains a set of controls More...
 
class  QWidgetAction
 Extends QAction by an interface for inserting custom widgets into action based containers, such as toolbars More...
 

Detailed Description

These classes provide the components which can be used on the top level application window like menus, toolbars, and a status bar.

Main Window Classes

CopperSpice provides the following classes for managing main windows and user interface components.

QMainWindow
Central class used to create GUI applications
QDockWidget
Used to create detachable tool palettes or other windows
QToolBar
Provides a generic toolbar widget which holds different action related widgets, such as buttons, drop-down menus, combo boxes, and spin boxes

Example

In this example we will declare a new class called MyMainWIndow which inherits from QMainWindow. A menu, toolbar, and a dock widget will be added.

To add a menu to the main window first create the menu actions and then add them to the menu. The QMainWindow::menuBar() method automatically creates the menu bar the first time it is called. You can also call QMainWindow::setMenuBar() to use a custom menu bar.

MyMainWindow::MyMainWindow(QWidget *parent)
: public QMainWindow(parent)
{
// create actions
newAct = new QAction(tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, &QAction::triggered, this, &MyMainWindow::newFile);
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, &QAction::triggered, this, &MyMainWindow::open);
// add the actions to the menu bar
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addSeparator();

Next a toolbar is created as a child of the main window.

// create tool bar
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
fileToolBar->addAction(openAct);
// add toolbar to main window
fileToolbar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
addToolBar(Qt::TopToolBarArea, fileToolbar);

In this example the toolbar is restricted to the top and bottom toolbar areas of the main window. It is initially placed in the top toolbar area. The actions specified by newAct and openAct will be displayed both on the toolbar and in the file menu.

The last step is to create a dock widget as a child of the main window and then add widgets as children of the dock widget.

contentsWindow = new QDockWidget(tr("Table of Contents"), this);
contentsWindow->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::LeftDockWidgetArea, contentsWindow);
headingList = new QListWidget(contentsWindow);
contentsWindow->setWidget(headingList);

In this example the dock widget can only be placed in the left and right dock areas. It is initially placed in the left dock area. The QMainWindow API allows the programmer to customize which dock widget areas occupy the four corners of the main window. The default can be changed by calling QMainWindow::setCorner().

setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);

Main Window Layout

The following diagram shows the configuration produced by the above example. The left and right dock widgets will occupy the top and bottom corners of the main window in this layout.

Once all of the main window components have been set up, the central widget is created and set by using code similar to the following code. The central widget can be any subclass of QWidget.

QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);