Fetching data from table view
I've a QSqlQueryModel as a table view. I want users to delete some of entries by simply selecting them from the table and pressing delete button. I delete the entry by first column's value so I'm successful if user clicks to first column. I can't force user to click first column especially on S60 so I want to get first columns value if user clicks to other columns. How can I do it?
Ask if you want more information and thanks in advance. :)
Re: Fetching data from table view
1. Set up a menu item or a context menu with signal/slot to deleteEntry()
Example:
Code:
connect(ui->actionDelete_Log_Entry, SIGNAL(triggered(bool)),
this, SLOT(deleteEntry()));
2. All user has to do is click anywhere in a record (any column) then select your new menu item 'Delete'
or new context menu entry 'Delete'.
Code:
void MainWindow::deleteEntry() {
if (index.isValid()) {
model->removeRow(index.row());
model->submitAll();
}
}
Works fine for me.
Re: Fetching data from table view
Strangely, it returns "QSqlQuery::value: not positioned on a valid record" error when I press to the delete key.
My delete function:
Code:
{
model->removeRow(index.row());
model->submitAll();
}
lessonsTable function:
Code:
{
model->setTable("lessons");
model->select();
model->removeColumn(0); // Don't show the ID
model->setHeaderData(0, Qt::Horizontal, "Name");
model->setHeaderData(1, Qt::Horizontal, "Teacher");
return model;
}
And where I call the function:
Code:
void osman::on_pushButton_del_lesson_clicked()
{
lsn.deleteLesson(ui->tableView_lessons->currentIndex());
}
Re: Fetching data from table view
Looks like you are creating a NEW model in your delete function, and trying to delete from that one, which would be empty, instead of deleting from the existing model.
Re: Fetching data from table view
I've set my existing model's edit strategy to QSqlTableModel::OnFieldChange and when user edits the table directly it returns the same error too. Is it a problem with model or my database connection?
Re: Fetching data from table view
How does
Code:
lsn.deleteLesson(ui->tableView_lessons->currentIndex())
In your button clicked calling function relate to
You are passing a tableview index in the calling function to who knows what in the deleteLesson slot.
Also have you connected the signal and slot properly?
Re: Fetching data from table view