application crushes after performing selectionModel() from tableView
Hi guys,
i'm trying to get a current selected row from a tableview so i can retrieve data from database edit them and update the database.i'm using QSqlTableModel and QTableView.
When i run my application and push the editButton the application always crush,what seem to be a problem ,please help me here is the slot which cause the problem.
Code:
void customers::on_pushButton_2_clicked()
{
const QModelIndexList list = m_ui->customerView->selectionModel()->selection().indexes();
for (int i = 1; i < list.count(); i++)
{
int row = index.row();
rows.insert(index.row());
}
qDebug() << row;
query.
exec(QString("SELECT * FROM customer where Id = row"));
while (query.next()) {
db_id = query.value(0).toInt();
db_LastName = query.value(1).toString();
db_FirstName = query.value(2).toString();
db_Category = query.value(3).toString();
db_Amount = query.value(4).toString();
}
dialog = new newCustomer(this);
dialog->editCustomer(db_LastName);
int accepted = dialog->exec();
if(accepted == 1 )
{
}
}
Re: application crushes after performing selectionModel() from tableView
One of m_ui, m_ui->customerView or m_ui->customerView->selectionModel() is null or your list has less than two elements.
Re: application crushes after performing selectionModel() from tableView
the qDebug << name<< row; shows that the list contains 4 items shown below,whis is second row in a table view
"pop" 2
"pop" 2
"pop" 2
"pop" 2
but still the program crush .I modifies my code to one below here and i discovered that the "row" variable in a select statement is the one causing
the application to crush.....
Code:
void customers::on_pushButton_2_clicked()
{
int row;
const QModelIndexList list = m_ui->customerView->selectionModel()->selection().indexes();
for (int i = 1; i < list.count(); i++)
{
row = index.row();
QString name
= model
->record
(row
).
value("LastName").
toString();
qDebug() << name << row ;
}
query.
exec(QString("SELECT * FROM customer where Id = " + row
));
//when i remove the variable "row" the crush stops while (query.next()) {
db_id = query.value(0).toInt();
db_LastName = query.value(1).toString();
db_FirstName = query.value(2).toString();
db_Category = query.value(3).toString();
db_Amount = query.value(4).toString();
}
dialog = new newCustomer(this);
dialog->editCustomer(db_LastName);
int accepted = dialog->exec();
if(accepted == 1 )
{
}
}
Re: application crushes after performing selectionModel() from tableView
You are adding an integer to a const char *. What effect do you think it has?