Namespaces
Variants
Actions
Revision as of 04:13, 11 October 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:Como usar um QString

Jump to: navigation, search
Archived.png
Aquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.

Este exemplo demonstra, algumas funções básicas para strings em Qt.

Dados do artigo

Testado com
SDK: Qt 4.5
Aparelho(s): Emulator & creator IDE V4.5

Compatibilidade
Plataforma(s): S60 3rd Edition, S60 5th Edition

Artigo
Palavras-chave: QString
Tradução:
Última alteração feita por hamishwillee em 11 Oct 2012

Funcionalidades e operadores

  • Se você quiser adicionar um certo número de caracteres idênticos ao string, use os operadores +=(). Por exemplo:
QString str = "Hello";
str += QString(10, 'X');

O conteùdo final do QString str é "Hello" seguido pelos dez "X": "HelloXXXXXXXXXX".

  • Adiciona a string Hello ao inicio da string e retorna a referência para esse string.
 QString str = "world";
str.prepend("Hello ");

O conteúdo de str é "Hello world".

  • Retorna uma cópia de um string em letras minúsculas.
QString str = "HELLO";
QString lowerCase = str.toLower();

O conteúdo de str ainda é "HELLO", o método QString::toLower() não modifica a string, ele apenas retorna uma cópia em letras minúsculas da string.

Trecho de código usando QString

O exemplo seguinte mostra o uso do QString em uma aplicação gráfica. QLabel é um widget capaz de exibir textos ou imagens.

#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();
}

Aqui está o resultado deste código no Microsoft Windows:

String2.JPG

Links relacionados

140 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved