Qt World Summit: Register Today!

Problem displaying a column in a ProxyModel

  • I've subclassed QSortFilterProxyModel and overridden the data() method to show two columns (name and surname) in a combobox as a single element.
    the combobox is mapped in a QWidgetMapper to edit a database.
    here's the problem: i select an element, then i click on another part of the window and my selection changes, and another element is selected automatically (it seems to be a random choice).

    here is the data() function:
    QVariant MyProxyModel::data(const QModelIndex & index,int role) const{
    if (index.column()==1 && role==Qt::DisplayRole){
    QModelIndex name = mapToSource(index);
    QModelIndex surname = name.sibling(name.row(),name.column()+1);
    return (sourceModel()->data(name).toString()+" "+
    else return QSortFilterProxyModel::data(index,role);


    here is the mapping:
    MyProxyModel *opm = new MyProxyModel(this);

    another info: the issue exist only if i select an entry that effectively has a surname to concat; if i select someone with an empty surname my selection is conserved (obviously, the empty surname is a strange situation that exist because for testing I haven't inserted full names for everybody)

  • discovered something: the problem is caused by the delegate.

    mapper->setItemDelegate(new QSqlRelationalDelegate(mapper));@

    when the real data is different from the proxy data, it makes crazy things.
    however, i don't know how to handle this.

  • well, exploring a bit, I've discovered the problem is inside qt. Since I have no time (and probably not enough skills) to remake the qsqlrelationalmodel class I'm stopping to use it and handle it by hand. when everything would be stable, I will post in the wiki my code snippets, and I hope it would be useful to somebody.

Log in to reply