zed220
10th August 2017, 10:01
Hi All,
I'm using [Qt] QTableView and QSqlQueryModel with SQLite Database, I'm trying to delete rows from the QTableView(from the database) by clicking on a row which opens up another page where I carry out the deletion and using 'emit' signal and slot to update the parent page, it all works fine till the VERY LAST ROW. It deletes from the database but remains in the
QTableView until I reload the page, why ?
while(qry.next())
{
QSqlQueryModel *model = new QSqlQueryModel(ui->tableView_staffLog);
model->setQuery(qry);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Depart"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Car Reg"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("Srt Date"));
model->setHeaderData(5, Qt::Horizontal, QObject::tr("End Date"));
model->setHeaderData(6, Qt::Horizontal, QObject::tr("Contact"));
model->setHeaderData(7, Qt::Horizontal, QObject::tr("Photo"));
ui->tableView_staffLog->setModel(model);
}
connect(ui->tableView_staffLog, SIGNAL(pressed(const QModelIndex&)), this,
SLOT(on_tableView_staffLog_clicked(const QModelIndex&)));
}
void ControlPanel::on_tableView_staffLog_clicked(const QModelIndex& index)
{
const QAbstractItemModel* tableModel = ui->tableView_staffLog->model();
int emp_id = tableModel->data(tableModel->index(index.row(),0), Qt::DisplayRole).toInt();
employeedetails = EmployeeDetails::instance(emp_id, index.row(), this);
connect( employeedetails, SIGNAL(employeeDeleted(int)), this, SLOT(deleteEmployee(int)) );
employeedetails->show();
}
Child page
query.prepare("UPDATE employee SET is_hidden = '1' WHERE emp_id = (:emp_id)");
query.bindValue(":emp_id", emp_id);
success = query.exec();
if(!success)
{
qDebug() << "removeEmployee error: "
<< query.lastError();
}
else
{
if( tableRow >= 0 )
emit employeeDeleted(tableRow);
this->close();
}
I'm using [Qt] QTableView and QSqlQueryModel with SQLite Database, I'm trying to delete rows from the QTableView(from the database) by clicking on a row which opens up another page where I carry out the deletion and using 'emit' signal and slot to update the parent page, it all works fine till the VERY LAST ROW. It deletes from the database but remains in the
QTableView until I reload the page, why ?
while(qry.next())
{
QSqlQueryModel *model = new QSqlQueryModel(ui->tableView_staffLog);
model->setQuery(qry);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Depart"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Car Reg"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("Srt Date"));
model->setHeaderData(5, Qt::Horizontal, QObject::tr("End Date"));
model->setHeaderData(6, Qt::Horizontal, QObject::tr("Contact"));
model->setHeaderData(7, Qt::Horizontal, QObject::tr("Photo"));
ui->tableView_staffLog->setModel(model);
}
connect(ui->tableView_staffLog, SIGNAL(pressed(const QModelIndex&)), this,
SLOT(on_tableView_staffLog_clicked(const QModelIndex&)));
}
void ControlPanel::on_tableView_staffLog_clicked(const QModelIndex& index)
{
const QAbstractItemModel* tableModel = ui->tableView_staffLog->model();
int emp_id = tableModel->data(tableModel->index(index.row(),0), Qt::DisplayRole).toInt();
employeedetails = EmployeeDetails::instance(emp_id, index.row(), this);
connect( employeedetails, SIGNAL(employeeDeleted(int)), this, SLOT(deleteEmployee(int)) );
employeedetails->show();
}
Child page
query.prepare("UPDATE employee SET is_hidden = '1' WHERE emp_id = (:emp_id)");
query.bindValue(":emp_id", emp_id);
success = query.exec();
if(!success)
{
qDebug() << "removeEmployee error: "
<< query.lastError();
}
else
{
if( tableRow >= 0 )
emit employeeDeleted(tableRow);
this->close();
}