PDA

View Full Version : QTablewidget and QItemDelegate?



whitefurrows
22nd April 2010, 21:03
Hi,

i try to display widgets within a QTablewidget:

1. How can i show the first item in the QComboBox if editing finished and not the model value?
2. How can i set at row 0 a different QDoubbleValidator dependent the QComboBox entry?

Thanks in advanced


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()));
}

whitefurrows
25th April 2010, 18:26
Can this be done or is it so hard because i dont get a tip?

whitefurrows
27th April 2010, 19:21
1. How can i show the first item in the QComboBox if editing finished and not the model value?

I know how can i do that, but how can i figure out the currentText from the ComboBox?


QStyleOptionComboBox *box = new QStyleOptionComboBox();
box->palette = option.palette;
box->rect = option.rect;
box->state = QStyle::State_Active | QStyle::State_Enabled;
box->currentText = "Text"; //comboBox->currentText();
box->frame = false;
painter->save();
QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, box, painter);
painter->restore();

I have try this, but thats not right:

qDebug() << "DisplayRole: " << index.data(Qt::DisplayRole).toString();
qDebug() << "EditRole: " << index.data(Qt::EditRole).toString();

whitefurrows
28th April 2010, 15:33
Hi,

now i have try to set the Qt :: DisplayRole within setModelData() like this:

model->setData(index, combo->currentText(), Qt::DisplayRole);

and write within paint() like this:

QStyleOptionComboBox *box = new QStyleOptionComboBox();
box->palette = option.palette;
box->rect = option.rect;
box->state = QStyle::State_Active | QStyle::State_Enabled;
box->currentText = index.data(Qt::DisplayRole).toString();
box->frame = false;
painter->save();
QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, box, painter);
painter->restore();

But i cant display the text. Must i do anything else in createEditor() or setEditorData()?

Please help me! Thanks!