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  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

bookdelegate.cpp Example File
demos/books/bookdelegate.cpp

 /****************************************************************************
 **
 ** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
 **
 ** This file is part of the demonstration applications of the Qt Toolkit.
 **
 ** This file may be used under the terms of the GNU General Public
 ** License version 2.0 as published by the Free Software Foundation
 ** and appearing in the file LICENSE.GPL included in the packaging of
 ** this file.  Please review the following information to ensure GNU
 ** General Public Licensing requirements will be met:
 ** http://www.trolltech.com/products/qt/opensource.html
 **
 ** If you are unsure which license is appropriate for your use, please
 ** review the following information:
 ** http://www.trolltech.com/products/qt/licensing.html or contact the
 ** sales department at sales@trolltech.com.
 **
 ** 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 "bookdelegate.h"

 #include <QtGui>

 BookDelegate::BookDelegate(QObject *parent)
     : QSqlRelationalDelegate(parent), star(QPixmap(":images/star.png"))
 {
 }

 void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                            const QModelIndex &index) const
 {
     if (index.column() != 5) {
         QStyleOptionViewItem opt = option;
         opt.rect.adjust(0, 0, -1, -1); // since we draw the grid ourselves
         QSqlRelationalDelegate::paint(painter, opt, index);
     } else {
         const QAbstractItemModel *model = index.model();
         QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
                                   ? QPalette::Normal : QPalette::Disabled;
         if (option.state & QStyle::State_Selected)
             painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));

         int rating = model->data(index, Qt::DisplayRole).toInt();
         int width = star.width();
         int height = star.height();
         int x = option.rect.x();
         int y = option.rect.y() + (option.rect.height() / 2) - (height / 2);
         for (int i = 0; i < rating; ++i) {
             painter->drawPixmap(x, y, star);
             x += width;
         }
         drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
     }

     QPen pen = painter->pen();
     painter->setPen(option.palette.color(QPalette::Mid));
     painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
     painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
     painter->setPen(pen);
 }

 QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option,
                                  const QModelIndex &index) const
 {
     if (index.column() == 5)
         return QSize(5 * star.width(), star.height()) + QSize(1, 1);

     return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1); // since we draw the grid ourselves
 }

 bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
                                const QStyleOptionViewItem &option,
                                const QModelIndex &index)
 {
     if (index.column() != 5)
         return QSqlRelationalDelegate::editorEvent(event, model, option, index);

     QMouseEvent *mouseEvent;
     int stars;

     switch (event->type()) {
         case QEvent::MouseButtonPress:
             mouseEvent = static_cast<QMouseEvent*>(event);
             stars = qBound(0, int(0.7 + qreal(mouseEvent->pos().x()
                               - option.rect.x()) / star.width()), 5);
             model->setData(index, QVariant(stars));
             break;
         default:
             break;
     }

     return true;
 }

 QWidget *BookDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                     const QModelIndex &index) const
 {
     if (index.column() != 4)
         return QSqlRelationalDelegate::createEditor(parent, option, index);

     // for editing the year, return a spinbox with a range from -1000 to 2100.
     QSpinBox *sb = new QSpinBox(parent);
     sb->setFrame(false);
     sb->setMaximum(2100);
     sb->setMinimum(-1000);

     return sb;
 }

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.2
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