PDA

View Full Version : Delegate for QTableWidget



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

srazi
6th December 2010, 18:31
I tested your code and it works!
EDIT: I set delegate like this,


//table is a QTableWidget* instance
table->setItemDelegate(new NTableDelegate(table));

Wasabi
7th December 2010, 13:21
As soon as I saw that EDIT, I slapped myself. I created the delegate on the stack with a modeless dialog.

A question though: I've been told in another thread to use a delegate to control the positioning of the QTableWidgetItem's checkbox. As you can see in the "default" image above, the second column is entirely made up of checkable items. These items are in fact disabled for text-editing, but I can't figure out how to center the checkbox and remove the text-box next to it. It's a safe bet that I need to use the delegate's paint() function, but I haven't a clue how to do it.