QString

 #include <QtCore>
 #include <iostream>
 ​
 int main(){
     std::cout << "Qt version: " << qVersion() << std:endl;
 }

 

 #include <QApplication>
 #include <QWidget>
 ​
 int main(int argc, char *argv[]){
     QApplication app(argc,argv);
     QWidget window;
     
     window.resize(250,150);
     window.setWindowTitle("Simple example");
     window.show();
     
     return app.exec();
 }
 TEMPLATE = app
 TARGET = simple
 INCLUDEPATH += .
 ​
 SOURCES += simple.cpp
 ​
 QT += widgets
基础示例
 #include <QTextStream>
 ​
 int main(void){
     QTextStream out(stdout);
     
     QString a{"love"};
     a.append(" chess");
     a.prepend("I ");
     
     out << a << endl;
     out << "The a string has" << a.count() << " characters" << endl;
     out << a.toUpper() << endl;
     
     return 0;
 }

 

初始化字符串
 #include <QTextStream>
 #include <iostream>
 ​
 int main(void){
     //stream
     QTextStream out(stdout);
     //赋值
     QString str1 = "The niight train";
     out << str1 << endl;
     //赋值
     QString str2("A yellow rose");
     out << str2 << endl;
     //falcon
     QString str3{"An old falcon"};
     out << str3 << endl;
     //c_str()
     std::string s1 = "A blue sky";
     QString str4 = s2.c_str();
     out << str4 << endl;
     //latin
     std::string s2 = "A thick fog";
     QString str5 = QString.fromLatin1(s2.data(),s2.size());
     //char[]
     char s3[] = "A deep forest";
     QString str6(s3);
     out << str6 << endl;
     
     return 0;
 }
访问字符串元素
 #include <QTextStream>
 ​
 int main(void){
     QTextStream out(stdout);
 ​
     QString a { "Eagle" };
     
     out << a[0] << endl;
     out << a[4] << endl;
     
     out << a.at(0) << endl;
     
     if(a.at(5).isNull()){
         out << "outside the range of the string" << endl;
    }
     return 0;
 }
字符串长度
 #include <QTextStream>
 ​
 int main(void){
     QTextStream out(stdout);
     QString s1 = "Eagle";
     QString s2 = "Eagle\n";
     QString s3 = "Eagle ";
     QString s4 = "open";
     //size(),count(),length()相同
     out << s1.length() << endl;
     out << s2.length() << endl;
     out << s3.length() << endl;
     out << s4.length() << endl;
     
     return 0;
 }
构建字符串
 #include <QTextStream>
 ​
 int main(void){
     QTextStream out(stdout);
     
     QString s1 = "There are 1% white roses";
     int n = 12;
     out << s1.arg(n) << endl;
     
     QString s2 = "The tree is %1 m height";
     double h = 5.65;
     out << s2.arg(h) << endl;
     
     QString s3 = "We have %1 lemons and %2 oranges";
  int ln = 12;
     int on = 4;
     out << s3.arg(ln).arg(on) << endl;
     
     return 0;
 }
子字符串
 #include <QTextStream>
 ​
 int main(void){
     QTextStream out(stdout);
     
     QString str = {"The night train"};
     
     out << str.right(5) << endl;
     out << str.left(9) << endl;
     out << str.mid(4,5) << endl;
     
     QString str2("the big apple");
     QStringRef subref(&str2,0,7);
     
     out << subref.toString() << endl;
 }
字符串遍历
 #include <QTextStream>
 ​
 int main(void){
     QTextStream out(stdout);
     QString str{"There are many stars."};
     //:
     for(QChar qc:str){
         out << qc << " ";
    }
     out << endl;
     //begin
     for(QChar *it = str.begin();it!=str.end();++it){
         out << *it << " ";
    }
     out << endl;
     //i
     for(int i = 0;i<str.size();++1){
         out << str.at(i) << " ";
    }
     out << endl;
     
     return 0;
 }
