×
Namespaces

Variants
Actions
Revision as of 13:22, 12 September 2009 by fasttrack (Talk | contribs)

How to use QString in Qt

From Nokia Developer Wiki
Jump to: navigation, search
{{{width}}}
Article Metadata
Tested with
Devices(s): Emulator & creator IDE V4.5
Compatibility
Platform(s): S60 3rd Edition, S60 5th Edition
Symbian
Article
Keywords: QString
Created: (31 Dec 2008)
Last edited: fasttrack (12 Sep 2009)

Contents

Introduction

This example demonstrate some basic string function in Qt for Symbian.


Download Links

Note:Some of image are from qt Creator IDE V4.5 and the code associated with image can also be executed in carbide c++ 2.0

Various Function and Operators

  • If you want to append a certain number of identical characters to the string, use operator+=()
QString str = "Hello";
str += QString(10, 'X');

The final content of str is "Hello" followed by ten "X": "HelloXXXXXXXXXX".

  • Prepends the string str to the beginning of this string and returns a reference to this string.
QString str = "world";
str.prepend("Hello ");

The content of str is "Hello world".

  • Returns a lowercase copy of the string.
QString str = "HELLO";
QString lowerCase = str.toLower();

The content of lowerCase is "hello". The content of str is still "HELLO", the method QString::toLower() does not modify the string, it returns a lowercase version of the string.

Code snippet using QString

The following example show the use of QString in a graphical application. QLabel is a widget capable of displaying text or images.

#include <QApplication>
#include <QLabel>
#include <QString>
#include <QVBoxLayout>
 
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
 
QString str("world");
QString sizeOfWorld = QString::number(str.size());
 
// resize the string and removes last two characters
str.resize(3); // str = "wor"
 
// Add "Hello " at the beginning
str.prepend("Hello "); // str = "Hello wor"
 
// Add "ld" at the end
str.append("ld"); // str = "Hello world"
 
// Remove 6 characters at position five, and insert " India" at this position
str.replace(5, 6, " India");
 
QLabel *label1 = new QLabel(str);
QLabel *label2 = new QLabel(sizeOfWorld);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label1);
layout->addWidget(label2);
 
QWidget window;
window.setLayout(layout);
window.show();
return app.exec();
}

Here is the result of this code on Microsoft Windows:

String2.JPG

Related links:

449 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×