Maximum editable length qtablewidget item
-
Hi,
I have a tablewidget and I want to set the maximum length for editing of the qtablewidgetitems. However, this maximum length varies per tablewidgetitem. I read a lot of documentation and examples and it seems I have to create my own delegate.
I found this example http://www.qtcentre.org/archive/index.php/t-36272.html, so I created the stringdelegate class. I try to use the delegate with setItemDelegate, so right now I can change the maximum length during editing for the entire table using (the fields are added in a loop varying over y)
newField = new QTableWidgetItem(tr("%1").arg(pow((float)y, (float)0)));
newField->setText("text");
ui.tableWidget->setItem(y,0, newField);
newField->tableWidget()->setItemDelegate(new stringDelegate(fieldlength[y],newField->tableWidget()));So I end up with a table with a maximum editable length based on the value in fieldlength.end().
Any ideas how to get the maximum length varying for different items?
-
QModelIndex &index gives you information for which cell createEditor is called.
So you can adjust max length to the number you want.
Or if it is column dependent you can set different delegates on different column. -
I do understand what you mean, however I still don't really understand how to apply it.
So do I change the implementation of createEditor?
QWidget* stringDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/option/,const QModelIndex &index)
const{
QLineEdit *line = new QLineEdit(parent);
line->setMaxLength(length);
line->setFrame(false);
connect(line, SIGNAL(editingFinished()),this, SLOT(commitAndCloseEditor()));
return line;
}or do I change it in the main?
-
change length in createEditor depending on row , column