Skip to content

Commit cdc5ef9

Browse files
authored
qtypes: add support for Qt alias types that don't match (#904)
* qtypes: add support for Qt alias types that don't match Some times don't match the Rust types so add these missing types. Closes #882 * cxx-qt-lib: use qint64 type for QDate and QDateTime * cxx-qt-lib: use quintptr for QModelIndex
1 parent a98b1b8 commit cdc5ef9

File tree

16 files changed

+304
-250
lines changed

16 files changed

+304
-250
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- `QDateTime::from_string` to parse `QDateTime` from a `QString`.
2323
- Support for further types: `QUuid`
2424
- New example: Basic greeter app
25+
- Support for further types: `qreal`, `qint64`, `qintptr`, `qsizetype`, `quint64`, `quintptr`
2526

2627
### Fixed
2728

crates/cxx-qt-lib/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ fn main() {
176176
"core/qstringlist",
177177
"core/qt",
178178
"core/qtime",
179+
"core/qtypes",
179180
"core/qurl",
180181
"core/quuid",
181182
"core/qvariant/mod",
@@ -303,6 +304,7 @@ fn main() {
303304
"core/qstring",
304305
"core/qstringlist",
305306
"core/qtime",
307+
"core/qtypes",
306308
"core/qurl",
307309
"core/quuid",
308310
"core/qvariant/qvariant",

crates/cxx-qt-lib/include/core/qdate.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
namespace rust {
1616
namespace cxxqtlib1 {
1717

18-
QDate
19-
qdateAddDays(const QDate& date, ::std::int64_t ndays);
2018
QDate
2119
qdateCurrentDate();
2220
QDate
2321
qdateFromString(const QString& string, const QString& format);
2422
QDate
2523
qdateFromString(const QString& string, Qt::DateFormat format);
2624
// In Qt 5 d is const-ref, in Qt 6 it is value
27-
::std::int64_t
25+
qint64
2826
qdateDaysTo(const QDate& date, QDate d);
2927
bool
3028
qdateIsLeapYear(::std::int32_t year);

crates/cxx-qt-lib/include/core/qdatetime.h

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,24 @@ struct IsRelocatable<QDateTime> : ::std::true_type
2828
namespace rust {
2929
namespace cxxqtlib1 {
3030

31-
QDateTime
32-
qdatetimeAddDays(const QDateTime& datetime, ::std::int64_t ndays);
33-
QDateTime
34-
qdatetimeAddMSecs(const QDateTime& datetime, ::std::int64_t msecs);
35-
QDateTime
36-
qdatetimeAddSecs(const QDateTime& datetime, ::std::int64_t secs);
3731
QDateTime
3832
qdatetimeCurrentDateTime();
3933
QDateTime
4034
qdatetimeCurrentDateTimeUtc();
41-
::std::int64_t
35+
qint64
4236
qdatetimeCurrentMSecsSinceEpoch();
43-
::std::int64_t
37+
qint64
4438
qdatetimeCurrentSecsSinceEpoch();
45-
::std::int64_t
46-
qdatetimeDaysTo(const QDateTime& datetime, const QDateTime& other);
4739
QDateTime
48-
qdatetimeFromMSecsSinceEpoch(::std::int64_t msecs, const QTimeZone& timeZone);
40+
qdatetimeFromMSecsSinceEpoch(qint64 msecs, const QTimeZone& timeZone);
4941
QDateTime
50-
qdatetimeFromSecsSinceEpoch(::std::int64_t secs, const QTimeZone& timeZone);
51-
::std::int64_t
52-
qdatetimeMSecsTo(const QDateTime& datetime, const QDateTime& other);
53-
::std::int64_t
54-
qdatetimeSecsTo(const QDateTime& datetime, const QDateTime& other);
42+
qdatetimeFromSecsSinceEpoch(qint64 secs, const QTimeZone& timeZone);
5543
void
5644
qdatetimeSetDate(QDateTime& datetime, QDate date);
5745
void
58-
qdatetimeSetMSecsSinceEpoch(QDateTime& datetime, ::std::int64_t msecs);
59-
void
60-
qdatetimeSetSecsSinceEpoch(QDateTime& datetime, ::std::int64_t secs);
61-
void
6246
qdatetimeSetTime(QDateTime& datetime, QTime time);
6347
::std::unique_ptr<QTimeZone>
6448
qdatetimeTimeZone(const QDateTime& datetime);
65-
::std::int64_t
66-
qdatetimeToMSecsSinceEpoch(const QDateTime& datetime);
67-
::std::int64_t
68-
qdatetimeToSecsSinceEpoch(const QDateTime& datetime);
6949
void
7050
qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone);
7151
QDateTime

crates/cxx-qt-lib/include/core/qmodelindex.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,3 @@
99
#include <cinttypes>
1010

1111
#include <QtCore/QModelIndex>
12-
13-
namespace rust {
14-
namespace cxxqtlib1 {
15-
16-
::std::size_t
17-
qmodelindexInternalId(const QModelIndex& index);
18-
19-
}
20-
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// clang-format off
2+
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
3+
// clang-format on
4+
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
5+
//
6+
// SPDX-License-Identifier: MIT OR Apache-2.0
7+
#pragma once
8+
9+
#include <QtCore/Qt>

crates/cxx-qt-lib/src/core/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pub use qt::{
8383
mod qtime;
8484
pub use qtime::QTime;
8585

86+
mod qtypes;
87+
pub use qtypes::{qint64, qintptr, qreal, qsizetype, quint64, quintptr};
88+
8689
#[cfg(not(target_os = "emscripten"))]
8790
mod qtimezone;
8891
#[cfg(not(target_os = "emscripten"))]

crates/cxx-qt-lib/src/core/qdate.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,17 @@ static_assert(::std::is_trivially_copyable<QDate>::value,
2121
namespace rust {
2222
namespace cxxqtlib1 {
2323

24-
QDate
25-
qdateAddDays(const QDate& date, ::std::int64_t ndays)
26-
{
27-
return date.addDays(static_cast<qint64>(ndays));
28-
}
29-
3024
QDate
3125
qdateCurrentDate()
3226
{
3327
return QDate::currentDate();
3428
}
3529

36-
::std::int64_t
30+
qint64
3731
qdateDaysTo(const QDate& date, QDate d)
3832
{
3933
// In Qt 5 d is const-ref, in Qt 6 it is value
40-
return static_cast<::std::int64_t>(date.daysTo(d));
34+
return date.daysTo(d);
4135
}
4236

4337
QDate

crates/cxx-qt-lib/src/core/qdate.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,24 @@ mod ffi {
1414
type DateFormat = crate::DateFormat;
1515
}
1616

17+
unsafe extern "C++" {
18+
include!("cxx-qt-lib/qtypes.h");
19+
type qint64 = crate::qint64;
20+
}
21+
1722
unsafe extern "C++" {
1823
include!("cxx-qt-lib/qstring.h");
1924
type QString = crate::QString;
2025

2126
include!("cxx-qt-lib/qdate.h");
2227
type QDate = super::QDate;
2328

29+
/// Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative).
30+
///
31+
/// Returns a null date if the current date is invalid or the new date is out of range.
32+
#[rust_name = "add_days"]
33+
fn addDays(self: &QDate, ndays: qint64) -> QDate;
34+
2435
/// Returns a QDate object containing a date nmonths later than the date of this object (or earlier if nmonths is negative).
2536
#[rust_name = "add_months"]
2637
fn addMonths(self: &QDate, nmonths: i32) -> QDate;
@@ -76,17 +87,13 @@ mod ffi {
7687

7788
#[namespace = "rust::cxxqtlib1"]
7889
unsafe extern "C++" {
79-
#[doc(hidden)]
80-
#[rust_name = "qdate_add_days"]
81-
fn qdateAddDays(date: &QDate, ndays: i64) -> QDate;
82-
8390
#[doc(hidden)]
8491
#[rust_name = "qdate_current_date"]
8592
fn qdateCurrentDate() -> QDate;
8693

8794
#[doc(hidden)]
8895
#[rust_name = "qdate_days_to"]
89-
fn qdateDaysTo(date: &QDate, d: QDate) -> i64;
96+
fn qdateDaysTo(date: &QDate, d: QDate) -> qint64;
9097

9198
#[doc(hidden)]
9299
#[rust_name = "qdate_from_string"]
@@ -147,13 +154,6 @@ impl fmt::Debug for QDate {
147154
}
148155

149156
impl QDate {
150-
/// Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative).
151-
///
152-
/// Returns a null date if the current date is invalid or the new date is out of range.
153-
pub fn add_days(&self, ndays: i64) -> Self {
154-
ffi::qdate_add_days(self, ndays)
155-
}
156-
157157
/// Returns the current date, as reported by the system clock.
158158
pub fn current_date() -> Self {
159159
ffi::qdate_current_date()
@@ -162,7 +162,7 @@ impl QDate {
162162
/// Returns the number of days from this date to d (which is negative if d is earlier than this date).
163163
///
164164
/// Returns 0 if either date is invalid.
165-
pub fn days_to(&self, date: Self) -> i64 {
165+
pub fn days_to(&self, date: Self) -> ffi::qint64 {
166166
ffi::qdate_days_to(self, date)
167167
}
168168

@@ -271,15 +271,15 @@ mod test {
271271
#[test]
272272
fn qdate_current_date() {
273273
let date_a = QDate::current_date();
274-
let date_b = date_a.add_days(100);
275-
assert_eq!(date_a.days_to(date_b), 100);
274+
let date_b = date_a.add_days(100.into());
275+
assert_eq!(i64::from(date_a.days_to(date_b)), 100);
276276
}
277277

278278
#[test]
279279
fn qdate_julian_day() {
280280
let date_a = QDate::from_julian_day(1000);
281281
let date_b = QDate::from_julian_day(1010);
282-
assert_eq!(date_a.days_to(date_b), 10);
282+
assert_eq!(i64::from(date_a.days_to(date_b)), 10);
283283
}
284284

285285
#[cfg(feature = "chrono")]

crates/cxx-qt-lib/src/core/qdatetime.cpp

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,6 @@ static_assert(QTypeInfo<QDateTime>::isRelocatable);
2727
namespace rust {
2828
namespace cxxqtlib1 {
2929

30-
QDateTime
31-
qdatetimeAddDays(const QDateTime& datetime, ::std::int64_t ndays)
32-
{
33-
return datetime.addDays(static_cast<qint64>(ndays));
34-
}
35-
36-
QDateTime
37-
qdatetimeAddMSecs(const QDateTime& datetime, ::std::int64_t msecs)
38-
{
39-
return datetime.addMSecs(static_cast<qint64>(msecs));
40-
}
41-
42-
QDateTime
43-
qdatetimeAddSecs(const QDateTime& datetime, ::std::int64_t secs)
44-
{
45-
return datetime.addSecs(static_cast<qint64>(secs));
46-
}
47-
4830
QDateTime
4931
qdatetimeCurrentDateTime()
5032
{
@@ -57,48 +39,28 @@ qdatetimeCurrentDateTimeUtc()
5739
return QDateTime::currentDateTimeUtc();
5840
}
5941

60-
::std::int64_t
42+
qint64
6143
qdatetimeCurrentMSecsSinceEpoch()
6244
{
6345
return QDateTime::currentMSecsSinceEpoch();
6446
}
6547

66-
::std::int64_t
48+
qint64
6749
qdatetimeCurrentSecsSinceEpoch()
6850
{
6951
return QDateTime::currentSecsSinceEpoch();
7052
}
7153

72-
::std::int64_t
73-
qdatetimeDaysTo(const QDateTime& datetime, const QDateTime& other)
74-
{
75-
return static_cast<::std::int64_t>(datetime.daysTo(other));
76-
}
77-
7854
QDateTime
79-
qdatetimeFromMSecsSinceEpoch(::std::int64_t msecs, const QTimeZone& timeZone)
55+
qdatetimeFromMSecsSinceEpoch(qint64 msecs, const QTimeZone& timeZone)
8056
{
81-
return QDateTime::fromMSecsSinceEpoch(static_cast<qint64>(msecs), timeZone);
57+
return QDateTime::fromMSecsSinceEpoch(msecs, timeZone);
8258
}
8359

8460
QDateTime
85-
qdatetimeFromSecsSinceEpoch(::std::int64_t secs, const QTimeZone& timeZone)
61+
qdatetimeFromSecsSinceEpoch(qint64 secs, const QTimeZone& timeZone)
8662
{
87-
return QDateTime::fromSecsSinceEpoch(static_cast<qint64>(secs), timeZone);
88-
}
89-
90-
::std::int64_t
91-
qdatetimeMSecsTo(const QDateTime& datetime, const QDateTime& other)
92-
{
93-
94-
return static_cast<::std::int64_t>(datetime.msecsTo(other));
95-
}
96-
97-
::std::int64_t
98-
qdatetimeSecsTo(const QDateTime& datetime, const QDateTime& other)
99-
{
100-
101-
return static_cast<::std::int64_t>(datetime.secsTo(other));
63+
return QDateTime::fromSecsSinceEpoch(secs, timeZone);
10264
}
10365

10466
void
@@ -107,18 +69,6 @@ qdatetimeSetDate(QDateTime& datetime, QDate date)
10769
datetime.setDate(date);
10870
}
10971

110-
void
111-
qdatetimeSetMSecsSinceEpoch(QDateTime& datetime, ::std::int64_t msecs)
112-
{
113-
datetime.setMSecsSinceEpoch(static_cast<qint64>(msecs));
114-
}
115-
116-
void
117-
qdatetimeSetSecsSinceEpoch(QDateTime& datetime, ::std::int64_t secs)
118-
{
119-
datetime.setSecsSinceEpoch(static_cast<qint64>(secs));
120-
}
121-
12272
void
12373
qdatetimeSetTime(QDateTime& datetime, QTime time)
12474
{
@@ -131,18 +81,6 @@ qdatetimeTimeZone(const QDateTime& datetime)
13181
return ::std::make_unique<QTimeZone>(datetime.timeZone());
13282
}
13383

134-
::std::int64_t
135-
qdatetimeToMSecsSinceEpoch(const QDateTime& datetime)
136-
{
137-
return static_cast<::std::int64_t>(datetime.toMSecsSinceEpoch());
138-
}
139-
140-
::std::int64_t
141-
qdatetimeToSecsSinceEpoch(const QDateTime& datetime)
142-
{
143-
return static_cast<::std::int64_t>(datetime.toSecsSinceEpoch());
144-
}
145-
14684
void
14785
qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone)
14886
{

0 commit comments

Comments
 (0)