Jak przekonwertować int na QString?

Czy istnieje funkcja QString, która pobiera int i wyświetla ją jako QString?

Author: Nejat, 2010-07-09

8 answers

Użycie QString::number():

int i = 42;
QString s = QString::number(i);
 697
Author: Georg Fritzsche,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-17 07:38:21

I jeśli chcesz umieścić go w łańcuchu w jakimś kontekście tekstowym, zapomnij o operatorze +. Po prostu zrób:

// Qt 5 + C++11
auto i = 13;    
auto printable = QStringLiteral("My magic number is %1. That's all!").arg(i);

// Qt 5
int i = 13;    
QString printable = QStringLiteral("My magic number is %1. That's all!").arg(i);

// Qt 4
int i = 13;    
QString printable = QString::fromLatin1("My magic number is %1. That's all!").arg(i);
 123
Author: Kamil Klimek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-07 20:10:13

Ponadto, aby przekonwertować cokolwiek chcesz, możesz użyć QVariant. Dla an int do a QString otrzymujesz:

QVariant(3).toString();

A float do a string lub a string do a float:

QVariant(3.2).toString();
QVariant("5.2").toFloat();
 28
Author: Gabriel de Grimouard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-18 15:16:23

Kolejną opcją jest użycie QTextStream i operatora << w taki sam sposób, jak w C++:

QPoint point(5,1);
QString str;
QTextStream(&str) << "Mouse click: (" << point.x() << ", " << point.y() << ").";

// OUTPUT:
// Mouse click: (5, 1).

Ponieważ operator <<() został przeciążony, można go używać dla wielu typów, nie tylko int. QString::arg() jest przeciążone, na przykład arg(int a1, int a2), ale nie ma arg(int a1, QString a2), więc używanie QTextStream() i operatora << jest wygodne przy formatowaniu dłuższych łańcuchów z typami mieszanymi.

Uwaga: możesz pokusić się o użycie sprintf() obiektu do imitować polecenia w stylu C printf(), ale zaleca się stosowanie QTextStream lub arg(), ponieważ obsługują one Unicode strings.

 17
Author: Matthew Kraus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-28 21:09:07

Zawsze używam QString::setNum().

int i = 10;
double d = 10.75;
QString str;
str.setNum(i);
str.setNum(d);

setNum() jest przeciążony na wiele sposobów. Zobacz QString odniesienie do klasy.

 15
Author: Narek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-28 21:36:48

W najprostszej formie użyj odpowiedzi Georg Fritzsche

Dla nieco zaawansowanych, możesz użyć tego,

QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const

Pobierz dokumentację i przykład tutaj ..

 12
Author: liaK,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-30 14:08:42

Jeśli potrzebujesz formatowania liczb z uwzględnieniem ustawień regionalnych, użyj zamiast tego metody QLocale:: toString.

 4
Author: André,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-19 17:03:53

Tylko dla kompletności, możesz użyć biblioteki standardowej i wykonać QString qstr = QString::fromStdString(std::to_string(42));

 3
Author: Morgan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-12 05:06:07