Nominate our 2022 Qt Champions!

QTableView set mapper current index on row click

  • I want, when a row of the table is clicked, to set mapper->currentIndex at the selected record.
    How can do this?
    The mapper is a data mapper and the model of the table QSqlRelationalTableModel.

  • Lifetime Qt Champion


    QDataWidgetMapper::setCurrentModelIndex looks like a better option.

    See the sample code from the function's documentation. It looks like what you might be trying to accomplish.

    Hope it helps

  • Works perfectly SGaist!!
    But now I have another issue, I want to clear the mapped fields setting currentIndex to -1, I tried


    Also with:


    But the mapped fields (two QTextBoxes) don't get cleared!

  • Lifetime Qt Champion

    AFAIK, you can't do it like that. The QDataWidgetMapper won't change anything if you path an invalid index value.

    If you want it empty, you're likely going to have to reset the mappings.

    However that doesn't sound clean, why do you need it to be in such an "invalid" state ?

  • I have a combo, a table, and a textbox.
    TextBox is mapped to a field by the mapper.

    I select an option from the combo, the table gets refreshed with new records, depending on the option selected in the combo.
    I click on a row in the table, and mapper 's current index goes to the selected record(with the code you suggested).
    So the textbox gets the appropriate data.

    I select another option from the combo, the table gets refreshed.
    I have not, yet, clicked on the table, no row is selected.
    But the textbox still has the same content.
    I want it to be empty.

  • Maybe, remove mapping from the textbox and reattach it when needed?
    What do you think?

    EDIT: I dit it this way, it works.
    But I don't know if it's the best way.

  • can't you just call TextBox::clear() ?

  • @VRonin you 're right, this is enough.

Log in to reply