Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Application Development with Qt Creator - Second Edition
  • Toc
  • feedback
Application Development with Qt Creator - Second Edition

Application Development with Qt Creator - Second Edition

4 (25)
close
Application Development with Qt Creator - Second Edition

Application Development with Qt Creator - Second Edition

4 (25)

Overview of this book

This book is great for developers who are new to Qt and Qt Creator and who are interested in harnessing the power of Qt for cross-platform development. If you have basic experience programming in C++, you have what it takes to create engaging cross-platform applications using Qt and Qt Creator!
Table of Contents (15 chapters)
close
14
Index

Localizing special parameters – currencies and dates with QLocale


A common thing you might need to do is localize currencies and dates. Qt makes this easy, although the solution isn't obvious until you've thought about it a bit.

First, you need to know about the QString arg method. It replaces an escaped number with the formatted version of its argument; if we write:

QString s = QString("%1 %2").arg("a").arg("b");

Then, s contains the string "a b". Second, you need to know about the toString method of QLocale which formats its argument in a locale-specific way.

So, we could write:

QString currencyValue = QString("%1 %2")
    .arg(tr("$")).arg(QLocale::toString(value, 'g', 2)

This uses tr to localize the currency symbol and the QLocale class's static method, toString, to convert the value of the currency to a string with the locale-specific decimal separator (a period in the US and Canada, and a comma in Europe).

Date formatting is similar; the toString method of QLocale has overloads for the QDateTime...

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete