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.

791 questions
289
votes
12 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
  • 14,373
  • 13
  • 66
  • 79
192
votes
7 answers

How to change string into QString?

What is the most basic way to do it?
NeverAgain
  • 2,053
  • 3
  • 15
  • 10
124
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
  • 9,169
  • 14
  • 48
  • 57
112
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,948
  • 9
  • 47
  • 69
99
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
  • 355,277
  • 75
  • 744
  • 836
92
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
  • 921
  • 1
  • 6
  • 4
80
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
  • 801
  • 1
  • 6
  • 3
54
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,864
  • 3
  • 23
  • 44
46
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
  • 483
  • 1
  • 4
  • 8
41
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
  • 9,337
  • 4
  • 33
  • 32
39
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
  • 3,181
  • 7
  • 38
  • 62
38
votes
5 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
  • 773
  • 1
  • 6
  • 15
36
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
  • 792
  • 1
  • 7
  • 26
34
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
  • 17,120
  • 43
  • 133
  • 223
28
votes
4 answers

Removing whitespaces inside a string

I have a string lots\t of\nwhitespace\r\n which I have simplified but I still need to get rid of the other spaces in the string. QString str = " lots\t of\nwhitespace\r\n "; str = str.simplified(); I can do this erase_all(str, " "); in boost but…
Gandalf
  • 1
  • 29
  • 94
  • 165
1
2 3
52 53