{
}
{
int idx = index.column();
if(idx == 1)
{
// Display first QComboBox item ???
}
else if(idx == 2)
{
int secs = index.model()->data(index, Qt::DisplayRole).toInt();
.
arg(secs
/ 60,
2,
10,
QChar('0')) .
arg(secs
% 60,
2,
10,
QChar('0'));
myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
drawDisplay(painter, myOption, myOption.rect, text);
drawFocus(painter, myOption, myOption.rect);
}
else
{
}
}
const
{
int idx = index.column();
if(idx == 1)
{
combo->addItem("Item 1");
combo->addItem("Item 2");
combo->addItem("Item 3");
combo->setCurrentIndex(0);
connect(combo, SIGNAL( activated( int ) ), this, SLOT(commitAndCloseEditor()));
return combo;
}
else if(idx == 2)
{
timeEdit->setDisplayFormat("mm:ss");
connect(timeEdit, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
return timeEdit;
}
else
{
}
}
{
int idx = index.column();
if(idx == 1)
{
QComboBox *combo
= qobject_cast<QComboBox
*>
(editor
);
combo->setCurrentIndex(index.model()->data(index).toInt());
}
else if(idx == 2)
{
int secs = index.model()->data(index, Qt::DisplayRole).toInt();
QTimeEdit *timeEdit
= qobject_cast<QTimeEdit
*>
(editor
);
timeEdit
->setTime
(QTime(0, secs
/ 60, secs
% 60));
}
else
{
}
}
{
int idx = index.column();
if(idx == 1)
{
QComboBox *combo
= qobject_cast<QComboBox
*>
(editor
);
model->setData(index, combo->currentIndex());
}
else if(idx == 2)
{
QTimeEdit *timeEdit
= qobject_cast<QTimeEdit
*>
(editor
);
QTime time = timeEdit
->time
();
int secs = (time.minute() * 60) + time.second();
model->setData(index, secs);
}
else
{
}
}
void TableCellDelegate::commitAndCloseEditor()
{
emit commitData(qobject_cast<QWidget *>(sender()));
}
TableCellDelegate::TableCellDelegate(QObject *parent) : QItemDelegate(parent)
{
}
void TableCellDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
int idx = index.column();
if(idx == 1)
{
// Display first QComboBox item ???
}
else if(idx == 2)
{
int secs = index.model()->data(index, Qt::DisplayRole).toInt();
QString text = QString("%1:%2")
.arg(secs / 60, 2, 10, QChar('0'))
.arg(secs % 60, 2, 10, QChar('0'));
QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
drawDisplay(painter, myOption, myOption.rect, text);
drawFocus(painter, myOption, myOption.rect);
}
else
{
QItemDelegate::paint(painter, option, index);
}
}
QWidget *TableCellDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index)
const
{
int idx = index.column();
if(idx == 1)
{
QComboBox *combo = new QComboBox( parent );
combo->addItem("Item 1");
combo->addItem("Item 2");
combo->addItem("Item 3");
combo->setCurrentIndex(0);
connect(combo, SIGNAL( activated( int ) ), this, SLOT(commitAndCloseEditor()));
return combo;
}
else if(idx == 2)
{
QTimeEdit *timeEdit = new QTimeEdit(parent);
timeEdit->setDisplayFormat("mm:ss");
connect(timeEdit, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
return timeEdit;
}
else
{
return QItemDelegate::createEditor(parent, option, index);
}
}
void TableCellDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
int idx = index.column();
if(idx == 1)
{
QComboBox *combo = qobject_cast<QComboBox *>(editor);
combo->setCurrentIndex(index.model()->data(index).toInt());
}
else if(idx == 2)
{
int secs = index.model()->data(index, Qt::DisplayRole).toInt();
QTimeEdit *timeEdit = qobject_cast<QTimeEdit *>(editor);
timeEdit->setTime(QTime(0, secs / 60, secs % 60));
}
else
{
QItemDelegate::setEditorData(editor, index);
}
}
void TableCellDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
int idx = index.column();
if(idx == 1)
{
QComboBox *combo = qobject_cast<QComboBox *>(editor);
model->setData(index, combo->currentIndex());
}
else if(idx == 2)
{
QTimeEdit *timeEdit = qobject_cast<QTimeEdit *>(editor);
QTime time = timeEdit->time();
int secs = (time.minute() * 60) + time.second();
model->setData(index, secs);
}
else
{
QItemDelegate::setModelData(editor, model, index);
}
}
void TableCellDelegate::commitAndCloseEditor()
{
emit commitData(qobject_cast<QWidget *>(sender()));
}
To copy to clipboard, switch view to plain text mode
Bookmarks