Skip to content

Commit

Permalink
[fix Qt6]修复Qt6编译错误;修复一些严重的警告
Browse files Browse the repository at this point in the history
[bugs]: treeview项目QML在Qt6运行界面问题和model过滤问题未修复
  • Loading branch information
yuriyoung committed Jun 25, 2024
1 parent df1f5f6 commit f8431b7
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/gridview/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/tableview/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 36 additions & 9 deletions src/tableview/migration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
#include <QSqlError>
#include <QLoggingCategory>

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QRegExp>
#else
#include <QRegularExpression>
#endif

Q_LOGGING_CATEGORY(lcMigration, "app.Migration")

class MigrationPrivate
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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();
Expand Down Expand Up @@ -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()));
Expand Down
5 changes: 2 additions & 3 deletions src/tableview/sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
const QString DRIVER = "QSQLITE";
static QThreadStorage<QSqlDatabase> databasePool;

class Sql
namespace Sql
{
public:
static QSqlDatabase database(const QString &databaseName = QString(), bool open = true)
{
QSqlDatabase db;
Expand Down Expand Up @@ -70,6 +69,6 @@ class Sql
QString connectionName = QUuid::createUuid().toString(QUuid::Id128);
return QSqlDatabase::database(connectionName, true);
}
};
} // namespace Sql

#endif // SQL_H
3 changes: 2 additions & 1 deletion src/tableview/tablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ class TableModelPrivate;
class TableModel : public QSqlRelationalTableModel, public QQmlParserStatus
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
Q_DECLARE_PRIVATE(TableModel)
QScopedPointer<TableModelPrivate> 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:
Expand Down
38 changes: 32 additions & 6 deletions src/treeview/jsontreemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@
#include <QJSValue>
#include <QDebug>

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:
Expand Down Expand Up @@ -127,22 +150,21 @@ void JsonTreeModel::setJson(const QVariant &value)
data = data.value<QJSValue>().toVariant();

QJsonDocument doc;
if(data.canConvert(QVariant::Url))
if(data.canConvert<QUrl>())
{
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))
{
qCritical() << file.errorString() << file.fileName();
return;
}
QByteArray json = file.readAll();
doc = QJsonDocument::fromBinaryData(json);
if(doc.isNull())
doc = QJsonDocument::fromJson(json);
doc = QJsonDocument::fromJson(json);

file.close();
}
Expand All @@ -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());

Expand Down
6 changes: 4 additions & 2 deletions src/treeview/qml/TreeView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/treeview/qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/treeview/res.qrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/">
<file>resources/arrow.png</file>
<file>resources/tree.json</file>
</qresource>
</RCC>
11 changes: 7 additions & 4 deletions src/treeview/treemodelproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit f8431b7

Please sign in to comment.