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
24
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
  • 11,766
  • 26
  • 82
  • 146
24
votes
2 answers

QString:number with maximum 2 decimal places without trailing zero

I have a division like this: number / 1000.0 Sometimes it gives answers like 96.0000000001, sometimes the division works as expected. I want to limit my number to a maximum of two decimal places and without trailing zeros. If it's 96.5500000001 it…
24
votes
4 answers

read QProcess output to string

I have a code that uses QProcess like this. int main(int argc, char *argv[]) { int status=0; QProcess pingProcess; QString ba; QString exec = "snmpget"; QStringList params; params << "-v" << "2c" << "-c" << "public" <<…
sersem1
  • 446
  • 1
  • 3
  • 10
22
votes
4 answers

What is the equivalence for QString::arg() in QML

I'm wondering How I can have a string in QML that will be occupied with some arguments? Some thing like this in Qt: QString str("%1 %2"); str = str.arg("Number").arg(12);//str = "Number 12"
s4eed
  • 6,033
  • 7
  • 51
  • 91
22
votes
4 answers

QString Splitting

I have these url strings file:///home/we/Pictures/neededWord/3193_n.jpg file:///home/smes/Pictures/neededWord/jds_22.png file:///home/seede/kkske/Pictures/neededWord/3193_n.jpg I want to extract the "neededWord" from each of them. As it appears from…
Wazery
  • 13,971
  • 15
  • 55
  • 91
21
votes
4 answers

Why is QString printed with quotation marks?

So when you use qDebug() to print a QString, quotation marks appears suddenly in the output. int main() { QString str = "hello world"; //Classic qDebug() << str; //Output: "hello world" //Expected Ouput: hello world } I know we can…
Michael
  • 873
  • 2
  • 11
  • 26
19
votes
2 answers

how to initialize a QString to null?

What is the difference between QString::number(0) and ((const char*) 0)? I want to initialize a QString say phoneNumber to null. Will phoneNumber(QString::number(0)) and phoneNumber((const char*) 0) both work?
user1065969
  • 519
  • 4
  • 9
  • 18
19
votes
2 answers

ImportError: cannot import name 'QStringList' in PyQt5

I am using PyQt5 but can't import QStringList. I know that QStringList used to be in the module QtCore in PyQt4. So I try importing the class using from PyQt5.QtCore import QStringList but it shows this error C:\Python34\python.exe…
Sнаđошƒаӽ
  • 13,406
  • 11
  • 67
  • 83
19
votes
1 answer

Convert const char* to QString

I have to use the output of a function of a type const char* and I need to convert it to QString. Note: inside that function, these are lines of code to return the const char* char* ClassA::getData() const{ return const_cast
Mahmoud Hassan
  • 572
  • 1
  • 3
  • 13
19
votes
2 answers

How to get QString from QListView selected item in Qt?

I need to get the selected item name in QListView as a QString. I have tried to google, but I haven't found anything useful.
MartinS
  • 711
  • 2
  • 12
  • 27
18
votes
4 answers

Concatenating two QStrings with an integer

I want to do something like this in C++ using Qt: int i = 5; QString directory = ":/karim/pic" + i + ".jpg"; where + means I want to concatenate the strings and the integer (that is, directory should be :/karim/pic5.jpg). How can I do this?
Karim M. El Tel
  • 428
  • 1
  • 7
  • 19
18
votes
4 answers

Operator << for QString

It makes sense to implement << for QString like: std::ostream& operator <<(std::ostream &stream,const QString &str) { stream << str.toAscii().constData(); //or: stream << str.toStdString(); //?? return stream; } instead of writing stream <<…
Ilya Kobelevskiy
  • 4,987
  • 4
  • 20
  • 40
18
votes
3 answers

Best way to convert std::wstring to QString

I'm currently working on a larger project, where the "logic" is implemented in standard C++ with all strings being handled with std::wstring and the UI part is implemented using Qt and thus necessarily QString (Bonus question: is this true?). What…
mort
  • 10,940
  • 14
  • 45
  • 92
17
votes
5 answers

How to convert a pointer value to QString?

for debug purposes I often output pointer values (mostly this) to qDebug: qDebug("pointer of current object = 0x%08x",this);, using "%08x" as format string and simply passing this as a parameter. How can I convert the pointer value to a…
Martin Hennings
  • 14,732
  • 8
  • 38
  • 65
17
votes
2 answers

Draw rich text with QPainter

is there a way to draw fixed text that has subscripts. My goal is to have something like: "K_max=K_2 . 3" QString equation="K_max=K_2 . 3"; painter.drawText( QRect(x, y , width, y+height), Qt::AlignLeft|Qt::AlignVCenter, equation); I also tried…
luffy
  • 1,766
  • 1
  • 16
  • 22
1
2
3
49 50