PDA

View Full Version : Maximum Characters in Qtablewidgetitem



Nexus6
31st July 2009, 11:33
Hi,

I want to fix the maximum characters that a user can input in a qtablewidgetitem.

I was thinking of validators, but I'm not sure how to do it.

Any hints?

Thanks

shentian
31st July 2009, 15:30
QTableWidget uses a QLineEdit to edit the contents of an item. The QLineEdit is provided by the QItemDelegate installed for every column. You can use QLineEdit::setMaxLength to fix the maximum number of characters.

Do the following:

Create a subclass of QItemDelegate.
Override QItemDelegate::createEditor (see code below).
Install your ItemDelegate in the QTableWidget using setItemDelegate , setItemDelegateForColumn, or setItemDelegateForRow as appropriate.




virtual QWidget* MyItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
QLineEdit* lineEdit = new QLineEdit(parent);
lineEdit->setMaxLength(80); // or whatever...
return lineEdit;
}