Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, December 2007
QVariant::canConvert(QVariant::List) bug ?


Message 1 in thread

I have some code that does this:

if (column_data.value(column).canConvert(QVariant::StringList)
              ||  column_data.value(column).canConvert(QVariant::List))
{
    std::cout << column_data.value(column).toList().size() << std::endl;

If I run this code with data like this:

column_data["Fred"] = QString("Smith");

(i.e. with a single QString in the appropriate value) then
then the branch shown above is taken, but the list returned
by toList() has a size() of 0.

Is this a bug in QVariant::canConvert() ? Presumably if
canConvert() returns true, than the conversion should
then occur correctly i.e. I should get a list of size 1.

-- 
 [ signature omitted ] 

Message 2 in thread

Stephen Collyer wrote:
>I have some code that does this:
>
>if (column_data.value(column).canConvert(QVariant::StringList)
>
>              ||  column_data.value(column).canConvert(QVariant::List))
>
>{
>    std::cout << column_data.value(column).toList().size() << std::endl;
>
>If I run this code with data like this:
>
>column_data["Fred"] = QString("Smith");
>
>(i.e. with a single QString in the appropriate value) then
>then the branch shown above is taken, but the list returned
>by toList() has a size() of 0.
>
>Is this a bug in QVariant::canConvert() ? Presumably if
>canConvert() returns true, than the conversion should
>then occur correctly i.e. I should get a list of size 1.

It's almost a bug. But your code is wrong. It should be:

if (column_data.value(column).canConvert(QVariant::List))
{
    std::cout << column_data.value(column).toList().size() << std::endl;


I.e., if you are going to call toList(), you have to verify that it can be 
converted to a List. But since you added the test for 
QVariant::StringList too, you're getting variant types that can be 
converted to string lists, but not to variant lists. 

The problem is that a QString is convertible into QStringList and a 
QStringList can be converted to QVariantList. But the direct conversion 
of QString->QVariantList is not allowed in QVariant.

That's what I call "almost a bug": if you can indirectly convert it, you 
should be able to do so directly too. So, yes, it's a shortcoming of the 
QVariant conversion code, but your code is also misusing the API.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 3 in thread

Thiago Macieira wrote:

> It's almost a bug. But your code is wrong. It should be:
> 
> if (column_data.value(column).canConvert(QVariant::List))
> {
>     std::cout << column_data.value(column).toList().size() << std::endl;
> 
> 
> I.e., if you are going to call toList(), you have to verify that it can be 
> converted to a List. But since you added the test for 
> QVariant::StringList too, you're getting variant types that can be 
> converted to string lists, but not to variant lists. 
> 
> The problem is that a QString is convertible into QStringList and a 
> QStringList can be converted to QVariantList. But the direct conversion 
> of QString->QVariantList is not allowed in QVariant.

Yes, I realised this after posting. The two tests should be separated.

> That's what I call "almost a bug": if you can indirectly convert it, you 
> should be able to do so directly too. So, yes, it's a shortcoming of the 
> QVariant conversion code, but your code is also misusing the API.

Right. This is slightly confusing. I think the docs would benefit
from more examples in this area.

-- 
 [ signature omitted ]