简体   繁体   中英

Qt signal and slot doesn't work when QTableView is updated

I have a QTableView and a button. I have used signal and slot to connect table view with a function. The slot performs following function. Whenever user selects single row the button must be enabled. If user doesn't select a row or if more than one row is selected, then the button must be disabled. This is the code.

connect(ui->client_table->selectionModel(),SIGNAL(selectionChanged(const QItemSelection &, constQItemSelection &)),SLOT(disableButtons(const QItemSelection &, const QItemSelection &)));

The problem starts when i refresh the table view. Then the signal and slot stops working. The code to refresh the table is given below.

void client_table_view::refreshTable()
{



    dbconnector db;
    QSqlQueryModel* modal = new QSqlQueryModel();
    QSqlQuery* qry = new QSqlQuery(db.digi_db);


    qry->exec("select client_id,company_name,contact_name,address,email,phone from client_details");

    int qrycount = 0;


    modal->setQuery(*qry);
    ui->client_table->setModel(modal);
}

How can i change the code so that i can refresh the table along with implementing the signal and slot mechanism.

Add this at the start of refresh() to tidy up:

disconnect(ui->client_table->selectionModel(),SIGNAL(selectionChanged(const QItemSelection &, constQItemSelection &)),SLOT(disableButtons(const QItemSelection &, const QItemSelection &)));

And then use this to connect with you new model at the end of refresh():

connect(ui->client_table->selectionModel(),SIGNAL(selectionChanged(const QItemSelection &, constQItemSelection &)),SLOT(disableButtons(const QItemSelection &, const QItemSelection &)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM