diff options
author | axis <qt-info@nokia.com> | 2009-04-24 11:34:15 (GMT) |
---|---|---|
committer | axis <qt-info@nokia.com> | 2009-04-24 11:34:15 (GMT) |
commit | 8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76 (patch) | |
tree | a17e1a767a89542ab59907462206d7dcf2e504b2 /examples/itemviews/spinboxdelegate | |
download | Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.zip Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.tar.gz Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.tar.bz2 |
Long live Qt for S60!
Diffstat (limited to 'examples/itemviews/spinboxdelegate')
-rw-r--r-- | examples/itemviews/spinboxdelegate/delegate.cpp | 103 | ||||
-rw-r--r-- | examples/itemviews/spinboxdelegate/delegate.h | 71 | ||||
-rw-r--r-- | examples/itemviews/spinboxdelegate/main.cpp | 87 | ||||
-rw-r--r-- | examples/itemviews/spinboxdelegate/spinboxdelegate.pro | 11 |
4 files changed, 272 insertions, 0 deletions
diff --git a/examples/itemviews/spinboxdelegate/delegate.cpp b/examples/itemviews/spinboxdelegate/delegate.cpp new file mode 100644 index 0000000..03a1a23 --- /dev/null +++ b/examples/itemviews/spinboxdelegate/delegate.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.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 the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* + delegate.cpp + + A delegate that allows the user to change integer values from the model + using a spin box widget. +*/ + +#include <QtGui> + +#include "delegate.h" + + +//! [0] +SpinBoxDelegate::SpinBoxDelegate(QObject *parent) + : QItemDelegate(parent) +{ +} +//! [0] + +//! [1] +QWidget *SpinBoxDelegate::createEditor(QWidget *parent, + const QStyleOptionViewItem &/* option */, + const QModelIndex &/* index */) const +{ + QSpinBox *editor = new QSpinBox(parent); + editor->setMinimum(0); + editor->setMaximum(100); + + return editor; +} +//! [1] + +//! [2] +void SpinBoxDelegate::setEditorData(QWidget *editor, + const QModelIndex &index) const +{ + int value = index.model()->data(index, Qt::EditRole).toInt(); + + QSpinBox *spinBox = static_cast<QSpinBox*>(editor); + spinBox->setValue(value); +} +//! [2] + +//! [3] +void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const +{ + QSpinBox *spinBox = static_cast<QSpinBox*>(editor); + spinBox->interpretText(); + int value = spinBox->value(); + + model->setData(index, value, Qt::EditRole); +} +//! [3] + +//! [4] +void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, + const QStyleOptionViewItem &option, const QModelIndex &/* index */) const +{ + editor->setGeometry(option.rect); +} +//! [4] diff --git a/examples/itemviews/spinboxdelegate/delegate.h b/examples/itemviews/spinboxdelegate/delegate.h new file mode 100644 index 0000000..a81cfd1 --- /dev/null +++ b/examples/itemviews/spinboxdelegate/delegate.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.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 the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DELEGATE_H +#define DELEGATE_H + +#include <QItemDelegate> +#include <QModelIndex> +#include <QObject> +#include <QSize> +#include <QSpinBox> + +//! [0] +class SpinBoxDelegate : public QItemDelegate +{ + Q_OBJECT + +public: + SpinBoxDelegate(QObject *parent = 0); + + QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, + const QModelIndex &index) const; + + void setEditorData(QWidget *editor, const QModelIndex &index) const; + void setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const; + + void updateEditorGeometry(QWidget *editor, + const QStyleOptionViewItem &option, const QModelIndex &index) const; +}; +//! [0] + +#endif diff --git a/examples/itemviews/spinboxdelegate/main.cpp b/examples/itemviews/spinboxdelegate/main.cpp new file mode 100644 index 0000000..4b9d6a4 --- /dev/null +++ b/examples/itemviews/spinboxdelegate/main.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.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 the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* + main.cpp + + A simple example that shows how a view can use a custom delegate to edit + data obtained from a model. +*/ + +#include <QApplication> +#include <QHeaderView> +#include <QItemSelectionModel> +#include <QStandardItemModel> +#include <QTableView> + +#include "delegate.h" + +//! [0] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QStandardItemModel model(4, 2); + QTableView tableView; + tableView.setModel(&model); + + SpinBoxDelegate delegate; + tableView.setItemDelegate(&delegate); +//! [0] + + tableView.horizontalHeader()->setStretchLastSection(true); + +//! [1] + for (int row = 0; row < 4; ++row) { + for (int column = 0; column < 2; ++column) { + QModelIndex index = model.index(row, column, QModelIndex()); + model.setData(index, QVariant((row+1) * (column+1))); + } +//! [1] //! [2] + } +//! [2] + +//! [3] + tableView.setWindowTitle(QObject::tr("Spin Box Delegate")); + tableView.show(); + return app.exec(); +} +//! [3] diff --git a/examples/itemviews/spinboxdelegate/spinboxdelegate.pro b/examples/itemviews/spinboxdelegate/spinboxdelegate.pro new file mode 100644 index 0000000..31b55d4 --- /dev/null +++ b/examples/itemviews/spinboxdelegate/spinboxdelegate.pro @@ -0,0 +1,11 @@ +HEADERS = delegate.h +SOURCES = delegate.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/spinboxdelegate +sources.files = $$SOURCES $$HEADERS *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/spinboxdelegate +INSTALLS += target sources + +include($$QT_SOURCE_TREE/examples/examplebase.pri) |