QString & String

今天要说的是QString。之所以把QString单独拿出来,是因为string是很常用的一个数据结构,甚至在很多语言中,比如JavaScript,都是把string作为一种同int等一样的基本数据结构来实现的。

每一个GUI程序都需要string,这些string可以用在界面上的提示语,也可以用作一般的数据结构。C++语言提供了两种字符串的实现:C风格的字符串,以'\0‘结尾;std::string,即标准模版库中的类。Qt则提供了自己的字符串实现:QString。QString以16位Uniode进行编码。我们平常用的ASCII等一些编码集都作为Unicode编码的子集提供。关于编码的问题,我们会到以后的时候再详细说明。

在使用QString的时候,我们不需要担心内存分配以及关于'\0'结尾的这些注意事项。QString会把这些问题解决。通常,你可以把QString看作是一个QChar的向量。另外,与C风格的字符串不同,QString中间是可以包含'\0'符号的,而length()函数则会返回整个字符串的长度,而不仅仅是从开始到'\0'的长度。

同Java的String类类似,QString也重载的+和+=运算符。这两个运算符可以把两个字符串连接到一起,正像Java里面的操作一样。QString可以自动的对占用内存空间进行扩充,这种连接操作是恨迅速的。下面是这两个操作符的使用:

 
  1. QString str = "User: ";  
  2. str += userName + "\n"

QString的append()函数则提供了类似的操作,例如:

 
  1. str = "User: ";  
  2. str.append(userName);  
  3. str.append("\n"); 

C语言中有printf()函数作为格式化输出,QString则提供了一个sprintf()函数实现了相同的功能:

 
  1. str.sprintf("%s %.1f%%""perfect competition", 100.0); 

这句代码将输出:perfect competition 100.0%,同C语言的printf()一样。不过前面我们也见到了Qt提供的另一种格式化字符串输出的函数arg():

 
  1. str = QString("%1 %2 (%3s-%4s)")  
  2.       .arg("permissive").arg("society").arg(1950).arg(1970); 

这段代码中,%1, %2, %3, %4作为占位符,将被后面的arg()函数中的内容依次替换,比如%1将被替换成permissive,%2将被替换成society,%3将被替换成1950,%4将被替换曾1970,最后,这句代码输出为:permissive society (1950s-1970s). arg()函数比起sprintf()来是类型安全的,同时它也接受多种的数据类型作为参数,因此建议使用arg()函数而不是传统的sprintf()。

使用static的函数number()可以把数字转换成字符串。例如:

 
  1. QString str = QString::number(54.3); 

你也可以使用非static函数setNum()来实现相同的目的:

 
  1. QString str;  
  2. str.setNum(54.3); 

