Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

Strings, Byte Arrays, and Variants

QString, QByteArray, and QVariant are three classes that have many things in common with containers and that can be used as alternatives to containers in some contexts. Also, like the containers, these classes use implicit sharing as a memory and speed optimization.

We will start with QString. Every GUI program uses strings, not only for the user interface but often also as data structures. C++ natively provides two kinds of strings: traditional C-style '\0'-terminated character arrays and the std::string class. Unlike these, QString holds 16-bit Unicode values. Unicode contains ASCII and Latin-1 as a subset, with their usual numeric values. But since QString is 16-bit, it can represent thousands of other characters for writing most of the world's languages. See Chapter 18 for more information about Unicode.

When using QString, we don't need to worry about such arcane details as allocating enough memory or ensuring that the data is '\0'-terminated. Conceptually, QStrings can be thought of as a vector of QChars. A QString can embed '\0' characters. The length() function returns the size of the entire string, including embedded '\0' characters.

QString provides a binary + operator to concatenate two strings and a += operator to append one string to another. Because QString automatically preallocates memory at the end of the string data, building up a string by repeatedly appending characters is very fast. Here's an example that combines + and +=:

QString str = "User: ";
str += userName + "\n";

There is also a QString::append() function that does the same thing as the += operator:

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

A completely different way to combine strings is to use QString's sprintf() function:

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

This function supports the same format specifiers as the C++ library's sprintf() function. In the preceding example, str is assigned "perfect competition 100.0%".

Yet another way to build a string from other strings or from numbers is to use arg():

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

In this example, "%1" is replaced by "permissive", "%2" is replaced by "society", "%3" is replaced by "1950", and "%4" is replaced by "1970". The result is "permissive society (1950s-1970s)". There are arg() overloads to handle various data types. Some overloads have extra parameters for controlling the field width, the numerical base, or the floating-point precision. In general, arg() is a much better solution than sprintf(), because it is type-safe, fully supports Unicode, and allows translators to reorder the "%n" parameters.

QString can convert numbers into strings using the QString::number() static function:

str = QString::number(59.6);

Or using the setNum() function:

str.setNum(59.6);

The reverse conversion, from a string to a number, is achieved using toInt(), toLongLong(), toDouble(), and so on. For example:

bool ok;
double d = str.toDouble(&ok);

These functions accept an optional pointer to a bool variable and set the variable to true or false depending on the success of the conversion. If the conversion fails, these functions return zero.

Once we have a string, we often want to extract parts of it. The mid() function returns the substring starting at a given position (the first argument) and of up to a given length (the second argument). For example, the following code prints "pays" to the console: [*]

QString str = "polluter pays principle";
qDebug() << str.mid(9, 4);

If we omit the second argument, mid() returns the substring starting at the given position and ending at the end of the string. For example, the following code prints "pays principle" to the console:

QString str = "polluter pays principle";
qDebug() << str.mid(9);

There are also left() and right() functions that perform a similar job. Both accept a number of characters, n, and return the first or last n characters of the string. For example, the following code prints "polluter principle" to the console:

QString str = "polluter pays principle";
qDebug() << str.left(8) << " " << str.right(9);

If we want to find out whether a string contains a particular character, substring, or regular expression, we can use one of QString's indexOf() functions:

QString str = "the middle bit";
int i = str.indexOf("middle");

This will set i to 4. The indexOf() function returns -1 on failure, and accepts an optional start position and case-sensitivity flag.

If we just want to check whether a string starts or ends with something, we can use the startsWith() and endsWith() functions:

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

The preceding code is both simpler and faster than the following code:

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

String comparison with the == operator is case-sensitive. If we are comparing user-visible strings, localeAwareCompare() is usually the right choice, and if we want to make the comparisons case-insensitive, we can use toUpper() or toLower(). For example:

if (fileName.toLower() == "readme.txt")
    ...

If we want to replace a certain part of a string by another string, we can use replace():

