Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Using QString effectively


Published on

Using QString effectively

Published in: Education
  • Be the first to comment

  • Be the first to like this

Using QString effectively

  1. 1. By Roman Okolovich
  2. 2. General <ul><li>The QString class provides a Unicode character string. </li></ul><ul><li>QString class is used for </li></ul><ul><ul><li>Initialize a Qstring from const char * </li></ul></ul><ul><ul><li>Keep strings </li></ul></ul><ul><ul><li>Compare strings </li></ul></ul><ul><ul><li>Manipulate string data (append, prepend, insert, replace, mid, left, right, etc) </li></ul></ul><ul><ul><li>Construct a complex string from multiple substrings </li></ul></ul><ul><ul><li>Etc </li></ul></ul>
  3. 3. Important <ul><li>Many of QString's member functions are overloaded to accept const char * instead of QString. </li></ul><ul><li>QString converts the  const char*  data into Unicode using the fromAscii() function </li></ul><ul><ul><li>QString will allocate memory for the string (malloc function is called) </li></ul></ul><ul><ul><li>creates a deep copy of the C-style string </li></ul></ul><ul><li>Applications that define  QT_NO_CAST_FROM_ASCII  don't have access to QString‘s const char* API. </li></ul>
  4. 4. Example <ul><li>if (str1.contains( &quot;title&quot; )) // 1 </li></ul><ul><li>{ </li></ul><ul><li>QString str2 = str1. mid (str1.indexOf( &quot;TX&quot; ) + 2, 5); // 2* </li></ul><ul><li>if (str2 == &quot;text1&quot; || // 3 </li></ul><ul><li>0 == &quot;text2&quot; )) // 4 </li></ul><ul><li>{ </li></ul><ul><li>// todo </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>// hidden malloc is called 4 times </li></ul><ul><li>// * mid() creates new instance of QString (+1 malloc) </li></ul><ul><li>bool contains(const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const </li></ul><ul><li>bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const </li></ul><ul><li>bool contains(const QRegExp & rx) const </li></ul><ul><li>bool contains(QRegExp & rx) const </li></ul><ul><li>int compare(const QString & other) const </li></ul><ul><li>int compare(const QString & other, Qt::CaseSensitivity cs) const </li></ul><ul><li>int compare(const QLatin1String & other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const </li></ul><ul><li>int compare(const QStringRef & ref, Qt::CaseSensitivity cs = Qt::CaseSensitive) const </li></ul><ul><li>bool operator== (const char * other) const </li></ul><ul><ul><li>The  other  const char pointer is converted to a QString using the fromAscii() function. </li></ul></ul>
  5. 5. Solution if (str1.contains(QLatin1String( &quot;title&quot; ))) { QStringRef str2 = str1.midRef(str1.indexOf(QLatin1String( &quot;TX&quot; )) + 2, 4); if (str2 == QLatin1String( &quot;text1&quot; ) || // QString::operator==(const QLatin1String &) 0 == &quot;text2&quot; ))) { // todo } } <ul><li>The QLatin1String class provides a thin wrapper around an ASCII/Latin-1 encoded string literal (It just holds a pointer to the C-string provided in it’s constructor) </li></ul><ul><ul><li>Thanks to the  QString(const QLatin1String &) constructor, QLatin1String can be used everywhere a QString is expected. For example: </li></ul></ul><ul><ul><ul><li>QLabel *label = new QLabel(QLatin1String(&quot;MOD&quot;), this); </li></ul></ul></ul><ul><li>QStringRef is designed to improve the performance of substring handling when manipulating substrings obtained from existing QString instances. QStringRef avoids the memory allocation and reference counting overhead of a standard QString by simply referencing a part of the original string. </li></ul><ul><ul><li>Calling toString() returns a copy of the data as a real QString instance. </li></ul></ul>
  6. 6. More Efficient String Construction <ul><li>if (foo.startsWith( &quot;(&quot;  + type +  &quot;) 0x“ ) ) </li></ul><ul><ul><li>This code requires at least 2 mallocs </li></ul></ul><ul><li>if (foo.startsWith(QLatin1String( &quot;(&quot; )  + type + QLatin1String (&quot;) 0x&quot; ))) </li></ul><ul><li>In 4.6, an internal template class QStringBuilder has been added. This class is marked internal and does not appear in the documentation. </li></ul><ul><li>QStringBuilder uses expression templates and reimplements the '%' operator so that when you use '%' for string concatenation instead of '+', multiple substring concatenations will be postponed until the final result is about to be assigned to a QString. At this point, the amount of memory required for the final result is known. The memory allocator is then called  once  to get the required space, and the substrings are copied into it one by one. </li></ul>#include <QStringBuilder> QString hello( &quot;hello&quot; ); QStringRef el(&hello, 2, 3); QLatin1String world( &quot;world&quot; ); QString message = hello % el % world % QChar( '!' );
  7. 7. References <ul><li>QString Class Reference </li></ul><ul><li>QLatin1String Class Reference </li></ul><ul><li>Qt wiki </li></ul>