File tree Expand file tree Collapse file tree 6 files changed +73
-0
lines changed
tests/qt_types_standalone Expand file tree Collapse file tree 6 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -104,6 +104,7 @@ add_executable(${APP_NAME}
104104 cpp/qstringlist.h
105105 cpp/qtime.h
106106 cpp/qtimezone.h
107+ cpp/qtlogging.h
107108 cpp/qurl.h
108109 cpp/qvariant.h
109110 cpp/qvector.h
Original file line number Diff line number Diff line change 4545#include " qstringlist.h"
4646#include " qtime.h"
4747#include " qtimezone.h"
48+ #include " qtlogging.h"
4849#include " qurl.h"
4950#include " qvariant.h"
5051#include " qvector.h"
@@ -97,6 +98,7 @@ main(int argc, char* argv[])
9798 runTest (QScopedPointer<QObject>(new QStringListTest));
9899 runTest (QScopedPointer<QObject>(new QTimeTest));
99100 runTest (QScopedPointer<QObject>(new QTimeZoneTest));
101+ runTest (QScopedPointer<QObject>(new QtLoggingTest));
100102 runTest (QScopedPointer<QObject>(new QUrlTest));
101103 runTest (QScopedPointer<QObject>(new QVariantTest));
102104 runTest (QScopedPointer<QObject>(new QVectorTest));
Original file line number Diff line number Diff line change 1+ // clang-format off
2+ // SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3+ // clang-format on
4+ // SPDX-FileContributor: Joshua Booth <joshua.n.booth@gmail.com>
5+ //
6+ // SPDX-License-Identifier: MIT OR Apache-2.0
7+ #pragma once
8+
9+ #include < QtCore/qlogging.h>
10+ #include < QtTest/QTest>
11+
12+ #include " qt_types_standalone/src/qtlogging.cxx.h"
13+
14+ static QtMessageHandler originalHandler = nullptr ;
15+ static QString loggedMessage{};
16+
17+ class QtLoggingTest : public QObject
18+ {
19+ Q_OBJECT
20+
21+ private:
22+ static void logAndStore (QtMsgType type,
23+ const QMessageLogContext& context,
24+ const QString& msg)
25+ {
26+ if (type == QtMsgType::QtInfoMsg) {
27+ loggedMessage = QStringLiteral (" %1:%2 - %3" )
28+ .arg (QString::fromUtf8 (context.file ))
29+ .arg (context.line )
30+ .arg (msg);
31+ }
32+ if (originalHandler) {
33+ (*originalHandler)(type, context, msg);
34+ }
35+ }
36+
37+ private Q_SLOTS:
38+ void log ()
39+ {
40+ originalHandler = qInstallMessageHandler (logAndStore);
41+ const QString expectedMessage = log_info (QStringLiteral (" test message" ));
42+ qInstallMessageHandler (originalHandler);
43+ QCOMPARE (loggedMessage, expectedMessage);
44+ }
45+ };
Original file line number Diff line number Diff line change @@ -44,6 +44,7 @@ fn main() {
4444 . file ( "src/qstringlist.rs" )
4545 . file ( "src/qtime.rs" )
4646 . file ( "src/qtimezone.rs" )
47+ . file ( "src/qtlogging.rs" )
4748 . file ( "src/qurl.rs" )
4849 . file ( "src/qvariant.rs" )
4950 . file ( "src/qvector.rs" )
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ mod qstring;
4141mod qstringlist;
4242mod qtime;
4343mod qtimezone;
44+ mod qtlogging;
4445mod qurl;
4546mod qvariant;
4647mod qvector;
Original file line number Diff line number Diff line change 1+ // SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2+ // SPDX-FileContributor: Joshua Booth <joshua.n.booth@gmail.com>
3+ //
4+ // SPDX-License-Identifier: MIT OR Apache-2.0
5+
6+ use cxx_qt_lib:: { q_info, QString } ;
7+
8+ #[ cxx:: bridge]
9+ mod ffi {
10+ extern "C++" {
11+ include ! ( "cxx-qt-lib/qstring.h" ) ;
12+ type QString = cxx_qt_lib:: QString ;
13+ }
14+
15+ extern "Rust" {
16+ fn log_info ( message : & QString ) -> QString ;
17+ }
18+ }
19+
20+ fn log_info ( message : & QString ) -> QString {
21+ q_info ! ( "Message: {message}" ) ;
22+ QString :: from ( & format ! ( "{}:{} - Message: {message}" , file!( ) , line!( ) - 1 ) )
23+ }
You can’t perform that action at this time.
0 commit comments