QString str = "a cloudy day";
str.replace(2, 6, "sunny");

The result is "a sunny day". The code can be rewritten to use remove() and insert():

str.remove(2, 6);
str.insert(2, "sunny");

First, we remove six characters starting at position 2, resulting in the string "a day" (with two spaces), and then we insert "sunny" at position 2.

There are overloaded versions of replace() that replace all occurrences of their first argument with their second argument. For example, here's how to replace all occurrences of "&" with "&amp" in a string:

str.replace("&", "&amp");

One very frequent need is to strip the whitespace (such as spaces, tabs, and newlines) from a string. QString has a function that eliminates whitespace from both ends of a string:

QString str = "   BOB \t THE \nDOG \n";
qDebug() << str.trimmed();

String str can be depicted as

     

B

O

B

 

\t

 

T

H

E

   

\n

D

O

G

 

\n

The string returned by trimmed() is

B

O

B

 

\t

 

T

H

E

   

\n

D

O

G

When handling user input, we often also want to replace every sequence of one or more internal whitespace characters with single spaces, in addition to stripping whitespace from both ends. This is what the simplified() function does:

QString str = "   BOB \t THE \nDOG \n";
qDebug() << str.simplified();

The string returned by simplified() is

B

O

B

 

T

H

E

 

D

O

G

A string can be split into a QStringList of substrings using QString::split():

QString str = "polluter pays principle";
QStringList words = str.split(" ");

In the preceding example, we split the string "polluter pays principle" into three substrings: "polluter", "pays", and "principle". The split() function has an optional second argument that specifies whether empty substrings should be kept (the default) or discarded.

The items in a QStringList can be joined to form a single string using join(). The argument to join() is inserted between each pair of joined strings. For example, here's how to create a single string that is composed of all the strings contained in a QStringList sorted into alphabetical order and separated by newlines:

words.sort();
str = words.join("\n");

When dealing with strings, we often need to determine whether a string is empty or not. This is done by calling isEmpty() or by checking whether length() is 0.

The conversion from const char * strings to QString is automatic in most cases, for example:

str += " (1870)";

Here we add a const char * to a QString without formality. To explicitly convert a const char * to a QString, simply use a QString cast, or call fromAscii() or fromLatin1(). (See Chapter 18 for an explanation of handling literal strings in other encodings.)

To convert a QString to a const char *, use toAscii() or toLatin1(). These functions return a QByteArray, which can be converted into a const char * using QByteArray::data() or QByteArray::constData(). For example:

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

For convenience, Qt provides the qPrintable() macro that performs the same as the sequence toAscii().constData():

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

When we call data() or constData() on a QByteArray, the returned string is owned by the QByteArray object. This means that we don't need to worry about memory leaks; Qt will reclaim the memory for us. On the other hand, we must be careful not to use the pointer for too long. If the QByteArray is not stored in a variable, it will be automatically deleted at the end of the statement.

The QByteArray class has a very similar API to QString. Functions such as left(), right(), mid(), toLower(), toUpper(), trimmed(), and simplified() exist in QByteArray with the same semantics as their QString counterparts. QByteArray is useful for storing raw binary data and 8-bit encoded text strings. In general, we recommend using QString for storing text rather than QByteArray because QString supports Unicode.

For convenience, QByteArray automatically ensures that the "one past the last" byte is always '\0', making it easy to pass a QByteArray to a function taking a const char *. QByteArray also supports embedded '\0' characters, allowing us to use it to store arbitrary binary data.

In some situations, we need to store data of different types in the same variable. One approach is to encode the data as a QByteArray or a QString. For example, a string could hold a textual value or a numeric value in string form. These approaches give complete flexibility, but do away with some of C++'s benefits, in particular type safety and efficiency. Qt provides a much cleaner way of handling variables that can hold different types: QVariant.

