QCompleter with a QSqlTableModel [Solved]



  • Hi all,
    I need to use a QCompleter in a QLineEdit fetching data from a QSqlTableModel.

    I have a QSqlTableModel that I use to show data in a QTableView, and the model has 5 column.

    I'd like to use the same model (its second column) to load data in the completer. Is it possible?



  • Sure. See QCompleter::setModel + QCompleter::setCompletionColumn.



  • Absolutely, you can set the same model you use for your tableview to the completer using setModel() and select the Column using setCompletionColumn().



  • Thanks a lot, I didn't found it.
    The problem was that in docs there is:
    @
    ...
    The model can be list model or a tree model ...
    @



  • Now when I select a row from the completer popup I need to fetch the corresponding row in the QSqlTableModel.
    I connected this:
    @
    connect(my_completer, SIGNAL(activated(QModelIndex)), SLOT(setTableData(QModelIndex)));
    @

    And I'm trying to get the row this way:
    @
    setTableData(QModelIndex index)
    {
    int model_row;
    model_row = index.row();
    qDebug() << model_row;
    ....
    }
    @
    But I always get the row in the popup, not in the model. For example if the popup has 4 row and I select the second, I get model_row==1 while in the model it's the row 52.

    What do I wrong?



  • I found this solution:
    @
    setTableData(QModelIndex index)
    {
    ....
    QModelIndex index_table_model = qobject_cast<QAbstractProxyModel*> (m_Completer->completionModel())->mapToSource(index);
    int table_model_row;
    table_model_row = index_table_model.row();
    ....
    }
    @

    and it works...


Log in to reply
 

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