cosas de qt11

Upload: grissgs

Post on 02-Mar-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/26/2019 Cosas de Qt11

    1/6

    Scrolling Areas

    The QScrollAreaclass provides a scrollable viewport and two scroll bars. If we want to add scroll bars to a widget, it is muchsimpler to use a QScrollAreathan to instantiate our own QScrollBars and implement the scrolling functionality ourselves.

    The way to use QScrollAreais to call setWidget()with the widget to which we want to add scroll bars. QScrollAreaautomatically reparents the widget to make it a child of the viewport (accessible through QScrollArea::viewport()) if itisn't already. For example, if we want scroll bars around the IconEditorwidget we developed in Chapter 5(as shown inFigure 6.11), we can write this:

    int main(int argc, char *argv[]){

    QApplication app(argc, argv); IconEditor *iconEditor = new IconEditor; iconEditor->setIconImage(QImage(":/images/mouse.png"));

    QScrollArea scrollArea; scrollArea.setWidget(iconEditor); scrollArea.viewport()->setBackgroundRole(QPalette::Dark); scrollArea.viewport()->setAutoFillBackground(true); scrollArea.setWindowTitle(QObject::tr("Icon Editor"));

    scrollArea.show(); return app.exec();}

    Figure 6.11. Resizing a QScrollArea

    The QScrollArea(shown schematically in Figure 6.12) presents the widget at its current size or uses the size hint if the widgethasn't been resized yet. By calling setWidgetResizable(true), we can tell QScrollAreato automatically resize thewidget to take advantage of any extra space beyond its size hint.

    Figure 6.12. QScrollArea's constituent widgets

    155

  • 7/26/2019 Cosas de Qt11

    2/6

    By default, the scroll bars are displayed only when the viewport is smaller than the child widget. We can force the scroll bars toalways be shown by setting scroll bar policies:

    scrollArea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);scrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

    QScrollAreainherits much of its functionality from QAbstractScrollArea. Classes such as QTextEditandQAbstractItemView(the base class of Qt's item view classes) derive from QAbstractScrollArea, so we don't need towrap them in a QScrollAreato get scroll bars.

    156

  • 7/26/2019 Cosas de Qt11

    3/6

    Dock Windows and Toolbars

    Dock windows are windows that can be docked inside a QMainWindowor floated as independent windows. QMainWindowprovides four dock window areas: one above, one below, one to the left, and one to the right of the central widget. Applicationssuch as Microsoft Visual Studio and Qt Linguist make extensive use of dock windows to provide a very flexible user interface. InQt, dock windows are instances of QDockWidget. Figure 6.13shows a Qt application with toolbars and a dock window.

    Figure 6.13. A QMainWindowwith a dock window

    Every dock window has its own title bar, even when it is docked. Users can move dock windows from one dock area to another by

    dragging the title bar. They can also detach a dock window from an area and let the dock window float as an independent windowby dragging the dock window outside of any dock area. Free-floating dock windows are always "on top" of their main window.Users can close a QDockWidgetby clicking the close button in the window's title bar. Any combination of these features can bedisabled by calling QDockWidget::setFeatures().

    In earlier versions of Qt, toolbars were treated like dock windows and shared the same dock areas. Starting with Qt 4, toolbarsoccupy their own areas around the central widget (as shown in Figure 6.14) and can't be undocked. If a floating toolbar isrequired, we can simply put it inside a QDockWidget.

    Figure 6.14. QMainWindow's dock and toolbar areas

    157

  • 7/26/2019 Cosas de Qt11

    4/6

    The corners indicated with dotted lines can belong to either of their two adjoining dock areas. For example, we could make thetop-left corner belong to the left dock area by calling QMainWindow::setCorner(Qt::TopLeftCorner,Qt::LeftDockWidgetArea).

    The following code snippet shows how to wrap an existing widget (in this case, a QTreeWidget) in a QDockWidgetandinsert it into the right dock area:

    QDockWidget *shapesDockWidget = new QDockWidget(tr("Shapes")); shapesDockWidget->setObjectName("shapesDockWidget"); shapesDockWidget->setWidget(treeWidget); shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); addDockWidget(Qt::RightDockWidgetArea, shapesDockWidget);

    The setAllowedAreas()call specifies constraints on which dock areas can accept the dock window. Here, we only allow theuser to drag the dock window into the left and right dock areas, where there is enough vertical space for it to be displayedsensibly. If no allowed areas are explicitly set, the user can drag the dock window to any of the four areas.

    Every QObjectcan be given an "object name". This name can be useful when debugging and is used by some test tools.Normally we do not bother to give widgets object names, but when we create dock windows and toolbars, doing so is necessary ifwe want to use QMainWindow::saveState()and QMainWindow::restoreState()to save and restore the dockwindow and toolbar geometries and states.

    Here's how to create a toolbar containing a QComboBox, a QSpinBox, and a few QToolButtons from a QMainWindowsubclass's constructor:

    QToolBar *fontToolBar = new QToolBar(tr("Font")); fontToolBar->setObjectName("fontToolBar"); fontToolBar->addWidget(familyComboBox);

    fontToolBar->addWidget(sizeSpinBox); fontToolBar->addAction(boldAction); fontToolBar->addAction(italicAction); fontToolBar->addAction(underlineAction); fontToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea); addToolBar(fontToolBar);

    If we want to save the position of all the dock windows and toolbars so that we can restore them the next time the application isrun, we can write code that is similar to the code we used to save a QSplitter's state, using QMainWindow's saveState()and restoreState()functions:

    void MainWindow::writeSettings()

    158

  • 7/26/2019 Cosas de Qt11

    5/6

    { QSettings settings("Software Inc.", "Icon Editor");

    settings.beginGroup("mainWindow"); settings.setValue("geometry", saveGeometry()); settings.setValue("state", saveState()); settings.endGroup();}

    void MainWindow::readSettings(){ QSettings settings("Software Inc.", "Icon Editor");

    settings.beginGroup("mainWindow");

    restoreGeometry(settings.value("geometry").toByteArray()); restoreState(settings.value("state").toByteArray()); settings.endGroup();}

    Finally, QMainWindowprovides a context menu that lists all the dock windows and toolbars. This menu is shown in Figure 6.15.The user can close and restore dock windows and hide and restore toolbars using this menu.

    Figure 6.15. A QMainWindowcontext menu

    159

  • 7/26/2019 Cosas de Qt11

    6/6

    Multiple Document Interface

    Applications that provide multiple documents within the main window's central area are called multiple document interfaceapplications, or MDI applications. In Qt, an MDI application is created by using the QMdiAreaclass as the central widget and bymaking each document window a QMdiAreasubwindow.

    It is conventional for MDI applications to provide a Window menu that includes some commands for managing both the windowsand the list of windows. The active window is identified with a checkmark. The user can make any window active by clicking itsentry in the Window menu.

    In this section, we will develop the MDI Editor application shown in Figure 6.16to demonstrate how to create an MDI applicationand how to implement its Window menu. All the application's menus are shown in Figure 6.17.

    Figure 6.16. The MDI Editor application

    Figure 6.17. The MDI Editor application's menus

    The application consists of two classes: MainWindowand Editor. The code is supplied with the book's examples, and sincemost of it is the same or similar to the Spreadsheet application from Part I, we will present only the MDI-relevant code.

    Let's start with the MainWindowclass.

    MainWindow::MainWindow(){

    160