QtableView how to find the proper signal



  • I have QtablewView used with QSqlQueryModel.
    I want when a row is selected to make "remove" button enabled.When no selection is there the button to remain disabled.

    I figured out how to get signal when the selection has changed, but i need whether there is a selected row:

    QItemSelectionModel *sm = ui->tableView_partners->selectionModel();
    connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    this, SLOT(enableDeleteButton()));

    void Partners::enableDeleteButton()
    {

    if (ui->tableView_partners->selectionModel()->currentIndex().isValid() == true)
    {
        ui->pushButton_delete->setEnabled(true);
     }
    
    else {
        ui->pushButton_delete->setEnabled(false);
    }
    

    }


  • Lifetime Qt Champion

    Hi,

    What about using the hasSelection method ?



  • @SGaist
    The problem is that hasSelection() is a method, i need a signal.Now i can use only currentRowChanged(), but it doesn't emit when nothing is selected.I need every time when the table has selected row to be possible to delete and when there is no selection to be not possible.


  • Moderators

    You can connect to selectionChanged() signal and use selectedRows()inside the connected slot to check if any row is selected.



  • @Chris-Kawa

    i did this

    connect(ui->tableView_partners->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
                this, SLOT(enableDeleteButton()));
    

    void Partners::enableDeleteButton()
    {

    if (ui->tableView_partners->selectionModel()->selectedRows().isEmpty())
    {
        ui->pushButton_delete->setEnabled(false);
     }
    
    else {
    
        ui->pushButton_delete->setEnabled(true);
    }
    

    }

    Unfortunately it works until new row is added.When a row is added there is no selected index in the tableView, but the button is enabled.Probably after adding row tableView selection is empty, but the model by default has last row selected or something like this.
    Is it possible aways to have selected rows?Any ideas?


  • Lifetime Qt Champion

    Do you mean enableDeleteButton is not called if you add a new row to the table ?



  • @SGaist
    YES


  • Lifetime Qt Champion

    So adding a new row clears the current selection and this doesn't result in your slot being called ? Sounds like it could be a little bug.

    Can you reproduce this with a minimal compilable example ?



  • @SGaist

    Here is the link to the example: Example
    What i want to achieve is when there is no selected row the button to be disabled.It works, but when select row and click add (adding new record) the button remain enabled, but no row is selected.Another option that i don't prefer is the view to have always selected row.


  • Lifetime Qt Champion

    I'm not fluent in Russian at all, I only saw the publicities. Where's the download ?



  • @SGaist
    Not only Russians use Cyrillic :) i'm bulgarian.I reuploaded the example here


  • Lifetime Qt Champion

    Sorry for the late reply, the page says that there's nothing to be found