Wasabi
6th December 2010, 14:43
I've been trying to figure out how Delegates and whatnot work, but am having quite a bit of trouble. I'm fairly certain there's something wrong with my concepts, but after reading the Delegate Classes page and looking through the SpinBox Delegate example, I thought I'd figured out how things work, but clearly haven't.
I'm trying to use delegates to control user input in a QTableWidget so that only numbers are allowed.
The code is as follows.
class NTableDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
NTableDelegate(QObject* parent);
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;
};
NTableDelegate::NTableDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
QWidget* NTableDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
QLineEdit* editor = new QLineEdit(parent);
QDoubleValidator* val = new QDoubleValidator(editor);
val->setBottom(0);
val->setNotation(QDoubleValidator::StandardNotation);
editor->setValidator(val);
return editor;
}
void NTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
double value = index.model()->data(index,Qt::EditRole).toDouble();
QLineEdit* line = static_cast<QLineEdit*>(editor);
line->setText(QString().setNum(value));
}
void NTableDelegate::setModelData(QWidget* editor,QAbstractItemModel* model,const QModelIndex &index) const
{
QLineEdit* line = static_cast<QLineEdit*>(editor);
QString value = line->text();
model->setData(index,value);
}
void NTableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
I basically got the SpinBox Delegate example and tried altering it for my needs, but to no avail. The cells are not visible and not modifiable. If I click the headers, they're selected, which tells me the table is populated, but simply locked away.
However, should I simply comment out the QTableWidget::setItemDelegate() line, all goes back to normal, as shown in the images below. So, my question remains: what have I done wrong?
55745575
I'm trying to use delegates to control user input in a QTableWidget so that only numbers are allowed.
The code is as follows.
class NTableDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
NTableDelegate(QObject* parent);
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;
};
NTableDelegate::NTableDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
QWidget* NTableDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
QLineEdit* editor = new QLineEdit(parent);
QDoubleValidator* val = new QDoubleValidator(editor);
val->setBottom(0);
val->setNotation(QDoubleValidator::StandardNotation);
editor->setValidator(val);
return editor;
}
void NTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
double value = index.model()->data(index,Qt::EditRole).toDouble();
QLineEdit* line = static_cast<QLineEdit*>(editor);
line->setText(QString().setNum(value));
}
void NTableDelegate::setModelData(QWidget* editor,QAbstractItemModel* model,const QModelIndex &index) const
{
QLineEdit* line = static_cast<QLineEdit*>(editor);
QString value = line->text();
model->setData(index,value);
}
void NTableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
I basically got the SpinBox Delegate example and tried altering it for my needs, but to no avail. The cells are not visible and not modifiable. If I click the headers, they're selected, which tells me the table is populated, but simply locked away.
However, should I simply comment out the QTableWidget::setItemDelegate() line, all goes back to normal, as shown in the images below. So, my question remains: what have I done wrong?
55745575