{
Int rowNum=UI - & gt; TableView - & gt; CurrentIndex (). The row ();
The model - & gt; RemoveRow (rowNum);
Int ok=QMessageBox: : warning (this, "prompt", "sure to delete the current line?" , QMessageBox: : No, QMessageBox: : Yes);
If (ok==QMessageBox: : No) {
The model - & gt; RevertAll ();//cancellation
}
The else
{
The model - & gt; SubmitAll ();//delete
}
}
So can't finish delete function
CodePudding user response:
I think this writing no problem but why don't submitCodePudding user response:
QSqlTableModel * model=new QSqlTableModel;The model - & gt; SetTable (" table ");
QSqlRecord record=model - & gt; Record ();
Record. SetValue (" id ", UI - & gt; IdLineEdit - & gt; The text ());
The model - & gt; InsertRecord (1, record);
If (model - & gt; SubmitAll ());//failure
Encounter with this kind of problem, the couple don't know is what reason,
But it can be:
QSqlQueryModel * model=new QSqlQueryModel;
The model - & gt; SetQuery (" insert into... ");
CodePudding user response:
To open Qt to solve it, whyCodePudding user response:
Restart also cannot be deleted, do you have any other wayCodePudding user response:
Do you want to see you before the program initialization model, if you use themodel - & gt; RemoveColumn ()function, then operating model, can't succeed,
CodePudding user response:
Is this problem solved the original poster I have baidu look bad or notCodePudding user response:
Problem solved, see if there are close the database connection,Possible within other functions, you call a QSqlDatabase object's close (), after the connection is broken, can't sunmitAll ()
There are two solutions, 1, close the database in the destructor, open the database inside a constructor, other functions need not care about, because there are QSqlTableModel help management.
2 is, in every need to open the database operation, operation to complete immediately shut down database, open and close in a function body, pay attention to the middle don't return ahead of schedule, etc.,
CodePudding user response:
In navicat, set the table index in the first column of the primary key, is not null can solve