Set a QString data in a QSqlRelationalTableModel



  • Hi everybody !

    I got a little problem with my model (and I think the problem would be the same with another kind of model).

    I have a QString to write in my model.
    For exemple, I have already some data in my model, got from a database. Here, a transportation (destination, product, vehicule). I added a column in this model and I want to write something (list of drivers related to this transport) in theses items. I have my QString with the good names etc, but I can't wite it in the model, the items stay withe, nothing written in it.

    I tried to use setData() but it doesn't seem to work :/

    If somebody has an idea, it would be cool ^^
    Thanks anyway for the attentions.



  • Just asking the obvious, but are the flags for the column set to be editable in your model. The flags in the model determine the way the view handles different columns. If you want to make it fancy you could also add a delegate (QComboBox e.g.).
    Then when you are finished editing the string the setData will be called. You have to store the data in your model yourself.



  • @Qt::ItemFlags TableSql::flags(QModelIndex index)
    {
    Qt::ItemFlags flags = QAbstractTableModel::flags(index);
    if (index.column() == 4)
    {
    flags = Qt::ItemIsEditable;
    return flags;
    }
    return QAbstractTableModel::flags(index);
    }@

    I tried this on my model class, but nothing changed.

    My call of setData() :
    @modelGererTransports->setData(vueGererTransports->model()->index(ligne, 4), chauffeurs, Qt::EditRole);@

    Where modelGererTransports = TableSql (inhérits from QSqlRelationalTableModel), vueGererTransports = QTableView and chauffeurs = QString



  • Hi,
    Maybe to solve this problem it's best to use a 'default' QTableView to display the data. That way you're able to pinpoint the problem. Maybe your view implementation is not correct, so the call to setData is wrong. When you use the default View it will all go in the normal way as it should. We can go from there.
    Also, debug the flags and setData functions. Check out what happens.



  • But, I'm using the "default" QTableView (with or without delegate) ^^"

    I'll test with the Debug one more time.


Log in to reply
 

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