字符串比较
 #include <QTextStream>
 ​
 #define STR_EQUAL 0
 ​
 int main(void) {
 ​
    QTextStream out(stdout);
 ​
    QString a { "Rain" };
    QString b { "rain" };
    QString c { "rain\n" };
    if (QString::compare(a, b) == STR_EQUAL) {
      out << "a, b are equal" << endl;
    } else {
      out << "a, b are not equal" << endl;
    }
 ​
    out << "In case insensitive comparison:" << endl;
 ​
    if (QString::compare(a, b, Qt::CaseInsensitive) == STR_EQUAL) {
      out << "a, b are equal" << endl;
    } else {
      out << "a, b are not equal" << endl;
    }
 ​
    if (QString::compare(b, c) == STR_EQUAL) {
      out << "b, c are equal" << endl;
    } else {
      out << "b, c are not equal" << endl;
    }
 ​
    c.chop(1);
 ​
    out << "After removing the new line character" << endl;
 ​
    if (QString::compare(b, c) == STR_EQUAL) {
      out << "b, c are equal" << endl;
    } else {
      out << "b, c are not equal" << endl;
    }
 ​
    return 0;
 }
字符串转换
 #include <QTextStream>
 ​
 int main(void){
     QTextStream out(stdout);
     QString s1{"12"};
     QString s2{"15"};
     QString s3,s4;
     out << s1.toInt() + s2.toInt() << endl;
     
     int n1 = 30;
     int n2 = 40;
     
     out << s3.setNum(n1) + s4.setNum(n2) << endl;
     
     return 0;
 }
字符
 #include <QTextStream>
 ​
 int main(void) {
 ​
   QTextStream out(stdout);
 ​
   int digits  = 0;
   int letters = 0;
   int spaces  = 0;
   int puncts  = 0;
 ​
   QString str { "7 white, 3 red roses." };
 ​
   for (QChar s : str) {
 ​
     if (s.isDigit()) {
       digits++;
    } else if (s.isLetter()) {
       letters++;
    } else if (s.isSpace()) {
       spaces++;
    } else if (s.isPunct()) {
       puncts++;
    }
  }
 ​
   out << QString("There are %1 characters").arg(str.count()) << endl;
   out << QString("There are %1 letters").arg(letters) << endl;
   out << QString("There are %1 digits").arg(digits) << endl;
   out << QString("There are %1 spaces").arg(spaces) << endl;
   out << QString("There are %1 punctuation characters").arg(puncts) << endl;
 ​
   return 0;
 }
修改字符串
 #include <QTextStream>
 ​
 int main(void) {
 ​
    QTextStream out(stdout);
 ​
    QString str { "Lovely" };
    str.append(" season");
 ​
    out << str << endl;
 ​
    str.remove(10, 3);
    out << str << endl;
 ​
    str.replace(7, 3, "girl");
    out << str << endl;
 ​
    str.clear();
 ​
    if (str.isEmpty()) {
      out << "The string is empty" << endl;
    }
 ​
    return 0;
 }
字符串对齐
 #include <QTextStream>
 ​
 int main(void) {
 ​
    QTextStream out(stdout);
 ​
    QString field1 { "Name: " };
    QString field2 { "Occupation: " };
    QString field3 { "Residence: " };
    QString field4 { "Marital status: " };
 ​
    int width = field4.size();
 ​
    out << field1.rightJustified(width, ' ') << "Robert\n";
    out << field2.rightJustified(width, ' ') << "programmer\n";
    out << field3.rightJustified(width, ' ') << "New York\n";
    out << field4.rightJustified(width, ' ') << "single\n";
 ​
    return 0;
 }
HTML转义
 #include <QTextStream>
 #include <QFile>
 ​
 int main(void) {
 ​
     QTextStream out(stdout);
 ​
     QFile file("cprog.c");
 ​
     if (!file.open(QIODevice::ReadOnly)) {
 ​
         qWarning("Cannot open file for reading");
         return 1;
    }
 ​
     QTextStream in(&file);
 ​
     QString allText = in.readAll();
     out << allText.toHtmlEscaped() << endl;
 ​
     file.close();
 ​
     return 0;
 }

 

上一篇:QT集成CEF10-JavaScript与C++互调


下一篇:Qt实现时间范围选择