我用sqlite保存数据,其中一列是中文文本。我在执行“模型->设置排序(0,Qt::升序)”时使用QSqlTableModel+QTableView.But,但第0列的数据是中文的,排序不正确。我知道这可能是编码问题,但我不知道如何修改SQLite的编码。
发布于 2020-05-11 16:19:14
不是编码问题。可以使用QSortFilterProxyModel。以下是示例代码:
#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();
}

https://stackoverflow.com/questions/61722547
复制相似问题