From f8431b76c41701a82a754b633ca43dab7f879543 Mon Sep 17 00:00:00 2001 From: Yuri Date: Tue, 25 Jun 2024 20:04:05 +0800 Subject: [PATCH] =?UTF-8?q?[fix=20Qt6]=E4=BF=AE=E5=A4=8DQt6=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E9=94=99=E8=AF=AF=EF=BC=9B=E4=BF=AE=E5=A4=8D=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E4=B8=A5=E9=87=8D=E7=9A=84=E8=AD=A6=E5=91=8A=20[bugs]?= =?UTF-8?q?:=20treeview=E9=A1=B9=E7=9B=AEQML=E5=9C=A8Qt6=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E9=97=AE=E9=A2=98=E5=92=8Cmodel=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E9=97=AE=E9=A2=98=E6=9C=AA=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gridview/main.cpp | 2 ++ src/tableview/main.cpp | 3 ++- src/tableview/migration.cpp | 45 ++++++++++++++++++++++++++------- src/tableview/sql.h | 5 ++-- src/tableview/tablemodel.h | 3 ++- src/treeview/jsontreemodel.cpp | 38 +++++++++++++++++++++++----- src/treeview/qml/TreeView.qml | 6 +++-- src/treeview/qml/main.qml | 2 +- src/treeview/res.qrc | 1 + src/treeview/treemodelproxy.cpp | 11 +++++--- 10 files changed, 89 insertions(+), 27 deletions(-) diff --git a/src/gridview/main.cpp b/src/gridview/main.cpp index e23d9aa..6e42c47 100644 --- a/src/gridview/main.cpp +++ b/src/gridview/main.cpp @@ -20,7 +20,9 @@ int main(int argc, char *argv[]) { +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); +#endif QGuiApplication app(argc, argv); diff --git a/src/tableview/main.cpp b/src/tableview/main.cpp index b4eb369..2320984 100644 --- a/src/tableview/main.cpp +++ b/src/tableview/main.cpp @@ -7,8 +7,9 @@ int main(int argc, char *argv[]) { +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); - +#endif QGuiApplication app(argc, argv); // create database and tables diff --git a/src/tableview/migration.cpp b/src/tableview/migration.cpp index 771a543..3ede573 100644 --- a/src/tableview/migration.cpp +++ b/src/tableview/migration.cpp @@ -36,6 +36,12 @@ #include #include +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) +#include +#else +#include +#endif + Q_LOGGING_CATEGORY(lcMigration, "app.Migration") class MigrationPrivate @@ -157,7 +163,13 @@ QStringList MigrationPrivate::resolveStatements(const QString &file) const QString sql = sqlFile.readAll().trimmed(); sqlFile.close(); - statements = sql.split(QRegExp(";"), QString::SkipEmptyParts); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + static QRegExp re(";"); + statements = sql.split(re, QString::SkipEmptyParts); +#else + static QRegularExpression re(";"); + statements = sql.split(re, Qt::SkipEmptyParts); +#endif // if(statements.last() == "\n" || statements.last() == "\r\n") // statements.removeLast(); @@ -227,7 +239,11 @@ QSqlDatabase Migration::connection() const QStringList Migration::files() const { Q_D(const Migration); +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) return d->files.toList(); +#else + return d->files.values(); +#endif } /** @@ -257,7 +273,11 @@ bool Migration::run(const QStringList &files) return true; } - return this->runMigration(d->files.toList()); +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + return d->files.toList(); +#else + return this->runMigration(d->files.values()); +#endif } /** @@ -287,8 +307,11 @@ bool Migration::run(const QString &path) qWarning(lcMigration) << "Nothing to migrate."; return true; } - - return this->runMigration(d->files.toList()); +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + return d->files.toList(); +#else + return this->runMigration(d->files.values()); +#endif } /** @@ -358,10 +381,12 @@ bool Migration::migrateUp(const QString &file) if(transacted) d->connection.transaction(); + + QSqlQuery query(d->connection); foreach(const QString &cmd, statements) { - d->connection.exec(cmd); - if(d->connection.lastError().type() != QSqlError::NoError) + query.exec(cmd); + if(query.lastError().type() != QSqlError::NoError) { qCritical(lcMigration) << "Migration up error '" << file << "' " << d->connection.lastError().text(); qCritical(lcMigration) << cmd; @@ -387,7 +412,7 @@ bool Migration::migrateDown(const QString &file) QString tableName = d->resolveInstance(file); bool transacted = d->connection.driver()->hasFeature(QSqlDriver::Transactions); - QStringList tables = d->connection.tables(); + // QStringList tables = d->connection.tables(); if(transacted) { @@ -396,7 +421,8 @@ bool Migration::migrateDown(const QString &file) // foreach(const QString &table, tables) // { const QString cmd = QString("DROP TABLE %1").arg(tableName); - d->connection.exec(cmd); + QSqlQuery query(d->connection); + query.exec(cmd); if(d->connection.lastError().type() != QSqlError::NoError) { qCritical(lcMigration) << "Migration down error" << d->connection.lastError().text(); @@ -521,7 +547,8 @@ bool Migration::createRepository() Q_D(Migration); if(!this->repositoryExists()) { - d->connection.exec(QString("CREATE TABLE %1 (" + QSqlQuery query(d->connection); + query.exec(QString("CREATE TABLE %1 (" "id INTEGER PRIMARY KEY AUTOINCREMENT," "migration VARCHAR(255) NOT NULL DEFAULT ''," "batch INTEGER NOT NULL DEFAULT 1)").arg(table())); diff --git a/src/tableview/sql.h b/src/tableview/sql.h index ccfc9d1..a1be589 100644 --- a/src/tableview/sql.h +++ b/src/tableview/sql.h @@ -36,9 +36,8 @@ const QString DRIVER = "QSQLITE"; static QThreadStorage databasePool; -class Sql +namespace Sql { -public: static QSqlDatabase database(const QString &databaseName = QString(), bool open = true) { QSqlDatabase db; @@ -70,6 +69,6 @@ class Sql QString connectionName = QUuid::createUuid().toString(QUuid::Id128); return QSqlDatabase::database(connectionName, true); } -}; +} // namespace Sql #endif // SQL_H diff --git a/src/tableview/tablemodel.h b/src/tableview/tablemodel.h index 2a3b2e3..af3452d 100644 --- a/src/tableview/tablemodel.h +++ b/src/tableview/tablemodel.h @@ -31,11 +31,12 @@ class TableModelPrivate; class TableModel : public QSqlRelationalTableModel, public QQmlParserStatus { Q_OBJECT + Q_INTERFACES(QQmlParserStatus) Q_DECLARE_PRIVATE(TableModel) QScopedPointer d_ptr; Q_PROPERTY(QString database READ databaseName WRITE setDatabaseName NOTIFY databaseNameChanged) Q_PROPERTY(QString table READ tableName WRITE setTable NOTIFY tableChanged) - Q_PROPERTY(QString selectedRows READ selectedRows NOTIFY selectionChanged) + Q_PROPERTY(int selectedRows READ selectedRows NOTIFY selectionChanged) Q_PROPERTY(QString errorString READ errorString) Q_ENUMS(ItemStatus) public: diff --git a/src/treeview/jsontreemodel.cpp b/src/treeview/jsontreemodel.cpp index 198623a..25374bd 100644 --- a/src/treeview/jsontreemodel.cpp +++ b/src/treeview/jsontreemodel.cpp @@ -27,6 +27,29 @@ #include #include +namespace { + +QString urlToLocalFileOrQrc(const QUrl &url) +{ + const QString scheme(url.scheme().toLower()); + if (scheme == QLatin1String("qrc")) { + if (url.authority().isEmpty()) + return QLatin1Char(':') + url.path(); + return QString(); + } + +#if defined(Q_OS_ANDROID) + if (scheme == QLatin1String("assets")) { + if (url.authority().isEmpty()) + return url.toString(); + return QString(); + } +#endif + return url.toLocalFile(); +} + +} + class JsonTreeModelPrivate : public TreeModelPrivate { public: @@ -127,12 +150,13 @@ void JsonTreeModel::setJson(const QVariant &value) data = data.value().toVariant(); QJsonDocument doc; - if(data.canConvert(QVariant::Url)) + if(data.canConvert()) { QUrl url = data.toUrl(); - if(url.isLocalFile()) + const auto file = urlToLocalFileOrQrc(url); + if(!file.isEmpty()) { - d->file = url.toLocalFile(); + d->file = file; QFile file(d->file); if(!file.open(QIODevice::ReadOnly)) { @@ -140,9 +164,7 @@ void JsonTreeModel::setJson(const QVariant &value) return; } QByteArray json = file.readAll(); - doc = QJsonDocument::fromBinaryData(json); - if(doc.isNull()) - doc = QJsonDocument::fromJson(json); + doc = QJsonDocument::fromJson(json); file.close(); } @@ -166,7 +188,11 @@ void JsonTreeModel::setJson(const QVariant &value) // dynamic role name for roleNames parseKeys(doc); +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) setRoleNames(d->keys.toList()); +#else + setRoleNames(d->keys.values()); +#endif if(d->keys.size() > columnCount()) this->insertColumns(columnCount(), d->keys.size() - columnCount()); diff --git a/src/treeview/qml/TreeView.qml b/src/treeview/qml/TreeView.qml index ffd2aad..36e3306 100644 --- a/src/treeview/qml/TreeView.qml +++ b/src/treeview/qml/TreeView.qml @@ -38,15 +38,17 @@ Item { anchors.right: parent.right anchors.leftMargin: 2 anchors.rightMargin: verticalScrollbar.width - implicitHeight: filterField.height + implicitHeight: filterField.height < minimumHeight ? minimumHeight : filterField.height background: Rectangle { color: "red"; anchors.fill: parent } TextField { id: filterField width: parent.width + implicitHeight: 30 onTextChanged: { listView.searchText = text; - sortFilterModel.setFilterRegExp(text) + // sortFilterModel.setFilterRegExp(text) qt5 + // sortFilterModel.setFilterRegularExpression(text) qt6 } } } diff --git a/src/treeview/qml/main.qml b/src/treeview/qml/main.qml index 083279e..75e4292 100644 --- a/src/treeview/qml/main.qml +++ b/src/treeview/qml/main.qml @@ -20,7 +20,7 @@ Window { model: JsonModel { id: jsonModel - json: "file:./tree.json" // path to json file + json: "qrc:/resources/tree.json" // path to json file } delegate: TreeItem { id: delegateItem diff --git a/src/treeview/res.qrc b/src/treeview/res.qrc index 3de49f2..7fa911f 100644 --- a/src/treeview/res.qrc +++ b/src/treeview/res.qrc @@ -1,5 +1,6 @@ resources/arrow.png + resources/tree.json diff --git a/src/treeview/treemodelproxy.cpp b/src/treeview/treemodelproxy.cpp index 4ee48d3..daa8421 100644 --- a/src/treeview/treemodelproxy.cpp +++ b/src/treeview/treemodelproxy.cpp @@ -226,6 +226,8 @@ QVariant TreeModelProxy::data(const QModelIndex &index, int role) const return QVariant(); const QModelIndex &modelIndex = d->mapToModel(index); + if(!modelIndex.isValid()) + return QVariant(); switch (role) { @@ -234,7 +236,8 @@ QVariant TreeModelProxy::data(const QModelIndex &index, int role) const case TreeModelProxyPrivate::ExpandedRole: return isExpanded(index.row()); case TreeModelProxyPrivate::HasChildrenRole: - return !(modelIndex.flags() & Qt::ItemNeverHasChildren) && d->model->hasChildren(modelIndex); + return d->model->hasChildren(modelIndex); + // return (modelIndex.flags() & Qt::ItemNeverHasChildren) && d->model->hasChildren(modelIndex); case TreeModelProxyPrivate::HasSiblingRole: return modelIndex.row() != d->model->rowCount(modelIndex.parent()) - 1; case TreeModelProxyPrivate::ModelIndexRole: @@ -579,7 +582,7 @@ QItemSelection TreeModelProxy::selectionRange(const QModelIndex &fromIndex, cons QItemSelection sel; sel.reserve(ranges.count()); - for (const MIPair &pair : qAsConst(ranges)) + for (const MIPair &pair : std::as_const(ranges)) sel.append(QItemSelectionRange(pair.first, pair.second)); return sel; @@ -856,7 +859,7 @@ void TreeModelProxy::modelRowsAboutToBeMoved(const QModelIndex &sourceParent, in { for (int i = endIndex + 1; i < destIndex; i++) { - d->items.swap(i, i - totalMovedCount); // Fast move from 1st to 2nd position + d->items.move(i, i - totalMovedCount); // Fast move from 1st to 2nd position } bufferCopyOffset = destIndex - totalMovedCount; } @@ -865,7 +868,7 @@ void TreeModelProxy::modelRowsAboutToBeMoved(const QModelIndex &sourceParent, in // NOTE: we will not enter this loop if startIndex == destIndex for (int i = startIndex - 1; i >= destIndex; i--) { - d->items.swap(i, i + totalMovedCount); // Fast move from 1st to 2nd position + d->items.move(i, i + totalMovedCount); // Fast move from 1st to 2nd position } bufferCopyOffset = destIndex; }