QTableview editing problem using proxy model



  • Sorry, I am new to Qt and trying to implement a Qtableview/Database program. I used this code for proxy model but at the end it says that the QComboboxes are replaced by TextEdits when editing. There is a link to a fix but it is broken. Anyone know a fix for this? or know how to fix it by any chance??
    http://wiki.qt.io/QSortFilterProxyModel_subclass_for_text_alignment_-and_readonly_columns
    Thanks in advance.


  • Moderators

    @cdontheqt
    for what types in the DB are now text-edit widgets displayed instead of combo-boxes? Boolean values?

    But nevertheless. A model shouldn't be responsible for the display of the data. The only thing what can influence this is, by returning a different QVariant type in the model's data() method.

    Also what type of item delegate are you using in your table view?



  • Apologies, I am a noob!
    All my types are QStrings.
    I'm using ..tableView->setItemDelegate(new QSqlRelationalDelegate(this));
    This works perfectly and adds combobox which automatically fills the values. When I use the proxy model the comboboxes are replaced with lineEdits and anything can be typed in which I don't want.
    I think I will try to use a custom delegate but the problem I see with that is that I would have to implement the values in the combobox manually, and I know that the standard delegate already does this. Does that make sense or am I not understanding something basic? Thanks


  • Moderators

    @cdontheqt
    as i said, you actually just need to make sure that the model's data() method returns the correct QVariant types for the columns.
    Since you are using a proxy model (which normally just forwards the calls mapped to the source model) and a QSqlQueryModel(??) this should already work. I would expect that QSqlQueryModel returns the QVariant types corresponding to the database type (from the used sql plugin). But to be honest i am not sure right now.

    Alternatively you can reimplement a custom delegate or try (in your proxymodel) to intercept the QVariant returned and convert it to a bool variant, or whatever.

    Creating a combo-box for booleans works with the default delegate out of the box. If you want a combo-box for a string list with predefined strings you need to create a custom delegate subclass.
    For example this might be necessary to show a combobox for columns containing foreign keys. Then you need to create a combobox in your custom delegate's createEditor() method for the specific column. This combobox then has it's own sql model only fetching the ids from the table the foreign key refers to.

    Hope this clears things up a bit?



  • Thanks for replies. I tried a custom delegate and this seems to work. I haven't done extensive testing yet. I just reimplemented the creatEditor method as below.

    QWidget *CustomDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const
    {
    if ((index.column() != ID_INDEX) && (index.column() != DATE_ADDED_INDEX))
    {
    QSqlRelationalDelegate::createEditor(parent,option,index);
    }
    }

    Thanks again for help.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Aren't missing a return statement in your createEditor implementation in the case the column is ID_INDEX or DATE_ADDED_INDEX ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.