Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Tous les espaces de nom  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

toolbar.cpp Example File
demos/mainwindow/toolbar.cpp

 /****************************************************************************
 **
 ** Copyright (C) 2004-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. In
** addition, as a special exception, Trolltech gives you certain
** additional rights. These rights are described in the Trolltech GPL
** Exception version 1.2, which can be found at
** http://www.trolltech.com/products/qt/gplexception/ and in the file
** GPL_EXCEPTION.txt in this package.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/

 #include "toolbar.h"

 #include <QMainWindow>
 #include <QMenu>
 #include <QPainter>
 #include <QPainterPath>
 #include <QSpinBox>
 #include <QLabel>
 #include <QToolTip>

 #include <stdlib.h>

 static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &color)
 {
     int w = iconSize.width();
     int h = iconSize.height();

     QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
     image.fill(0);

     QPainter p(&image);

     extern void render_qt_text(QPainter *, int, int, const QColor &);
     render_qt_text(&p, w, h, color);

     return QPixmap::fromImage(image, Qt::DiffuseDither | Qt::DiffuseAlphaDither);
 }

 static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color)
 { return genIcon(iconSize, QString::number(number), color); }

 ToolBar::ToolBar(const QString &title, QWidget *parent)
     : QToolBar(parent), spinbox(0), spinboxAction(0)
 {
     tip = 0;
     setWindowTitle(title);
     setObjectName(title);

     setIconSize(QSize(32, 32));

     QColor bg(palette().background().color());
     menu = new QMenu("One", this);
     menu->setIcon(genIcon(iconSize(), 1, Qt::black));
     menu->addAction(genIcon(iconSize(), "A", Qt::blue), "A");
     menu->addAction(genIcon(iconSize(), "B", Qt::blue), "B");
     menu->addAction(genIcon(iconSize(), "C", Qt::blue), "C");
     addAction(menu->menuAction());

     QAction *two = addAction(genIcon(iconSize(), 2, Qt::white), "Two");
     QFont boldFont;
     boldFont.setBold(true);
     two->setFont(boldFont);

     addAction(genIcon(iconSize(), 3, Qt::red), "Three");
     addAction(genIcon(iconSize(), 4, Qt::green), "Four");
     addAction(genIcon(iconSize(), 5, Qt::blue), "Five");
     addAction(genIcon(iconSize(), 6, Qt::yellow), "Six");
     orderAction = new QAction(this);
     orderAction->setText(tr("Order Items in Tool Bar"));
     connect(orderAction, SIGNAL(triggered()), SLOT(order()));

     randomizeAction = new QAction(this);
     randomizeAction->setText(tr("Randomize Items in Tool Bar"));
     connect(randomizeAction, SIGNAL(triggered()), SLOT(randomize()));

     addSpinBoxAction = new QAction(this);
     addSpinBoxAction->setText(tr("Add Spin Box"));
     connect(addSpinBoxAction, SIGNAL(triggered()), SLOT(addSpinBox()));

     removeSpinBoxAction = new QAction(this);
     removeSpinBoxAction->setText(tr("Remove Spin Box"));
     removeSpinBoxAction->setEnabled(false);
     connect(removeSpinBoxAction, SIGNAL(triggered()), SLOT(removeSpinBox()));

     movableAction = new QAction(tr("Movable"), this);
     movableAction->setCheckable(true);
     connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool)));

     allowedAreasActions = new QActionGroup(this);
     allowedAreasActions->setExclusive(false);

     allowLeftAction = new QAction(tr("Allow on Left"), this);
     allowLeftAction->setCheckable(true);
     connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool)));

     allowRightAction = new QAction(tr("Allow on Right"), this);
     allowRightAction->setCheckable(true);
     connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool)));

     allowTopAction = new QAction(tr("Allow on Top"), this);
     allowTopAction->setCheckable(true);
     connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool)));

     allowBottomAction = new QAction(tr("Allow on Bottom"), this);
     allowBottomAction->setCheckable(true);
     connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool)));

     allowedAreasActions->addAction(allowLeftAction);
     allowedAreasActions->addAction(allowRightAction);
     allowedAreasActions->addAction(allowTopAction);
     allowedAreasActions->addAction(allowBottomAction);

     areaActions = new QActionGroup(this);
     areaActions->setExclusive(true);

     leftAction = new QAction(tr("Place on Left") , this);
     leftAction->setCheckable(true);
     connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool)));

     rightAction = new QAction(tr("Place on Right") , this);
     rightAction->setCheckable(true);
     connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool)));

     topAction = new QAction(tr("Place on Top") , this);
     topAction->setCheckable(true);
     connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool)));

     bottomAction = new QAction(tr("Place on Bottom") , this);
     bottomAction->setCheckable(true);
     connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool)));

     areaActions->addAction(leftAction);
     areaActions->addAction(rightAction);
     areaActions->addAction(topAction);
     areaActions->addAction(bottomAction);

     toolBarBreakAction = new QAction(tr("Insert break"), this);
     connect(toolBarBreakAction, SIGNAL(triggered(bool)), this, SLOT(insertToolBarBreak()));

     connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool)));

     connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool)));

     menu = new QMenu(title, this);
     menu->addAction(toggleViewAction());
     menu->addSeparator();
     menu->addAction(orderAction);
     menu->addAction(randomizeAction);
     menu->addSeparator();
     menu->addAction(addSpinBoxAction);
     menu->addAction(removeSpinBoxAction);
     menu->addSeparator();
     menu->addAction(movableAction);
     menu->addSeparator();
     menu->addActions(allowedAreasActions->actions());
     menu->addSeparator();
     menu->addActions(areaActions->actions());
     menu->addSeparator();
     menu->addAction(toolBarBreakAction);

     connect(menu, SIGNAL(aboutToShow()), this, SLOT(updateMenu()));

     randomize();
 }

 void ToolBar::updateMenu()
 {
     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
     Q_ASSERT(mainWindow != 0);

     const Qt::ToolBarArea area = mainWindow->toolBarArea(this);
     const Qt::ToolBarAreas areas = allowedAreas();

     movableAction->setChecked(isMovable());

     allowLeftAction->setChecked(isAreaAllowed(Qt::LeftToolBarArea));
     allowRightAction->setChecked(isAreaAllowed(Qt::RightToolBarArea));
     allowTopAction->setChecked(isAreaAllowed(Qt::TopToolBarArea));
     allowBottomAction->setChecked(isAreaAllowed(Qt::BottomToolBarArea));

     if (allowedAreasActions->isEnabled()) {
         allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
         allowRightAction->setEnabled(area != Qt::RightToolBarArea);
         allowTopAction->setEnabled(area != Qt::TopToolBarArea);
         allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
     }

     leftAction->setChecked(area == Qt::LeftToolBarArea);
     rightAction->setChecked(area == Qt::RightToolBarArea);
     topAction->setChecked(area == Qt::TopToolBarArea);
     bottomAction->setChecked(area == Qt::BottomToolBarArea);

     if (areaActions->isEnabled()) {
         leftAction->setEnabled(areas & Qt::LeftToolBarArea);
         rightAction->setEnabled(areas & Qt::RightToolBarArea);
         topAction->setEnabled(areas & Qt::TopToolBarArea);
         bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
     }
 }

 void ToolBar::order()
 {
     QList<QAction *> ordered, actions1 = actions(),
                               actions2 = qFindChildren<QAction *>(this);
     while (!actions2.isEmpty()) {
         QAction *action = actions2.takeFirst();
         if (!actions1.contains(action))
             continue;
         actions1.removeAll(action);
         ordered.append(action);
     }

     clear();
     addActions(ordered);

     orderAction->setEnabled(false);
 }

 void ToolBar::randomize()
 {
     QList<QAction *> randomized, actions = this->actions();
     while (!actions.isEmpty()) {
         QAction *action = actions.takeAt(rand() % actions.size());
         randomized.append(action);
     }
     clear();
     addActions(randomized);

     orderAction->setEnabled(true);
 }

 void ToolBar::addSpinBox()
 {
     if (!spinbox) {
         spinbox = new QSpinBox(this);
     }
     if (!spinboxAction)
         spinboxAction = addWidget(spinbox);
     else
         addAction(spinboxAction);

     addSpinBoxAction->setEnabled(false);
     removeSpinBoxAction->setEnabled(true);
 }

 void ToolBar::removeSpinBox()
 {
     if (spinboxAction)
         removeAction(spinboxAction);

     addSpinBoxAction->setEnabled(true);
     removeSpinBoxAction->setEnabled(false);
 }

 void ToolBar::allow(Qt::ToolBarArea area, bool a)
 {
     Qt::ToolBarAreas areas = allowedAreas();
     areas = a ? areas | area : areas & ~area;
     setAllowedAreas(areas);

     if (areaActions->isEnabled()) {
         leftAction->setEnabled(areas & Qt::LeftToolBarArea);
         rightAction->setEnabled(areas & Qt::RightToolBarArea);
         topAction->setEnabled(areas & Qt::TopToolBarArea);
         bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
     }
 }

 void ToolBar::place(Qt::ToolBarArea area, bool p)
 {
     if (!p)
         return;

     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
     Q_ASSERT(mainWindow != 0);

     mainWindow->addToolBar(area, this);

     if (allowedAreasActions->isEnabled()) {
         allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
         allowRightAction->setEnabled(area != Qt::RightToolBarArea);
         allowTopAction->setEnabled(area != Qt::TopToolBarArea);
         allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
     }
 }

 void ToolBar::changeMovable(bool movable)
 { setMovable(movable); }

 void ToolBar::allowLeft(bool a)
 { allow(Qt::LeftToolBarArea, a); }

 void ToolBar::allowRight(bool a)
 { allow(Qt::RightToolBarArea, a); }

 void ToolBar::allowTop(bool a)
 { allow(Qt::TopToolBarArea, a); }

 void ToolBar::allowBottom(bool a)
 { allow(Qt::BottomToolBarArea, a); }

 void ToolBar::placeLeft(bool p)
 { place(Qt::LeftToolBarArea, p); }

 void ToolBar::placeRight(bool p)
 { place(Qt::RightToolBarArea, p); }

 void ToolBar::placeTop(bool p)
 { place(Qt::TopToolBarArea, p); }

 void ToolBar::placeBottom(bool p)
 { place(Qt::BottomToolBarArea, p); }

 void ToolBar::insertToolBarBreak()
 {
     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
     Q_ASSERT(mainWindow != 0);

     mainWindow->insertToolBarBreak(this);
 }

 void ToolBar::enterEvent(QEvent*)
 {
 /*
     These labels on top of toolbars look darn ugly

     if (tip == 0) {
         tip = new QLabel(windowTitle(), this);
         QPalette pal = tip->palette();
         QColor c = Qt::black;
         c.setAlpha(100);
         pal.setColor(QPalette::Window, c);
         pal.setColor(QPalette::Foreground, Qt::white);
         tip->setPalette(pal);
         tip->setAutoFillBackground(true);
         tip->setMargin(3);
         tip->setText(windowTitle());
     }
     QPoint c = rect().center();
     QSize hint = tip->sizeHint();
     tip->setGeometry(c.x() - hint.width()/2, c.y() - hint.height()/2,
                         hint.width(), hint.height());

     tip->show();
 */
 }

 void ToolBar::leaveEvent(QEvent*)
 {
     if (tip != 0)
         tip->hide();
 }

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 94
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 42
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 9
  7. Qt Commercial : Digia organise un webinar gratuit le 27 mars sur la conception d'interfaces utilisateur et d'applications avec le framework 0
Page suivante

Le blog Digia au hasard

Logo

Créer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut Cuvelier

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.3
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web