I have a model - yes is not very elegant but I need class like this:
{
public:
/*
there are all methods needed by model like
rowCount, columnCount, data ...
*/
bool insert( const QSqlRecord& rec );
protected:
}
class MySqlModel : public QAbstractTableModel
{
public:
/*
there are all methods needed by model like
rowCount, columnCount, data ...
*/
bool insert( const QSqlRecord& rec );
protected:
QSqlQueryModel* model;
QSqlQuery* query;
}
To copy to clipboard, switch view to plain text mode
All methods are redirect to model like:
int MySqlModel
::rowCount(const QModelIndex &parent
) const {
return model->rowCount();
}
int MySqlModel::rowCount(const QModelIndex &parent) const
{
return model->rowCount();
}
To copy to clipboard, switch view to plain text mode
I'm making insert to table by method which looks like that:
bool MySqlModel::insert( const QSqlRecord& rec )
{
query->prepare( insertQuery );
if( query->exec() )
{
// here I want to refresh a view
// I remove this -> emit dataChanged( QModelIndex(), QModelIndex() ); // nothing happens :(
// this methods resolved my issue
endInsertRows();
}
}
bool MySqlModel::insert( const QSqlRecord& rec )
{
query->prepare( insertQuery );
if( query->exec() )
{
// here I want to refresh a view
// I remove this -> emit dataChanged( QModelIndex(), QModelIndex() ); // nothing happens :(
// this methods resolved my issue
beginInsertRows( QModelIndex(), 0, 0 );
endInsertRows();
}
}
To copy to clipboard, switch view to plain text mode
How I should update view? I create view like this:
view->setModel( model ); // model it is MySqlModel and data are in view after this operation - view is a standard QTableView
view->setModel( model ); // model it is MySqlModel and data are in view after this operation - view is a standard QTableView
To copy to clipboard, switch view to plain text mode
Solution I describe in listening with insert method.
Bookmarks