Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to detect when user selects a row in QTableView



  • I have a form with a table of people (columns for name, height, weight, etc.), and a corresponding widget that populates with the selected person's information when user selects them in a table.

    I also have a New Person button that clears all the entry fields, and also clears the selection in the QTableView using tableView->clearSelection().

    I've connected the QItemSelectionModel::currentRowChanged signal of tableView->selectionModel() to a slot in my class that gets the selected person and populates the fields with that person's information.

    My problem is, when the New Person button is pressed, the form is cleared and the selection in the QTableView is cleared as well, but when I select the previously selected patient, the currentRowChanged signal is not emitted. I suppose this kind of makes sense, because this is the same row that is previously selected, but it seems that calling table->clearSelection() does not allow the table to detect when that same row is selected again.

    For instance, I have 1 person in the table. I click the person, and the fields populate with their information. I click "New Person" and the fields are cleared and the table selection is cleared. But when I select the person in the table again, the currentRowChanged signal is not emitted.

    If I select a different person in the table, the program works perfectly.

    How can I detect when the same row is selected after calling table->clearSelection? Is there a different signal to connect to (the selection model does not seem to have a more generic "rowSelected" signal or something like that), or a different way to clear the selection in the QTableView?


  • Lifetime Qt Champion

    Hi,

    I wonder if you are not looking for QDataWidgetMapper ?



  • @smeeth

    How can I detect when the same row is selected after calling table->clearSelection?

    If you really need to do this, and are finding the Qt code is not doing what you want, then do it in your own code! Save whatever row was last selected before clearing, compare when a row is selected afterward.

    @SGaist's suggestion of a QDataWidgetMapper is a Qt class designed for your whole task. I would heartily recommend you consider changing over to using that to save writing a lot of your own code.


Log in to reply