Forum
Sign Up
× DataGrid for Qt.C++

How to make a cell readonly?

11 years 11 months ago - 11 years 11 months ago #1 by
How can i make a cell readonly? I try to set the flags for this cell to Qt::NoItemFlags or only to Qt::ItemIsSelectable but when i doubleclick into the cell i get an exception, because i have not define editdata for these cell.
Thanks
Last edit: 11 years 11 months ago by .

Please Log in or Create an account to join the conversation.

  • 's Avatar Topic Author
11 years 11 months ago #2 by Developer Machines
What text of the exeption you are getting?

Please Log in or Create an account to join the conversation.

More
11 years 11 months ago - 11 years 11 months ago #3 by
Replied by on topic How to make a cell readonly?
I get the following exception (see screenshot):

ASSERT: "false" in file src\grid\QtnGridTableView.cpp, line 2554
Attachments:
Last edit: 11 years 11 months ago by .

Please Log in or Create an account to join the conversation.

  • 's Avatar Topic Author
11 years 11 months ago #4 by Developer Machines
This asert means that at the beginning of the cell editing grid was unable to convert the value obtained from your model to the type that your cell editor is expected.
You need to ensure that the value obtained from the model using the EditRole role to be the same type as the QVariant returned by GridEditorRepository::defaultValue(EditRole).

P.S. The defaultValue is used when you add row to the grid (for example) and grid fills known cells by default values.

Please Log in or Create an account to join the conversation.

More
11 years 11 months ago #5 by
Replied by on topic How to make a cell readonly?
Hi,
you are right. For DisplayRole for the cell I return a QString and for EditRole a QVariant. But the cell is readonly. I define in my model:

Qt::ItemFlags SystemGridModel::flags(const QModelIndex& index) const
{
if (!index.isValid())
{
return Qt::ItemFlags();
}

if(index.column() == TotalLength)
{
return Qt::NoItemFlags;
}

return Qt::ItemIsEnabled | Qt::ItemIsEditable;
}

So there is no data for editing. I found in your code in QtnGridTableView in line 2538 the check,

if (!column->editorRepository()->isEditable())
return true;

but you didn't check the flag. So I can't make an individual cell readonly.
Therefore I make a derived class of your class Grid. I overwrite the method cellClicked and insert the following code:

GridBandedTableView* pView = view<Qtitan::GridBandedTableView>();
QAbstractItemModel* pModel = pView->model();
if(!pModel)
{
return;
}

Qtitan::GridBandedTableColumn* pColumn = (Qtitan::GridBandedTableColumn * pView->getColumn(args->cell()->columnIndex());
if(!pColumn)
{
return;
}

pColumn->editorRepository()->setEditable(pModel->flags(pModel->index(args->cell()->rowIndex(), args->cell()->columnIndex())).testFlag(Qt::ItemIsEditable));

Then it works correct.

But I'm woundering why you didn't check the flags.

Please Log in or Create an account to join the conversation.

  • 's Avatar Topic Author
11 years 11 months ago #6 by Developer Machines
Indeed. This is a bug in our code. We will correct errors in a way that you suggested. Fix will be added to the next update.

Please change line:
    if (!column->editorRepository()->isEditable())
        return true;

to:
    QModelIndex index = column->dataBinding()->controller()->model()->index(
        row->modelRowIndex(), column->dataBinding()->column(), column->dataBinding()->controller()->parentIndex());
    if (!column->editorRepository()->isEditable() || !index.flags().testFlag(Qt::ItemIsEditable))
        return true;

After that you need to rebuld the component.
The following user(s) said Thank You:

Please Log in or Create an account to join the conversation.

More
  • Not Allowed: to create new topic.
  • Not Allowed: to reply.
  • Not Allowed: to edit your message.
Moderators: Developer Machines
Time to create page: 0.138 seconds

Developer Newsletter

Join our Developer Machines newsletter to get informed on all the latest releases of the commercial components for Qt.C++, Delphi FireMonkey, updates and general knowledges.

Quick Support

Should you need any additional information about our products or licensing, please contact us at the following email addresses:

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

Get in Touch

If you would like to purchase our products or services, but don’t know how to do it the right way, please feel free to contact us:

  • This email address is being protected from spambots. You need JavaScript enabled to view it.( any questions related to our products or services )
  • This email address is being protected from spambots. You need JavaScript enabled to view it.( questions related to licensing )