you are right I noticed that now. What a stupid mistake 
rowCount variable comes from here:
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
int rowCount;
~MainWindow();
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
int rowCount;
~MainWindow();
To copy to clipboard, switch view to plain text mode
when I add new contact, "rowCount" increased by one and then item() returns 0 and can't call text(). So it crashes. I understand.
----
In last one hour I wrote completely new code. And when I saw your reply, I now have two different code.
Code 2:
ChangeDialog dg;
QModelIndexList indexes = ui->tableView->selectionModel()->selection().indexes();
for(int i = 0; i < indexes.count(); ++i){
listOne.append(v.toString());
}
dg.SetList(listOne);
if(dg.
exec() == QDialog::Accepted){ list = dg.GetList();
for(int i = 0; i < 4;i++){
item->setEditable(false);
model->setItem(ui->tableView->currentIndex().row(),i,item);
}
}
ChangeDialog dg;
QStringList listOne;
QModelIndexList indexes = ui->tableView->selectionModel()->selection().indexes();
for(int i = 0; i < indexes.count(); ++i){
QModelIndex index = indexes.at(i);
QVariant v = index.data();
listOne.append(v.toString());
}
dg.SetList(listOne);
QStringList list;
if(dg.exec() == QDialog::Accepted){
list = dg.GetList();
for(int i = 0; i < 4;i++){
QStandardItem *item = new QStandardItem(QString("%1").arg(list.at(i)));
item->setEditable(false);
model->setItem(ui->tableView->currentIndex().row(),i,item);
}
}
To copy to clipboard, switch view to plain text mode
And old code
Code 1:
ChangeDialog dg;
dg.setWindowTitle(tr("Edit contact"));
for(int i = 0; i < 4; i++){
Slist.append(model->item(ui->tableView->currentIndex().row(),i)->text());
//QMessageBox::information(this,"title","asd");
}
dg.SetList(Slist);
if(dg.
exec() == QDialog::Accepted){ list2 << dg.GetList();
for(int i = 0; i < 4;i++){
item2->setEditable(false);
model->setItem(ui->tableView->currentIndex().row(),i,item2);
}
}
ChangeDialog dg;
dg.setWindowTitle(tr("Edit contact"));
QStringList Slist;
for(int i = 0; i < 4; i++){
Slist.append(model->item(ui->tableView->currentIndex().row(),i)->text());
//QMessageBox::information(this,"title","asd");
}
dg.SetList(Slist);
QStringList list2;
if(dg.exec() == QDialog::Accepted){
list2 << dg.GetList();
for(int i = 0; i < 4;i++){
QStandardItem *item2 = new QStandardItem(QString("%1").arg(list2.at(i)));
item2->setEditable(false);
model->setItem(ui->tableView->currentIndex().row(),i,item2);
}
}
To copy to clipboard, switch view to plain text mode
But I changed Code 1's rowCount to this: "ui->tableView->currentIndex().row()"
------
Both code do the same job but both have one problem. When I add new contact and then I edit contact. Edited contact is not fully edited.
Here is a picture:
1.JPG
2.JPG
3.JPG
BUT,when I change Code 1's: "ui->tableView->currentIndex().row()" to This: "rowCount",Here is a result
5.JPG
And the same happens if I change Code 2's ""ui->tableView->currentIndex().row()" to "rowCount".
I dont want to add new contact. I want fully edit contact,not just NAME field 
Added after 19 minutes:
I solved Code 2's problem. I changed this line:
model->setItem(indexes.at(i).row(),i,item);
model->setItem(indexes.at(i).row(),i,item);
To copy to clipboard, switch view to plain text mode
And this works 
In Code 1. I'm still thinking to find solution
Bookmarks