Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I examine the first character of a QString?

Tags:

c++

qt

I want the following code to remove a leading zero from a price (0.00 should be cut to .00)

QString price1 = "0.00";
if( price1.at( 0 ) == "0" ) price1.remove( 0 );

This gives me the following error: "error: conversion from ‘const char [2]’ to ‘QChar’ is ambiguous"

like image 929
Wes Avatar asked Dec 06 '11 18:12

Wes


People also ask

How do you initialize QString?

Generally speaking: If you want to initialize a QString from a char*, use QStringLiteral . If you want to pass it to a method, check if that method has an overload for QLatin1String - if yes you can use that one, otherwise fall back to QStringLiteral .

How do I check if QString is empty?

QString() creates a string for which both isEmpty() and isNull() return true . A QString created using the literal "" is empty ( isEmpty() returns true ) but not null ( isNull() returns false ). Both have a size() / length() of 0.

How do you split a QString?

To break up a string into a string list, we used the QString::split() function. The argument to split can be a single character, a string, or a QRegExp. To concatenate all the strings in a string list into a single string (with an optional separator), we used the join() function.

What is a QString?

QString stores unicode strings. By definition, since QString stores unicode, a QString knows what characters it's contents represent. This is in contrast to a C-style string (char*) that has no knowledge of encoding by itself.


2 Answers

The main issue is that Qt is seeing "0" as a null-terminated ASCII string, hence the compiler message about const char[2].

Also, QString::remove() takes two arguments. So you code should be:

if( price1.at( 0 ) == '0' ) price1.remove( 0, 1 );

This builds and runs on my system (Qt 4.7.3, VS2005).

like image 52
Gnawme Avatar answered Sep 24 '22 19:09

Gnawme


Try this:

price1.at( 0 ) == '0' ?
like image 38
Ghita Avatar answered Sep 26 '22 19:09

Ghita