c++ - 为什么QT QSqlTableModel不能正确排序中文?
问题描述
我用sqlite保存数据,其中一列是中文文本。我使用QSqlTableModel+QTableView。但是当我执行“Model->setSort(0, Qt::AscendingOrder)”并且第0列的数据是中文时,它没有'排序不正确。我知道这可能是编码问题,但我不知道如何修改 SQLite 的编码,
解决方案
不是编码问题。可以使用 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();
}
推荐阅读
- mongodb - Mogodb 聚合从 2 个数组创建输出为 {"key": "value"}
- php - 根据 Woocommerce 中的特定用户角色显示菜单项
- python-3.x - 无法获得漂亮的汤以返回正确的文章标题、链接和 img。帮忙调试?
- javascript - Pannellum clickHandlerFunc
- angular - 未处理的承诺拒绝:[object Object] 没有提供者!; 区:
; 任务:Promise.then;值:错误:[object Object] 没有提供者! - docker - 如何在 Jenkins Pipeline 上构建 Docker 映像,而不更改底层 Jenkins VM 的权限?
- php - htaccess 将规则重写到 Wordpress 页面而不更改 URL
- python-3.x - 分区键作为python azure存储表中的变量
- database - 将数据库用于人工智能?
- dialogflow-es - Dialogflow @sys.email 实体未识别电子邮件