T
T
timao2015-02-05 14:03:57
Qt
timao, 2015-02-05 14:03:57

QAbstractTableModel or QSqlTableModel?

I need a multi-page QSqlTableModel with goodies (search, sorting), but I'm still poorly versed in the possibilities of qt.
The plate in the database is large, and so that it does not load all, I want to stick buttons like "next page" - "previous" and do a search + sorting (by all data, not only on this page).
I found these components, but most likely not all will be needed. Can you tell me which link to choose?

  • QAbstractTableModel
  • QSortFilterProxyModel
  • QItemDelegate
  • QSqlRelationalDelegate
  • QSqlTableModel
  • QSqlQueryModel
  • QSQLQuery

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tugo, 2015-02-05
@tugo

1. Understand how model, view, and delegate interact. It's well described here .
2. Read in Blanchet, Summerfield - QT4 GUI Programming on ... start at page 326.
The table will not load in its entirety. The view (a table view, you will be represented by the QTableView class) will request from the model (the model will be represented by either the QSqlTableModel class (editable) or the QSqlQueryModel class (read-only)) only the amount of data that fits on the screen. As the table scrolls on the screen, new data will be requested from the model.
Yes, you will need QSortFilterProxyModel if you want to sort. Do it without it for now, then you can insert QSortFilterProxyModel without problems.
In order for the view (what is on the screen) to present the data from the model in the way you need, you need to use delegates.
If the data is numbers and strings and that is how it should be represented, then you do not need to use your own delegates.
If you want something non-standard, like there is a number in the data, but you need to present it in a certain color, then you need to implement your own delegate.
For starters, don't worry about delegates.

T
timao, 2015-03-26
@timao

If someone stumbles:
As a result, I inherited from QAbstractTableModel and made almost a copy of QSqlTableModel, but with one caveat: the data is loaded in batches of 25 rows. Much to my surprise, the table does not freeze when scrolling intensively. Almost invisible to the eye. Key pieces of code:

QVariant DynamicSqlTableModel::data(const QModelIndex &index, int role) const {
  if (role == Qt::DisplayRole) {
    initializePackageByRow(index.row());
    return data_list[index.row()].value(index.column());
  }
  return QVariant();
}
void DynamicSqlTableModel::initializePackageByRow(int row) const {
  for (int i = data_list.size(); i <= row; i+= package_size) {
    QString package_query = setLimit(query, QString("%1, %2").arg(i).arg(package_size)); //моя строковая ф-я

    QSqlQuery q = QSqlQuery();
    q.setForwardOnly(true);
    if (!q.exec(package_query))  {
      emit error(logger(q, LOG));
      return;
    }
    while(q.next())
      data_list.push_back(q.record());
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question