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

QTableView how to selectRow, highlight and fire select event?



  • When I add a row to a table view at the end I call selectRow with the rowCount() -1 from the model, this results in a grey highlight, but doesn't fire the selection event. If I then doubleclick the row it changes to a blue highlight and fires the selection event.

    How do I achieve the same programmatically?



  • I've done what I want to do, it might now be the best solution, but it works, when the data has been added to the database and the new record added to the table view I then do the following:

    int intRowCount(mpsiSDmodel->rowCount()),
        intNewIndex(intRowCount - 1);
    mptvSDrecs->selectRow(intNewIndex);
    QModelIndex index(mpsiSDmodel->index(intNewIndex, 0));
    emit mptvSDrecs->clicked(index);
    

  • Lifetime Qt Champion

    Hi,

    How are you handling the selection signaling in your application ?



  •     auto item = m_tableWidget->item( row_idx, 0); /* if the first column is selectable */
        QMetaObject::invokeMethod( m_tableWidget, "itemClicked", Qt::AutoConnection, Q_ARG( QTableWidgetItem *, item ) );
    

    This will help you.



  • @JoeCFD , I'm not using QTableWidget, I'm using QTableView which has no item function.



  • @SGaist , what do you mean? I'm connecting to clicked and doubleClicked.



  • @JoeCFD , @SGaist , move detail, this is what I'm doing and try to achieve.

    QTableView used to display records from database.
    I add data to the database then click a button to add the data to the database, a record is created and I call a function to update the table view with the data from the database.
    I then want to select the new row.

    When the row is selected by clicking it, signals occur like the clicked signal which triggers other controls to be enabled. This is not happening when I programmatically select the last row, the row is selected with a gray background where as when I click it with the mouse it has a blue background.

    Is there any way to replicate the functionality that occurs when I click with the mouse but by selection in code?



  • I've done what I want to do, it might now be the best solution, but it works, when the data has been added to the database and the new record added to the table view I then do the following:

    int intRowCount(mpsiSDmodel->rowCount()),
        intNewIndex(intRowCount - 1);
    mptvSDrecs->selectRow(intNewIndex);
    QModelIndex index(mpsiSDmodel->index(intNewIndex, 0));
    emit mptvSDrecs->clicked(index);
    


  • void QAbstractItemView::setCurrentIndex(const QModelIndex &index)
    does this help?


Log in to reply