2

Coming from a C# background, I have some habbits that need to be changed. I'm tring to return a QList<int> from a function. The compiler error message is conversion from 'QList*' to non-scalar type 'QList' requested. Here is the function:

QList<int> toCategories(QVariant qv)
{
    QList<int>categories = new QList<int>();
    if(qv.isValid() && qv.type() == QVariant::List)
    {
        foreach(QVariant category,qv.toList()){
            categories.append(category.toInt() );
        }
    }
    return categories;
}

I'd appreciate a link to the documentation or a function using the correct syntax

DarwinIcesurfer
  • 1,073
  • 4
  • 25
  • 42

1 Answers1

5

The error is referring to the fact that you are attempting to covert between a non-pointer QList to a pointer to a QList. You need to either use a pointer to a QList as follows:

QList<int>* toCategories(QVariant qv)
{
    QList<int>* categories = new QList<int>();
    if(qv.isValid() && qv.type() == QVariant::List)
    {
        foreach(QVariant category,qv.toList()){
            categories->append(category.toInt() );
        }
        return categories;
    }
}

Alternately, you can use a non-pointer QList:

QList<int> toCategories(QVariant qv)
{
    QList<int> categories;
    if(qv.isValid() && qv.type() == QVariant::List)
    {
        foreach(QVariant category,qv.toList()){
            categories.append(category.toInt() );
        }
        return categories;
    }
}

Since you are new to C++, I would recommend reviewing this question.

If I may also point out, your function as listed has a problem where a certain path doesn't return a value (though I suspect that might be because you only copy-pasted a portion of your code).

Community
  • 1
  • 1
RA.
  • 7,542
  • 1
  • 34
  • 35
  • Thanks. So in general if I use the 'new' keyword the storage will be created on the heap and I need to use a pointer to reference that storage location? (Yes.. I cut and pasted code and put the return in the wrong place. It should have been a couple line lower... I'll edit the question) – DarwinIcesurfer Nov 06 '12 at 03:14
  • @DarwinIcesurfer Yes, that's generally correct, though it's more complicated than that. Look into tutorials/examples of the dereferencing operator, address-of operator, and the use of values, references and pointers. It may also be worth looking into pointer arithmetic. – RA. Nov 06 '12 at 03:21
  • 1
    Qt has agressive rvalue support since 4.8, so second variant is much more safer and even faster than first. Because categories var will be moved (not copyed) to outside. – Eugene Mamin Nov 06 '12 at 08:39