Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Selection row doesn't work in qtableview



  • Hi.

    I am using qtableview and my own model and own delegator to show some data. I am using the following code to highlight the cell when is selected by the user :
    view->setSelectionBehavior(QAbstractItemView::SelectRows);
    view->setSelectionMode(QAbstractItemView::SingleSelection);

    However, it doesn't work. Could you tell me please what I am doing wrong?

    Thanks.



  • @medihech said in Selection row doesn't work in qtableview:

    I am using the following code to highlight the cell when is selected by the user

    It's not clear what you mean by this

    my own model

    how does the flags() method look like in the model?



  • By my own model, I mean I created a custom model that inherits from QAbstractTableModel.

    The flags looks like:

    Qt::ItemFlags TestModel::flags(const QModelIndex &index) const
    {
    return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
    }



  • Sorry, my bad.
    I was referring to "highlight the cell when is selected by the user" being vague



  • To get something similar to this when the user clicks the cell :
    d225ab66-2e30-4adc-89a1-19242af4365d-image.png

    The code I sent you before is to highlight the cell:

    0cdf8910-3b99-40be-afca-83ccedb725f6-image.png

    But it doesn't work either.

    I don't if it could be related, but I also reimplemented the paint method of my delegator, not sure if it is the reason I cannot highlight the cell.

    Thanks in advance!


  • Lifetime Qt Champion

    Hi,

    You should show the implementation of your delegate, you are likely not handling the selected state there.



  • Hi.

    I am afraid I cannot show the whole implementation unless it is something more specific. Could you tell me please what do you mean with "handling the select state" ? Could you show me an simple example how to handle it ?

    Thank you very much for your help. It is very much appreciated.


  • Lifetime Qt Champion

    The Star Delegate example shows it.


Log in to reply