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
9
votes
2 answers

efficiently compare QString and std::string for equality

I want to efficiently compare a QString and a std::string for (in)equality. Which is the best way to do it, possibly without creating intermediate objects?
Ber
  • 40,356
  • 16
  • 72
  • 88
9
votes
2 answers

How to make a QString from a QTextStream?

Will this work? QString bozo; QFile filevar("sometextfile.txt"); QTextStream in(&filevar); while(!in.atEnd()) { QString line = in.readLine(); bozo = bozo + line; } filevar.close(); Will bozo be the entirety of sometextfile.txt?
Dave
  • 477
  • 2
  • 5
  • 18
9
votes
1 answer

How to highlight a string of text within a QTextEdit

I'm a student programmer currently developing an application for work using Qt4. I am building an equation editor and I'm having issues attempting to highlight a string within my QTextEdit field. I have a function that parses through the QTextEdit…
Wylie Coyote SG.
  • 1,009
  • 6
  • 22
  • 37
9
votes
1 answer

QString::toDouble() giving me double with wrong precision

I have a QString myNumber containing "09338.712001". When I do: myNumber.toDouble();, it returns 9338.71, but I want the double to be the original value, which is 09338.712001. Does anyone know how to get the double returned by toDouble to have the…
user446836
  • 733
  • 4
  • 16
  • 24
8
votes
1 answer

How to get substring from a string in qt?

I have a text form: Last Name:SomeName, Day:23 ...etc From Last Name:SomeName, I would like to get Last Name, and separately SomeName. I have tried to use QRegularExpression, QRegularExpression re("(?
amol01
  • 1,823
  • 4
  • 21
  • 35
8
votes
1 answer

What's the difference between QString[] and QStringList

What's the difference between an object of QStringList and an Array of QStrings? I mean it seems that both behave as an array.
Nixmd
  • 775
  • 5
  • 11
  • 20
8
votes
2 answers

C++ Qt return empty QString

I made a function which returns a QString. At some points in my function it should return an empty QString. Just returning "" doesn't work. When I use QString::isEmpty() it's not. My "emergency plan" was to return an "empty" string and check with it…
8
votes
4 answers

Char to QString

I have a char array and want to convert one of the values from char to qstring: unsigned char inBuffer[64]; .... QString str= QString(*inBuffer[1]); ui->counter->setText(str); This isn't working (I get a compiler error). Any suggestions?
moesef
  • 4,641
  • 16
  • 51
  • 68
7
votes
3 answers

Appending number to QString with arg() , is there better ways?

I've been using QString::number () to convert numbers to string for long time , now i'm wondering if there's something better than following: int i = 0; QString msg = QString ("Loading %1").arg (QString::number (i)); How can i spare…
daisy
  • 22,498
  • 29
  • 129
  • 265
7
votes
2 answers

How to use QStringView (or QStringRef) in Qt widgets?

I'm trying to replace heavy QString class with the lighter alternatives (QStringRef/QStringView). The problem is that Qt widgets don't consume these classes by any mean. For example, the QLabel::setText method requires const QString& as an input…
Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40
7
votes
1 answer

Best way to initialize QString

What is the best way to initialize QString: QString name = "name"; // or QString nameL = QStringLiteral("name"); // or QString nameLL = QLatin1String("name"); // or something else...
svadym
  • 167
  • 1
  • 10
7
votes
3 answers

Change the Color and Font of QString or QLineEdit

How can I change the color and font of QLineEdit? Here is my code: self.lineEdit = QtGui.QLineEdit(widget) self.lineEdit.setText("enter keywords here") #I want this to be in italics and in brown color The setText line from Documentation says the…
learncode
  • 1,105
  • 4
  • 18
  • 36
7
votes
5 answers

In Qt; what is the best method to capitalise the first letter of every word in a QString?

I am thinking of regular expressions, but that is not exactly readable. There are also functions like s.toUpper() to consider, and probably other things as well. So what is the best method for capitalising the first letter of words in a QString?
Anon
  • 2,267
  • 3
  • 34
  • 51
7
votes
1 answer

Length of Utf-32 character in Qt

I'm using Qt5. I have a QString holding one character U"\x1D4CC" () that is longer than 16 bits. Even though this is only one character, Qt returns that size of this string is 2. Is there any way to display how many real characters a QString has…
user1781713
  • 177
  • 7
7
votes
2 answers

Convert jstring to QString

I'm calling a Java function that returns a string: QAndroidJniObject obj = QAndroidJniObject::callStaticObjectMethod("HelloJava", "getString"); jstring jstr = obj.object(); QString str = jstr; // This doesn't work, obviously,…
sashoalm
  • 75,001
  • 122
  • 434
  • 781