Archived:Como usar um QString
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.
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:
Originado de How to use QString in Qt
Por valderind4
Ú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:
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.
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:
Links relacionados
- Documentation of QString(Inglês)
- How to convert TBuf to QString(Inglês)
- How to convert TBuf to QString(Inglês)