The QVariant class can hold values of many Qt types, including QBrush, QColor, QCursor, QDateTime, QFont, QKeySequence, QPalette, QPen, QPixmap, QPoint, QRect, QRegion, QSize, and QString, as well as basic C++ numeric types such as double and int. The QVariant class can also hold containers: QMap<QString, QVariant>, QStringList, and QList<QVariant>.

The item view classes, the database module, and QSettings use variants extensively, allowing us to read and write item data, database data, and user preferences for any QVariant-compatible type. We already saw an example of this in Chapter 3, where we passed a QRect, a QStringList, and a couple of bools as variants to QSettings::setValue(), and retrieved them later as variants.

It is possible to create arbitrarily complex data structures using QVariant by nesting values of container types:

QMap<QString, QVariant> pearMap;
pearMap["Standard"] = 1.95;
pearMap["Organic"] = 2.25;

QMap<QString, QVariant> fruitMap;
fruitMap["Orange"] = 2.10;
fruitMap["Pineapple"] = 3.85;
fruitMap["Pear"] = pearMap;

Here we have created a map with string keys (product names) and values that are either floating-point numbers (prices) or maps. The top-level map contains three keys: "Orange", "Pear", and "Pineapple". The value associated with the "Pear" key is a map that contains two keys ("Standard" and "Organic"). When iterating over a map that holds variant values, we need to use type() to check the type that a variant holds so that we can respond appropriately.

Creating data structures like this can be very seductive since we can organize the data in any way we like. But the convenience of QVariant comes at the expense of efficiency and readability. As a rule, it is usually worth defining a proper C++ class to store our data whenever possible.

QVariant is used by Qt's meta-object system and is therefore part of the QtCore module. Nonetheless, when we link against the QtGui module, QVariant can store GUI-related types such as QColor, QFont, QIcon, QImage, and QPixmap:

QIcon icon("open.png");
QVariant variant = icon;

To retrieve the value of a GUI-related type from a QVariant, we can use the QVariant::value<T>() template member function as follows:

QIcon icon = variant.value<QIcon>();

The value<T>() function also works for converting between non-GUI data types and QVariant, but in practice we normally use the to...() conversion functions (e.g., toString()) for non-GUI types.

QVariant can also be used to store custom data types, assuming they provide a default constructor and a copy constructor. For this to work, we must first register the type using the Q_DECLARE_METATYPE() macro, typically in a header file below the class definition:

Q_DECLARE_METATYPE(BusinessCard)

This enables us to write code such as this:

BusinessCard businessCard;
QVariant variant = QVariant::fromValue(businessCard);
...
if (variant.canConvert<BusinessCard>()) {
    BusinessCard card = variant.value<BusinessCard>();
    ...
}

Because of a compiler limitation, these template member functions are not available for MSVC 6. If you need to use this compiler, use the qVariantFromValue(), qVariantValue<T>(), and qVariantCanConvert<T>() global functions instead.

If the custom data type has << and >> operators for writing to and reading from a QDataStream, we can register them using qRegisterMetaTypeStreamOperators<T>(). This makes it possible to store preferences of custom data types using QSettings, among other things. For example:

qRegisterMetaTypeStreamOperators<BusinessCard>("BusinessCard");

This chapter focused on the Qt containers, as well as on QString, QByteArray, and QVariant. In addition to these classes, Qt also provides a few other containers. One is QPair<T1, T2>, which simply stores two values and is similar to std::pair<T1, T2>. Another is QBitArray, which we will use in the first section of Chapter 21. Finally, there is QVarLengthArray<T, Prealloc>, a low-level alternative to QVector<T>. Because it preallocates memory on the stack and isn't implicitly shared, its overhead is less than that of QVector<T>, making it more appropriate for tight loops.

Qt's algorithms, including a few not covered here, such as qCopyBackward() and qEqual(), are described in Qt's documentation at http://doc.trolltech.com/4.3/qtalgorithms.html. And for more details of Qt's containers, including information on their time complexity and growth strategies, see http://doc.trolltech.com/4.3/containers.html.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020