PeaceSheep
PeaceSheep

Reputation: 69

Why QT QSqlTableModel does not sort Chinese correctly?

I save data with sqlite,and one of the columns is Chinese text.I use QSqlTableModel+QTableView.But When I excute "Model->setSort(0, Qt::AscendingOrder)" and the data of column 0 is Chinese,it doesn't sort correctly.I know this may be a coding problem, but I don't know how to modify SQLite's coding,

Upvotes: 0

Views: 111

Answers (1)

lilucpp
lilucpp

Reputation: 306

Not a coding problem. QSortFilterProxyModel can be used. Below is the sample code:

#include <QApplication>
#include <QSortFilterProxyModel>
#include <QTableView>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQueryModel>

int main(int argc, char* argv[]) {
  QApplication a(argc, argv);

  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  QString dbPath = QCoreApplication::applicationDirPath() + "/test.db";
  db.setDatabaseName(dbPath);
  db.open();

  QSqlQueryModel* model = new QSqlQueryModel();
  model->setQuery("select * from test");
  QSortFilterProxyModel* sort = new QSortFilterProxyModel();
  sort->setSortLocaleAware(true);
  sort->setSourceModel(model);
  QTableView table;
  table.setModel(sort);
  table.setSortingEnabled(true);
  table.show();
  return a.exec();
}

enter image description here

Upvotes: 1

Related Questions