Новый файл |
0,0 → 1,182 |
|
|
/**************************************************************************** |
** |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
** All rights reserved. |
** Contact: Nokia Corporation (qt-info@nokia.com) |
** |
** This file is part of the examples of the Qt Toolkit. |
** |
** $QT_BEGIN_LICENSE:LGPL$ |
** Commercial Usage |
** Licensees holding valid Qt Commercial licenses may use this file in |
** accordance with the Qt Commercial License Agreement provided with the |
** Software or, alternatively, in accordance with the terms contained in |
** a written agreement between you and Nokia. |
** |
** 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.1, 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 have questions regarding the use of this file, please contact |
** Nokia at qt-info@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); |
QComboBox *editor = new QComboBox(parent); |
editor->addItem("0"); |
editor->addItem("1"); |
editor->addItem("2"); |
editor->addItem("3"); |
editor->addItem("4"); |
editor->addItem("5"); |
editor->addItem("6"); |
editor->addItem("7"); |
editor->addItem("8"); |
editor->addItem("9"); |
// editor->addItem("0"); |
|
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); |
QString value = index.model()->data(index, Qt::EditRole).toString(); |
QComboBox *comboBox = static_cast<QComboBox*>(editor); |
if (value == "0") comboBox->setCurrentIndex(0); |
if (value == "1") comboBox->setCurrentIndex(1); |
if (value == "2") comboBox->setCurrentIndex(2); |
if (value == "3") comboBox->setCurrentIndex(3); |
if (value == "4") comboBox->setCurrentIndex(4); |
if (value == "5") comboBox->setCurrentIndex(5); |
if (value == "6") comboBox->setCurrentIndex(6); |
if (value == "7") comboBox->setCurrentIndex(7); |
if (value == "8") comboBox->setCurrentIndex(8); |
if (value == "9") comboBox->setCurrentIndex(9); |
comboBox->setEditable(true); |
|
|
//comboBox->setItemText(0, 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); |
|
QComboBox *comboBox = static_cast<QComboBox*>(editor); |
int currIndex; |
currIndex = comboBox->currentIndex(); |
QString value = comboBox->itemText(currIndex); |
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] |
MyDEDelegate::MyDEDelegate( |
bool calpopup, |
QObject *parent) |
: QItemDelegate(parent), |
m_calpopup(calpopup) { |
} |
|
QWidget *MyDEDelegate::createEditor( |
QWidget *parent, |
const QStyleOptionViewItem& /* option */, |
const QModelIndex& /* index */) const { |
QDateEdit *editor = new QDateEdit(parent); |
editor->setCalendarPopup(m_calpopup); |
editor->installEventFilter(const_cast<MyDEDelegate*>(this)); |
return editor; |
} |
|
void MyDEDelegate::setEditorData( |
QWidget *editor, |
const QModelIndex &index) const { |
QDate value = index.model()->data( |
index, Qt::EditRole).toDate(); |
QDateEdit *de = static_cast<QDateEdit*>(editor); |
de->setDate(value); |
} |
|
void MyDEDelegate::setModelData( |
QWidget *editor, |
QAbstractItemModel *model, |
const QModelIndex& index) const { |
QDateEdit *de = static_cast<QDateEdit*>(editor); |
de->interpretText(); |
QDate value = de->date(); |
model->setData(index, value); |
} |
|
void MyDEDelegate::updateEditorGeometry( |
QWidget *editor, |
const QStyleOptionViewItem &option, |
const QModelIndex& /* index */) const { |
editor->setGeometry(option.rect); |
} |