Questions tagged [qstring]

A QString is a class in Qt library which implements character strings.

QString holds a unicode string inside, and provides a set of useful functions to manipulate the data, including easy concatenation, trimming, search and replace, and conversion functions.

QString can also be converted to std::string and vice-versa with toStdString() and fromStdString() respectfully:

QString str = QString::fromStdString( std::string("Hello, World!") );

std::string str = QString("Hello world!").toStdString();

The official Qt documentation can be found here for Qt 4.8 and here for Qt 5.

744 questions
249
votes
10 answers

How to convert QString to std::string?

I am trying to do something like this: QString string; // do things... std::cout << string << std::endl; but the code doesn't compile. How to output the content of qstring into the console (e.g. for debugging purposes or other reasons)? How to…
augustin
  • 13,003
  • 11
  • 55
  • 77
162
votes
7 answers

How to change string into QString?

What is the most basic way to do it?
NeverAgain
  • 1,743
  • 3
  • 13
  • 9
106
votes
10 answers

QString to char* conversion

I was trying to convert a QString to char* type by the following methods, but they don't seem to work. //QLineEdit *line=new QLineEdit();{just to describe what is line here} QString temp=line->text(); char *str=(char *)malloc(10); QByteArray…
mawia
  • 8,513
  • 13
  • 45
  • 57
100
votes
6 answers

Convert an int to a QString with zero padding (leading zeroes)

I want to "stringify" a number and add zero-padding, like how printf("%05d") would add leading zeros if the number is less than 5 digits.
elcuco
  • 8,321
  • 9
  • 42
  • 63
84
votes
2 answers

Qt. get part of QString

I want to get QString from another QString, when I know necessary indexes. For example: Main string: "This is a string". I want to create new QString from first 5 symbols and get "This ". input : first and last char number. output : new QString. How…
AlekseyS
  • 841
  • 1
  • 6
  • 4
82
votes
4 answers

Convert std::string to QString

I've got an std::string content that I know contains UTF-8 data. I want to convert it to a QString. How do I do that, avoiding the from-ASCII conversion in Qt?
Fred Foo
  • 328,932
  • 68
  • 689
  • 800
66
votes
7 answers

How to convert QString to int?

I have a QString in my sources. So I need to convert it to integer without "Kb". I tried Abcd.toInt() but it does not work. QString Abcd = "123.5 Kb"
user2398614
  • 661
  • 1
  • 5
  • 3
49
votes
7 answers

QByteArray to QString

I'm having issues with QByteArray and QString. I'm reading a file and stores its information in a QByteArray. The file is in unicode, so it contains something like: t\0 e\0 s\0 t\0 \0 \0 I'm trying to compare this value to my specified value, but it…
Nika
  • 1,694
  • 3
  • 21
  • 40
34
votes
3 answers

How to deal with "%1" in the argument of QString::arg()?

Everybody loves QString("Put something here %1 and here %2") .arg(replacement1) .arg(replacement2); but things get itchy as soon as you have the faintest chance that replacement1 actually contains %1 or even %2 anywhere. Then, the second…
Tilman Vogel
  • 8,221
  • 4
  • 29
  • 30
34
votes
3 answers

How to convert a QJsonObject to QString

I have a QJsonObject data and want to convert to QString. How can I do this? Searched for help in Qt, it only can convert QJsonObject to QVariantMap... Thanks in advance.
gogo000
  • 363
  • 1
  • 3
  • 7
31
votes
14 answers

switch/case statement in C++ with a QString type

I want to use switch-case in my program but the compiler gives me this error: switch expression of type 'QString' is illegal How can I use the switch statement with a QString? My code is as follows: bool isStopWord( QString word ) { bool flag =…
amiref
  • 2,565
  • 5
  • 27
  • 43
31
votes
4 answers

Qt C++ QString to QByteArray Conversion

I have created an encrypt/decrypt program, when encrypting I store the encrypted QByteArray in a text file. When trying to decrypt I retrieved it and then put it into the decryption method, the problem is that I need a way to convert it to…
James
  • 658
  • 1
  • 6
  • 13
31
votes
3 answers

How to Compare two Qstrings?

I have to compare two Qstrings in qt, say, Qstring str1="1005",str2="1006"; I have tried using , if(str1==str2){ return true; } & if(str1.compare(str2)==0) { return true; } still both methods goes inside if condition & returns true.
krohit
  • 709
  • 1
  • 6
  • 26
29
votes
6 answers

How to print string literal and QString with qDebug?

Is there any easy way to get the following work? I mean is there any helper class in Qt which prepares the string for qDebug? QString s = "value"; qDebug("abc" + s + "def");
B Faley
  • 14,391
  • 34
  • 113
  • 191
26
votes
4 answers

How to specify a unicode character using QString?

How can I specify a unicode character by code (such as "4FF0") using QString? I tried QString s("\u4FF0"); but it only outputs a question mark. Any idea how to do this? Edit: It works that way, but is there a more direct way? std::wstring str =…
laurent
  • 79,308
  • 64
  • 256
  • 389
1
2 3
49 50