PDA

View Full Version : Fetching data from table view



utkuaydin
17th January 2010, 16:38
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. :)

waynew
18th January 2010, 01:43
1. Set up a menu item or a context menu with signal/slot to deleteEntry()

Example:


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'.




void MainWindow::deleteEntry() {
QModelIndex index = view->currentIndex();
if (index.isValid()) {
QSqlRecord record = model->record(index.row());
model->removeRow(index.row());
model->submitAll();
}
}


Works fine for me.

utkuaydin
18th January 2010, 07:58
Strangely, it returns "QSqlQuery::value: not positioned on a valid record" error when I press to the delete key.

My delete function:



void lessons::deleteLesson(QModelIndex index)
{
QSqlTableModel *model = this->lessonsTable();
model->removeRow(index.row());
model->submitAll();
}


lessonsTable function:



QSqlTableModel* lessons::lessonsTable()
{
QSqlTableModel *model = new QSqlTableModel;
model->setTable("lessons");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
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:



void osman::on_pushButton_del_lesson_clicked()
{
lsn.deleteLesson(ui->tableView_lessons->currentIndex());
}

waynew
18th January 2010, 12:42
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.

utkuaydin
18th January 2010, 13:30
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?

JD2000
18th January 2010, 14:21
How does


lsn.deleteLesson(ui->tableView_lessons->currentIndex())

In your button clicked calling function relate to


void lessons::deleteLesson(QModelIndex index)
{
QSqlTableModel *model = this->lessonsTable()
.....
}


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?

numbat
19th January 2010, 07:27
Also, get rid of the removeColumn and hide it in the view instead (http://stackoverflow.com/questions/1038954/problem-with-qsqltablemodel-no-automatic-updates).