forked from KDAB/cxx-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cxx-qt-lib: add QGuiApplication and use it in example
Related to KDAB#291
- Loading branch information
1 parent
132758a
commit be41e68
Showing
17 changed files
with
486 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#pragma once | ||
|
||
#ifdef CXX_QT_GUI_FEATURE | ||
#include <cstdint> | ||
#include <memory> | ||
|
||
#include <QtCore/QByteArray> | ||
#include <QtCore/QString> | ||
#include <QtCore/QStringList> | ||
#include <QtCore/QVector> | ||
#include <QtGui/QGuiApplication> | ||
|
||
#include "rust/cxx.h" | ||
|
||
namespace rust { | ||
namespace cxxqtlib1 { | ||
|
||
::std::unique_ptr<QGuiApplication> | ||
qguiapplicationNew(const QVector<QByteArray>& args); | ||
|
||
void | ||
qguiapplicationAddLibraryPath(QGuiApplication& app, const QString& path); | ||
QString | ||
qguiapplicationApplicationName(const QGuiApplication& app); | ||
QString | ||
qguiapplicationApplicationVersion(const QGuiApplication& app); | ||
::std::int32_t | ||
qguiapplicationExec(QGuiApplication& app); | ||
QStringList | ||
qguiapplicationLibraryPaths(const QGuiApplication& app); | ||
QString | ||
qguiapplicationOrganizationDomain(const QGuiApplication& app); | ||
QString | ||
qguiapplicationOrganizationName(const QGuiApplication& app); | ||
void | ||
qguiapplicationSetApplicationName(QGuiApplication& app, const QString& name); | ||
void | ||
qguiapplicationSetApplicationVersion(QGuiApplication& app, | ||
const QString& version); | ||
void | ||
qguiapplicationSetLibraryPaths(QGuiApplication& app, const QStringList& paths); | ||
void | ||
qguiapplicationSetOrganizationDomain(QGuiApplication& app, | ||
const QString& domain); | ||
void | ||
qguiapplicationSetOrganizationName(QGuiApplication& app, const QString& name); | ||
|
||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// SPDX-FileContributor: Leon Matthes <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
#ifdef CXX_QT_GUI_FEATURE | ||
#include "cxx-qt-lib/qguiapplication.h" | ||
|
||
#include <vector> | ||
|
||
#include <QtCore/QObject> | ||
|
||
namespace { | ||
|
||
class ArgsData : public QObject | ||
{ | ||
public: | ||
explicit ArgsData(const QVector<QByteArray>& args, QObject* parent = nullptr) | ||
: QObject(parent) | ||
, m_ownedVector(args) | ||
{ | ||
// Construct our vector of char* | ||
for (auto& bytes : m_ownedVector) { | ||
m_vector.emplace_back(bytes.data()); | ||
m_size += 1; | ||
} | ||
} | ||
|
||
char** data() { return m_vector.data(); } | ||
int& size() { return m_size; } | ||
|
||
private: | ||
QVector<QByteArray> m_ownedVector; | ||
int m_size = 0; | ||
std::vector<char*> m_vector; | ||
}; | ||
|
||
} | ||
|
||
namespace rust { | ||
namespace cxxqtlib1 { | ||
|
||
::std::unique_ptr<QGuiApplication> | ||
qguiapplicationNew(const QVector<QByteArray>& args) | ||
{ | ||
// Ensure that our QVector has the same lifetime as the QGuiApplication | ||
// by storing it inside a QObject that has QGuiApplication as it's parent | ||
auto argsData = new ArgsData(args); | ||
// Note that QGuiApplication uses a reference to an int for the size here | ||
// so we need to ensure that reference remains valid | ||
auto ptr = | ||
::std::make_unique<QGuiApplication>(argsData->size(), argsData->data()); | ||
Q_ASSERT(ptr != nullptr); | ||
argsData->setParent(ptr.get()); | ||
|
||
return ptr; | ||
} | ||
|
||
void | ||
qguiapplicationAddLibraryPath(QGuiApplication& app, const QString& path) | ||
{ | ||
app.addLibraryPath(path); | ||
} | ||
|
||
QString | ||
qguiapplicationApplicationName(const QGuiApplication& app) | ||
{ | ||
return app.applicationName(); | ||
} | ||
|
||
QString | ||
qguiapplicationApplicationVersion(const QGuiApplication& app) | ||
{ | ||
return app.applicationVersion(); | ||
} | ||
|
||
::std::int32_t | ||
qguiapplicationExec(QGuiApplication& app) | ||
{ | ||
return static_cast<::std::int32_t>(app.exec()); | ||
} | ||
|
||
QStringList | ||
qguiapplicationLibraryPaths(const QGuiApplication& app) | ||
{ | ||
return app.libraryPaths(); | ||
} | ||
|
||
QString | ||
qguiapplicationOrganizationDomain(const QGuiApplication& app) | ||
{ | ||
return app.organizationDomain(); | ||
} | ||
|
||
QString | ||
qguiapplicationOrganizationName(const QGuiApplication& app) | ||
{ | ||
return app.organizationName(); | ||
} | ||
|
||
void | ||
qguiapplicationSetApplicationName(QGuiApplication& app, const QString& name) | ||
{ | ||
app.setApplicationName(name); | ||
} | ||
|
||
void | ||
qguiapplicationSetApplicationVersion(QGuiApplication& app, | ||
const QString& version) | ||
{ | ||
app.setApplicationVersion(version); | ||
} | ||
|
||
void | ||
qguiapplicationSetLibraryPaths(QGuiApplication& app, const QStringList& paths) | ||
{ | ||
app.setLibraryPaths(paths); | ||
} | ||
|
||
void | ||
qguiapplicationSetOrganizationDomain(QGuiApplication& app, | ||
const QString& domain) | ||
{ | ||
app.setOrganizationDomain(domain); | ||
} | ||
|
||
void | ||
qguiapplicationSetOrganizationName(QGuiApplication& app, const QString& name) | ||
{ | ||
app.setOrganizationName(name); | ||
} | ||
|
||
} | ||
} | ||
#endif |
Oops, something went wrong.