2 Attachment(s)
Delegate for QTableWidget
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.
Code:
class NTableDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
};
NTableDelegate
::NTableDelegate(QObject *parent
) : QStyledItemDelegate
(parent
){
}
{
val->setBottom(0);
editor->setValidator(val);
return editor;
}
{
double value = index.model()->data(index,Qt::EditRole).toDouble();
QLineEdit* line
= static_cast<QLineEdit
*>
(editor
);
line
->setText
(QString().
setNum(value
));
}
{
QLineEdit* line
= static_cast<QLineEdit
*>
(editor
);
model->setData(index,value);
}
{
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?
Attachment 5574Attachment 5575
Re: Delegate for QTableWidget
I tested your code and it works!
EDIT: I set delegate like this,
Code:
//table is a QTableWidget* instance
table->setItemDelegate(new NTableDelegate(table));
Re: Delegate for QTableWidget
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.