Qt World Summit: Submit your Presentation

Forbid edition for a specified column of a QTreeView... ?

  • is it possible with Qt5 ?

    for exemple, i have q QTreeView with 5 columns (from 0 to 4), the last column is hidden... (this.. OK)
    then, i would like the items of columns: [0,1,2] forbid edition but the items of column 3 autorize the edition.

    How to do it (easy) ?

  • Lifetime Qt Champion


    Since you are using a QTreeView you are probably using a custom model then you can reimplement the flags(const QModelIndex &) const method to return the original flags minus Qt::ItemisEditable.

    Hope it helps

  • for one of them i have a model, not for the other one...

    so.. ok... it is really heavy that there is no member function able to do a job easier than this way. Each time i want a little personalization (who is not irrealist personalization), i have to do increase code a lot... and find solution Qt can not provide by these member functions... (sniff)

    ok... it is like that... thanks to help SGaist.

  • ok... i have an other idea... because i need to do crafts work and maybe quick more to use code exist allready in my application, i think about use the mouseEvent double click and select condition on columns index i choose for forbid the entry...

  • Lifetime Qt Champion

    The reimplementation of flags is less then 10 lines of code

    Qt::ItemFlags MySuperModel::flags(const QModelIndex & index) const
    if (!index.isValid())
    return Qt::ItemIsEnabled;

    if (index.column() == 3) {
       return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    return QAbstractItemModel::flags(index);


    Otherwise you can also create a QIdentityProxyModel that does it and insert it between both models and respective views.

  • thanks SGaist i will use this for this one has allready a custom model, this technic seems to be more quick and esay that i thank.
    and I will use event on mouse Release double click for the other one has only view custom class.

  • I would really advise you to go with one solution instead. In your situation, it seems a proxy model approach would be the best. What's more, you don't even have to write it yourself. "This snippet":/wiki/QSortFilterProxyModel_subclass_for_readonly_columns_columns_with_checkboxes_and_password_columns provides proxy model that can do what you want (and more).

  • The post is old, but i think this question will be ask many times.

    By the time, i think that your answer, SGaist and andre are together right.
    Difficult to do a choice, and that depend of the futur implementation of code.
    The best is to be able to know to do these two solutions.
    For my thinking, i think that delegate is good when it is for a simple view without a specific model.
    thank you.

Log in to reply