而一系列的to函数则可以将字符串转换成其他基本类型,例如toInt(), toDouble(), toLong()等。这些函数都接受一个bool指针作为参数,函数结束之后将根据是否转换成功设置为true或者false:

 
  1. bool ok;  
  2. double d = str.toDouble(&ok);  
  3. if(ok)  
  4. {  
  5.     // do something...  
  6. else {  
  7.     // do something...  

对于QString,Qt提供了很多操作函数,例如,使用mid()函数截取子串:

 
  1. QString x = "Nine pineapples";  
  2. QString y = x.mid(5, 4);            // y == "pine"  
  3. QString z = x.mid(5);               // z == "pineapples" 

mid()函数接受两个参数,第一个是起始位置,第二个是取串的长度。如果省略第二个参数,则会从起始位置截取到末尾。正如上面的例子显示的那样。

函数left()和rigt()类似,都接受一个int类型的参数n,都是对字符串进行截取。不同之处在于,left()函数从左侧截取n个字符,而right()从右侧开始截取。下面是left()的例子:

 
  1. QString x = "Pineapple";  
  2. QString y = x.left(4);      // y == "Pine" 

函数indexOf()返回字符串的位置,如:

 
  1. QString x = "sticky question";  
  2. QString y = "sti";  
  3. x.indexOf(y);               // returns 0  
  4. x.indexOf(y, 1);            // returns 10  
  5. x.indexOf(y, 10);           // returns 10  
  6. x.indexOf(y, 11);           // returns -1 

函数startsWith()和endsWith()可以检测字符串是不是以某个特定的串开始或结尾,例如:

 
  1. if (url.startsWith("http:") && url.endsWith(".png"))  
  2. {  

这段代码等价于

 
  1. if (url.left(5) == "http:" && url.right(4) == ".png")  
  2. {  

不过,前者要比后者更加清楚简洁,并且性能也更快一些。

QString还提供了replace()函数供实现字符串的替换功能;trimmed()函数去除字符串两侧的空白字符(注意,空白字符包括空格、Tab以及换行符,而不仅仅是空格);toLower()和toUpper()函数会将字符串转换成小写大写字符串;remove()和insert()函数提供了删除和插入字符串的能力;simplified()函数可以将串中的所有连续的空白字符替换成一个,并且把两端的空白字符去除,例如"   \t   ”会返回一个空格" "。

将const char *类型的C风格字符串转换成QString也是很常见的需求,简单来说,QString的+=即可完成这个功能:

 
  1. str += " (1870)"

这里,我们将const char * 类型的字符串" (1870)"转换成为QString类型。如果需要显式的转换,可以使用QString的强制转换操作,或者是使用函数fromAscii()等。为了将QString类型转成const char *字符串,需要进行两步操作,一是使用toAscii()获得一个QByteArray类型对象,然后调用它的data()或者constData()函数,例如:

 
  1. printf("User: %s\n", str.toAscii().data()); 

为了方便使用,Qt提供了一个宏qPrintable(),这个宏等价于toAscii().constData(),例如:

 
  1. printf("User: %s\n", qPrintable(str)); 

我们调用QByteArray类上面的data()或者constData()函数,将获得QByteArray内部的一个const char*类型的字符串,因此,我们不需要担心内存泄漏等的问题,Qt会替我们管理好内存。不过这也暗示我们,注意不要使用这个指针太长时间,因为如果QByteArray被delete,那么这个指针也就成为野指针了。如果这个QByteArray对象没有被放在一个变量中,那么当语句结束后,QbyteArray对象就会被delete,这个指针也就被delete 了。

本文出自 “豆子空间” 博客,请务必保留此出处http://devbean.blog.51cto.com/448512/275360

#ifndef __QFTP_CLIENT_H__ #define __QFTP_CLIENT_H__ // make sure to add qftp depending on #include <QTimer> #include <QFile> #include <QUrl> #include <QMap> #include <QVariant> #include <QMutex> #include <QBuffer> #include <QSet> #include "qftp.h" struct RemoteDir; struct SingleCmd; class QFtpClient : public QFtp { Q_OBJECT public: enum QFTP_CMD { QC_NONE, QC_CD, QC_LIST, QC_RENAME, QC_PUT_FILE, QC_PUT_FILES, QC_PUT_TREE, QC_GET_BUFFER, QC_GET_FILE, QC_GET_FILES, QC_GET_TREE, QC_RM_FILE, QC_RM_TREE, QC_MKD, QC_MKDS, }; enum record_type { rt_success, rt_executing, rt_total, rt_count, }; public: QFtpClient(QString serverip = "", QString username = "", QString password = "", qint16 port = 21, QObject* parent = 0); ~QFtpClient(); void setIP(const QString& ip, const qint16 port = 21); void setUser(const QString& username, const QString& password); void close_host(); bool sync_connect(int msec = 3000); // 现存缺陷: 不能创建含中文的目录/文件 // 绝对路径操作接口 // 上传文件/目录 bool put_files(const QList< QPair<QString, QString> >& vfiles, int msec = 30000); //list< pir<localfile, remotefile> > bool put_file(const QString& localabsfile, const QString& remoteabsfile, int msec = 3000); bool put_tree(const QString& localabspath, const QString& remoteabspath, const QStringList& exceptabspath = {}, int msec = 30000); // 下载文件/目录 bool get_buffer(const QString& remoteabsfile, int msec = 3000); bool get_file(const QString& remoteabsfile, const QString& localabsfile, int msec = 3000); bool get_files(const QList< QPair<QString, QString> >& vfiles, int msec = 30000); //list< pir<remotefile, localfile> > bool get_tree(const QString& remoteabspath, const QString& localabspath, const QStringList& exceptabspath = {}, int msec = 30000); // 删除文件/目录 bool rm_file(const QString& remotefile, int msec = 3000); bool rm_tree(const QString& dir, int msec = 30000); // 新建目录 bool mk_dir(const QString& dir, int msec = 3000); bool mk_dir(const QStringList& dirlist, int msec = 3000); // 重命名 bool _rename(const QString& remotefilepath, const QString& newname, int msec = 3000); // 显示列表 bool _list(const QString& remotepath, int msec = 3000); // 当前目录操作接口 bool pwd_put_file(const QString& localabsfile, int msec = 3000); bool pwd_get_file(const QString& remotefile, const QUrl& localurl, int msec = 3000); bool pwd_rm_file(const QString& remotefile, int msec = 3000); bool pwd_rm_tree(const QString& remotedir, int msec = 10000); bool pwd_mk_dir(const QString& remotedir, int msec = 3000); bool pwd_rename(const QString& remotefile, const QString& newname, int msec = 3000); bool pwd_list(int msec = 3000); bool pwd_cd(const QString& dirname, bool done_list = false, int msec = 3000); // 是否被占用 bool is_busy()const; // 是否已连接 bool is_connect()const; QString pwd(); signals: void signal_state_changed(int); void signal_command_finish(int cmdid, QVariantList params, QString errorstring = ""); void signal_ftp_log(const QString&, bool error = false); void signal_list(const QList<QUrlInfo>&); void signal_get_buffer(const QByteArray&); void signal_done(int cmdid, int ok_num, int exec_num, int total_num); private: bool inner_put_file(const QString& localabsfile, const QString& remoteabsfile, int msec = 3000); bool inner_put_files(QVariantList& files, int msec = 30000); bool inner_put_tree(const QString& localabspath, const QString& remoteabspath, const QStringList& vExcept = {}, int msec = 30000); bool inner_get_buffer(const QString& remoteabsfile, int msec = 3000); bool inner_get_file(const QString& remoteabsfile, const QString& localabsfile, int msec = 3000); bool inner_get_files(QVariantList& files, int msec = 30000); bool inner_get_tree(const QString& remoteabspath, const QString& localabspath, const QStringList& vExcept = {}, int msec = 30000); bool inner_rm_file(const QString& remotefile, int msec = 3000); bool inner_rm_tree(const QString& dir, int msec = 30000); bool inner_mk_dir(const QString& dir, int msec = 3000); bool inner_mk_dir(const QStringList& dirlist, int msec = 3000); bool inner_rename(const QString& remotefilepath, const QString& newname, int msec = 3000); bool inner_list(const QString& remotepath = "", int msec = 3000); bool inner_cd(const QString& dirname, bool done_list = false, int msec = 3000); // done_list - 切换完成后, 是否需要list(用于ui显示) void connect_host(); void connect_result(QString errstr); void login_result(QString errstr); void close_result(QString errstr); void list_result(const SingleCmd& smd); void get_result(const SingleCmd& smd, QString errstr); bool has_cmd(); bool next_cmd(); bool next_cmd_util_done(int msec); void cd_result(const SingleCmd& smd, QString errstr); void put_result(const SingleCmd& smd, QString errstr); void mkd_result(const SingleCmd& smd, QString errstr); void rmd_result(const SingleCmd& smd, QString errstr); void rm_result(const SingleCmd& smd, QString errstr); void rename_result(const SingleCmd& smd, QString errstr); void build_pwd_cd_cmd(const QString& dirname, bool done_list = false); // 当前目录cd void build_cd_cmd(const QString& remotepath); // 绝对路径cd void build_cd_cmd(const QString& remotepath, QList<SingleCmd>& l); void build_mkdir_cmd(const QString& remotepath); void build_rmdir_cmd(const QString& remotepath); void build_rm_cmd(const QString& remotepath); void build_put_cmd(const QString& localfile, const QString& remotefile); void build_puts_cmd(const QVariantList&); void build_putree_cmd(const QString& localpath, const QString& remotepath, const QStringList& vExcept = {}); void build_get_buffer_cmd(const QString& remotefile); void build_get_cmd(const QString& remotefile, const QString& localfile); void build_gets_cmd(const QVariantList&); void build_getree_cmd(const QString& remotepath, const QString& localpath, const QStringList& vExcept = {}); void build_rename_cmd(const QString& remotefilepath, const QString& newname); void build_list_cmd(const QString& remotepath); void clear_cmd(); void reset_record(); void inc_record(record_type, int = 1); private slots: void slot_state_changed(int); void slot_timer_connect(); void slot_finish(int, bool); void slot_listinfo(const QUrlInfo&); void slot_inner_cmd(int cmdid, QVariantList params, QString errorstring); protected: QUrl m_url; QTimer* m_pConnect = Q_NULLPTR; QPair<QString, QString> m_CurrentPath; // local/remote path // download member QList< QPair<QString, QString> > m_PendingPath; // local/remote path QBuffer* m_buffer = 0; // rmdir member QList<SingleCmd> m_rmlist; // cmd member QList<SingleCmd> m_lCmd; QMap<int, SingleCmd> m_mapCmd; // list member QFtp::Command m_listcmd = QFtp::Command::None; QFtp::Command m_prevlistcmd = QFtp::Command::None; QList<QUrlInfo> m_listresult; QStringList m_PathSection = {}; QFTP_CMD m_cmd = QC_NONE; int success_count = 0, execute_count = 0, total_count = 0; QSet<QString> m_vExcept; }; #endif 这是QFtpclient.h
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值