PDA

View Full Version : [solved]My SqlModel and refresh view



Hostel
15th September 2011, 22:55
I have a model - yes is not very elegant but I need class like this:


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;
}


All methods are redirect to model like:



int MySqlModel::rowCount(const QModelIndex &parent) const
{
return model->rowCount();
}


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
beginInsertRows( QModelIndex(), 0, 0 );
endInsertRows();
}
}


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


Solution I describe in listening with insert method.