Home > Mobile >  About the model in Qt QSqlTableModel - & gt; SubmitAll (); Submitted to the problem
About the model in Qt QSqlTableModel - & gt; SubmitAll (); Submitted to the problem

Time:11-05

Void A: : on_delBtn_clicked ()
{
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 submit

CodePudding 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, why

CodePudding user response:

Restart also cannot be deleted, do you have any other way

CodePudding user response:

Do you want to see you before the program initialization model, if you use the
 model - & 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 not

CodePudding 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
  •  Tags:  
  • Qt
  • Related