您的位置:首页 > 编程语言 > Qt开发

QT中关于QString的一些小结

2013-02-21 14:24 561 查看
 QString 类是 Qt 中用于表示字符串的类

各类型之间的转换:

1.将QString类转化为其他类型;

double toDouble(bool *ok = 0) const; // 转换为高精度浮点数
float toFloat(bool *ok = 0) cosnt; // 转换为浮点数
int toInt(bool *ok, int base = 10) const; // 转换为整型数
long toLong(bool *ok, int base = 10) cosnt; // 转换为长整型
short toShort(bool *ok, int base = 10) const; // 转换为短整型
uint toUInt(bool *ok = 0; int base = 10) const // 转换为无符号整型数
ulong toLong(bool *ok = 0, int base = 10) const; // 转换为无符号长整型数
ushort toUShort(bool *ok = 0, int base = 10) const; // 转换为无符号短整型数

用法eg:Qstring str;
double a = str.toDouble();

2.将其他类型转化为QString类型;

QString &setNum(int n, int base = 10); // 整型数
QString &setNum(uint n, int base = 10); // 无符号整型数
QString &setNum(long n, int base = 10); // 长整型
QString &setNum(ulong n, int base = 10); // 无符号长整型数
QString &setNum(short n, int base = 10); // 短整型数
QString &setNum(ushort n, int base = 10); // 无符号短整型数
QString &setNum(double n, char format = 'g', int precision = 6); // 高精度浮点数
QString &setNum(float n, char format = 'g', int precision = 6); // 浮点数
QString number(int n, int base = 10);
QString number(uint n, int base = 10);
QString number(long n, int base = 10);
QString number(ulong n ,int base = 10);
QString number(double n, char format = 'q', int precision = 6);


以上两种转化时有区别的;具体区别参考http://blog.csdn.net/hmsiwtv/article/details/7553328
3.大小写转换函数;

QString toLower() const; // 转换为小写
QString toUpper() const; // 转换为大写

4.下面这个成员函数可以截掉 QString 对象最后的若干个字符:
void chop(int n); // 截掉最后的 n个字符

5. 以下几个成员函数可以得到 QString 对象的子字符串
QString left(int n) const; // 得到左边 n 个字符构成的子字符串
QString right(int n) const; // 得到右边 n 个字符构成的子字符串
QString mid(int position, int n = -1) const; // 从中间得到子字符串

6. 以下两个成员函数都可以得到 QString 对象中字符的个数:
int size() const;
int length() const;
7.以下的成员函数可以将另一个字符串或字符接在 QString 对象后面,形成一个整体的字符串:
QString &append(const QString &str);                // 接续 QString 对象
QString &append(const char *str);                    // 接续普通字符串
QString &append(QChar ch);                            // 接续 QChar 对象

以上都是我自己在写程序时候用到且证实正确的内容,其实QString还有很多我不知道的内容,有意则可自己查找相关资料!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  QT QString