[Interest] QSortFilterProxyModel slow updating sql model attached

David Boosalis david.boosalis at gmail.com
Wed May 2 01:51:43 CEST 2012


I've not put much stock in the QSqlQueryModel, but instead done my models
derived from  QStandardModel.  This requires you to write more code for the
SQL transactions using QT's SQL module (or your own).  Granted you have to
write more code and you don't get notices from outside changes to the
database. But with your own mode,l proxy filters are easier to implement.

Taking it a bit further I create my own database class, which does all the
sql stuff.  For example part of my Database class looks like this:


class Database : public QObject {
  Q_OBJECT
    public:
  enum
{OK=0,Error=1,SQL_ERROR,TableDoesNotExists,TableFormatBad,EntryExists,
    EntryDoesNotExists};

  typedef  enum {Users,Profiles,UssageLog,Models,...NumOfTables} TableType;

  enum {NameSize=32,DescriptionSize=256};
  static QString tableNames[NumOfTables];
  static QString arguments[NumOfTables];
  Database(const QString &name,const QString &user,const QString &pass);
  ~Database();

  bool createTable(TableType);
  bool isOpen();
  bool open();
  void close();
  bool tableIsValid(TableType);
  QString getLastError();
  QSqlDatabase *getHandle();
  bool  validateUsersTable();
  bool  updateUser(User *);
  bool  updateUsersExpirationDate(int userID,QDate expirationDate);
  bool  addUser(User *);
  bool  deleteUser(qint32 userID);
  UserList  *getUsers();
.....


I don't use the database very much. As a result I can read all the records
on the application startup, cached the records and then be done with the
database except for seldom inserts, edits, and deletes.  Maybe this
approach will work for you.

-David

PS
One error I've seen with this approach is that my Qt database handle can go
state after a day, (This code ran on a server). Meaning that all SQL
Queries fail to talk to the MySQL database.  I got around this by
restarting the server code every day from my cron file




On Tue, May 1, 2012 at 3:11 PM, Linos <info at linos.es> wrote:

> Hi,
>        any tips on what should i be using to sort a sql table when user
> click in
> headers? Thanks!
>
> Regrds,
> Miguel Ángel.
>
> El 01/05/12 07:13, Bo Thorsen escribió:
> > Hi,
> >
> > My advice is to ditch QSortFilterProxyModel completely. It's great for
> > models that can be held in memory. But for SQL models, it sucks.
> >
> > Bo.
> >
> > Den 30-04-2012 19:02, Linos skrev:
> >> Hi,
> >>     i have a QTableView with a QSortFilterProxyModel using a
> QSqlQueryModel as
> >> source model, i am using Qt 4.8.1 in Linux x86_64.
> >>
> >> if i don't apply sort to any of the columns of the table and execute a
> new
> >> .select() on the QSqlQueryModel it does the usual and only call .data
> method for
> >> the visible rows after the new data has been populated but if i have
> the table
> >> sorted by any column the data method gets called 56000 times for a 1000
> row
> >> table ever using indexes of the sorted col, i can understand that it is
> sorting
> >> at the same it is creating indexes and data.
> >>
> >> The problem it is i can't get it to ignore sorting to do manually after
> the new
> >> data is populated in the model. i have tried this:
> >>
> >> 1) call table.setSortingEnabled(False) (with
> proxyModel.setDynamicSortFilter()
> >> using true and false)
> >> 2) call proxyModel.invalidate() before model.select()
> >> 3) call proxyModel.reset() before model.select()
> >>
> >> I can't think other way to make this right now, i could set a fake
> model as
> >> source for the proxymodel before call select in model and set the
> original model
> >> after that but it seems awkward to me, should be a better way, so any
> help
> >> please? :)
> >>
> >> Regards,
> >> Miguel Angel.
> >> _______________________________________________
> >> Interest mailing list
> >> Interest at qt-project.org
> >> http://lists.qt-project.org/mailman/listinfo/interest
> >
> >
> > Bo Thorsen,
> > Fionia Software.
> >
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120501/eb1913de/attachment.html>


More information about the Interest mailing list