一、历史遗留问题

C语言不支持真正意义上字符串
C语言用字符数组和一组函数实现字符串的操作
C语言不支持自定义类型,因此无法获得字符串类型
解决方案:
C到C++的进化过程引入了自定义类型
C++中可以通过类完成字符串类型的定义

二、标准库STL

STL是意义上需要与C++一同发布的标准库
STL是一套以模板技术完成的C++类库
STL中包含了常用的算法和数据结构
STL中包含了字符串类
STL的具体实现依赖于编译器生产厂商
STL的“标准”只是其接口是标准的
(1)相同的全局函数
(2)相同的算法类和数据结构类
(3)相同的类成员函数
不同的厂商的编译器所带的STL存在差异

三、Qt中的字符串类

采用Unicode编码
使用隐藏共享技术节省内存和不必要的数据拷贝
跨平台使用,不必考虑字符串的平台兼容性
QString直接支持字符串的数字的相互转换
QString直接支持字符串的大小比较
QString直接支持不用字符编码间的相互转换
QString直接支持std::string和std::wstring的相互转换
QString直接支持正则表达式的应用

实例分析:QString基本操作实例

#include <QDebug>
void Sample_1()
{
    QString s = "add";
    s.append(" ");    // "add "    在后面增加空格
    s.append("Qt");   // "add Qt"  在后面增加字符串
    s.prepend(" ");   // " add Qt" 在前面增加字符串
    s.prepend("C++"); // "C++ add Qt"
    qDebug() << s;
    s.replace("add", "&"); // "C++ & Qt"
    qDebug() << s;
}
void Sample_2()
{
    QString s = "";
    int index = 0;
    s.sprintf("%d. I'm %s, thank you!", 1, "Delphi Tang"); // "1. I'm Delphi Tang, thank you!"格式化字符串
    qDebug() << s;
    index = s.indexOf(",");//查找函数,查逗号
    s = s.mid(0, index);   // "1. I'm Delphi Tang"取子串,index 为长度
    qDebug() << s;
    index = s.indexOf(".");//index索引“.”
    qDebug() << index;
    s = s.mid(index + 1, s.length()); // " I'm Delphi Tang"  索引到的index为点的位置,+1不包含点。
    s = s.trimmed();                  // "I'm Delphi Tang"  去掉空白字符串
    qDebug() << s;
    index = s.indexOf(" ");
    s = s.mid(index + 1, s.length()); // "Delphi Tang"
    qDebug() << s;
}
void Sample_3(QString* a, int len)
{
    for(int i=0; i<len; i++)
    {
        for(int j=i+1; j<len; j++)
        {
            if( a[j] < a[i] )
            {
                QString tmp = a[i];
                a[i] = a[j];
                a[j] = tmp;
            }
        }
    }
}

int main()
{
    qDebug() << "Sample_1:";

    Sample_1();

    qDebug() << endl;
    qDebug() << "Sample_2:";

    Sample_2();

    qDebug() << endl;
    qDebug() << "Sample_3:";

    QString company[5] =
    {
        QString("Oracle"),
        QString("Borland"),
        QString("Microsoft"),
        QString("IBM"),
        QString("D.T.Software")
    };

    Sample_3(company, 5);

    for(int i=0; i<5; i++)
    {
        qDebug() << company[i];
    }

    return 0;
}

输出结果:

Sample_1: 
"C++ add Qt" 
"C++ & Qt" 

Sample_2: 
"1. I'm Delphi Tang, thank you!" 
"1. I'm Delphi Tang" 
1
"I'm Delphi Tang" 
"Delphi Tang" 

Sample_3: 
"Borland" 
"D.T.Software" 
"IBM" 
"Microsoft" 
"Oracle" 

QString在Qt库中无处不在
Qt图形用户依赖于QString

编程实验:为计算器实例添加消息响应

#include "QCalculatorUI.h"
#include <QDebug>

QCalculatorUI::QCalculatorUI() : QWidget(NULL, Qt::WindowCloseButtonHint)
{
}

bool QCalculatorUI::construct()
{
    bool ret = true;
    const char* btnText[20] =
    {
        "7", "8", "9", "+", "(",
        "4", "5", "6", "-", ")",
        "1", "2", "3", "*", "<-",
        "0", ".", "=", "/", "C",
    };

    m_edit = new QLineEdit(this);

    if( m_edit != NULL )
    {
        m_edit->move(10, 10);
        m_edit->resize(240, 30);
        m_edit->setReadOnly(true);
        m_edit->setAlignment(Qt::AlignRight); //设置右对齐
    }
    else
    {
        ret = false;
    }

    for(int i=0; (i<4) && ret; i++)
    {
        for(int j=0; (j<5) && ret; j++)
        {
            m_buttons[i*5 + j] = new QPushButton(this);

            if( m_buttons[i*5 + j] != NULL )
            {
                m_buttons[i*5 + j]->resize(40, 40);
                m_buttons[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);
                m_buttons[i*5 + j]->setText(btnText[i*5 + j]);

                connect(m_buttons[i*5 + j], SIGNAL(clicked()), this, SLOT(onButtonClicked()));
            }
            else
            {
                ret = false;
            }
        }
    }

    return ret;
}

QCalculatorUI* QCalculatorUI::NewInstance()
{
    QCalculatorUI* ret = new QCalculatorUI();

    if( (ret == NULL) || !ret->construct() )
    {
        delete ret;
        ret = NULL;
    }

    return ret;
}

void QCalculatorUI::show()
{
    QWidget::show();

    setFixedSize(width(), height());
}

void QCalculatorUI::onButtonClicked()//消息处理函数
{
    QPushButton* btn = (QPushButton*)sender();//发送消息的对象指针,
    QString clickText =btn->text();
    if(clickText == "<-")
    {
      QString text = m_edit->text();
      if(text.length()>0)
      {
        text.remove(text.length()-1,1);//删除最后一个字符
       // m_edit->setText(text);
      }
    }
    else if(clickText == "C")
    {
        m_edit->setText("");
    }
    else if(clickText == "=")
    {

    }
    else
    {
         m_edit->setText(m_edit->text() + clickText);
    }
    //QString clickText = m_edit->text() + btn->text();//获取当前显示的字符串,+被点击按钮上面的字符串
   // m_edit->setText(clickText);
    //qDebug() << "onButtonClicked()";
    //qDebug() << btn->text();//获取被点击按钮上面的字符串
}

QCalculatorUI::~QCalculatorUI()
{

}

四、小结:

应用开发中大多数的情况都在进行字符串的处理
Qt比STL更适合夸平台开发的场景
Qt中的QString比STLstring更强大
Qt图形用户组件都依赖于QString
项目开发时需要综合各种选择需要使用的库

郑重声明:以上内容参考狄泰软件学院系列课程!


YingLi
6 声望4 粉丝

From zero to hero.