How to limit values in the tableView which is set to a model QStandardItemModel where each cell is set to item delegate?
-
I have a tableView which is set to model QStandardItemModel
Now tableview is set to an itemdelegate( comboboxdelegate) which similar to this
How to limit the values (only to positive values)for each cell in the tableview?dataTableModel = new QStandardItemModel(8,4); ui->tableView->setModel(dataTableModel); for(int i=0;i<32;i++) { ui->tableView->setColumnWidth(i,64); ui->tableView->setRowHeight(i,33); } ui->tableView->verticalHeader()->hide(); ui->tableView->horizontalHeader()->hide(); monitorFilterDelegate=new ComboBoxDelegate(); // setting combobox delegate for the tableview ui->tableView->setItemDelegate(monitorFilterDelegate) ;
-
Does your ComboBoxDelegate create editable comboboxes?
If not all user can choose from are items you filled in the combobox.
In any case you can set validator to combobox when you create it in createEditor:
void QComboBox::setValidator ( const QValidator * validator )
-
@alex_malyu
Thanks for the reply
Now if i remove the the item delegate from the tableview.
I set the data to the model . Here each cell is user editable . user can give positive, negative and 0. I want the user to limit the values only to positive and zero.
Can we limit the user editable values?How to limit the values to positive and zero ??dataTableModel = new QStandardItemModel(8,4); ui->tableView->setModel(dataTableModel); for(int i=0;i<32;i++) { ui->tableView->setColumnWidth(i,64); ui->tableView->setRowHeight(i,33); } ui->tableView->verticalHeader()->hide(); ui->tableView->horizontalHeader()->hide(); int row = 8; int col = 4; for(int i = 0; i < row ; i++) { for(int j = 0; j< col ; j++) { QModelIndex index= dataTableModel->index(j,i); dataTableModel->setData(index,0,Qt::EditRole); } }
-
Hi,
Wouldn't a QSpinBox be more suited for that ?
-
@Ratzz said:
Now if i remove the the item delegate from the tableview.
Why? item delegate is the straightforward way to control editing widget.
If you do not need it for other purpose use it to set validator, which will prevent user from entering unwanted values.
This can be done only on the widget level. You may add control what data is set to model, but this will not prevent user from entering unwanted values, even though may prevent putting wrong values into tha model data. -
@alex_malyu said:
If you do not need it for other purpose use it to set validator
I did not get you. Can you just elaborate?
-
If the spin boxes all have the same range, just set them when construction the boxes
-
@Ratzz
Look at the variantDelegate::createEditor example below.
It creates QLineEdit *lineEdit editor and sets validator on it depending on the type of data.
You may look for validator you need in Qt documentation or if you ok with spin box you can use QSpinBox as an editor and set range to it through interface it provides as SGaist suggested. .QWidget *VariantDelegate::createEditor(QWidget parent,
const QStyleOptionViewItem & / option */,
const QModelIndex &index) const
{
if (index.column() != 2)
return 0;QVariant originalValue = index.model()->data(index, Qt::UserRole); if (!isSupportedType(originalValue.type())) return 0; QLineEdit *lineEdit = new QLineEdit(parent); lineEdit->setFrame(false); QRegExp regExp; switch (originalValue.type()) { case QVariant::Bool: regExp = boolExp; break; case QVariant::ByteArray: regExp = byteArrayExp; break; case QVariant::Char: regExp = charExp; break; case QVariant::Color: regExp = colorExp; break; case QVariant::Date: regExp = dateExp; break; case QVariant::DateTime: regExp = dateTimeExp; break; case QVariant::Double: regExp = doubleExp; break; case QVariant::Int: case QVariant::LongLong: regExp = signedIntegerExp; break; case QVariant::Point: regExp = pointExp; break; case QVariant::Rect: regExp = rectExp; break; case QVariant::Size: regExp = sizeExp; break; case QVariant::Time: regExp = timeExp; break; case QVariant::UInt: case QVariant::ULongLong: regExp = unsignedIntegerExp; break; default: ; } if (!regExp.isEmpty()) { QValidator *validator = new QRegExpValidator(regExp, lineEdit); lineEdit->setValidator(validator); } return lineEdit;
}
-
@alex_malyu Seems you have some different validator..I want a validator like some range check for integer..How do i do that
-
@Abhi_oct30
Hi
Do you mean like
http://doc.qt.io/qt-5/qintvalidator.html -
You just need a little cheat on the standard delegate
class PositiveDelegate : public QStyledItemDelegate{ Q_OBJECT Q_DISABLE_COPY(PositiveDelegate) public: explicit PositiveDelegate(QObject* parent=Q_NULLPTR) :QStyledItemDelegate(parent) {} virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex &index) const{ QWidget* result = QStyledItemDelegate::createEditor(parent,option,index); QSpinBox* intSpinRes = qobject_cast<QSpinBox*>(result); if(intSpinRes ){ intSpinRes ->setMinimum(0); return intSpinRes; } QDoubleSpinBox* doubSpinRes = qobject_cast<QDoubleSpinBox*>(result); if(doubSpinRes ){ doubSpinRes ->setMinimum(0.0); return doubSpinRes ; } return result; } };