0

So I have a custom class Foo which has been registered as a metatype using the Q_DECLARE_METATYPE(Foo) macro at the end of the class definition.

I can set items within a list, check to see if canConvert, but when I try to actually make an item of type Foo things fail at compile time.

Using QVariant passed in from a QModelIndex &index:

Foo item(index.data(Qt::DisplayRole).value<Foo>())

fails with the following error(s):

no matching function for call to 'namespace::Foo::Foo(namespace::Foo)'
In instantiation of 'T qvariant_cast(const Foo&) [with T = namesapce::Foo]':
required from 'T QVariant::value() const [with T = namespace::Foo]'
no matching function for call to 'namespace::Foo::Foo(const namespace::Foo &)'
no matching function for call to 'namespace::Foo::Foo(const namespace::Foo&)'    
no matching function for call to 'namespace::Foo::Foo(const namespace::Foo)'
In member function 'T QVariant::value() const [with T = namespace::Foo]'

all from file qvariant.h

What am I doing wrong here? My class has the following constructors:

Foo::Foo(const Foo &)
Foo::Foo()

The header is as follows:

namespace a {
namespace b {
class Foo {
    explicit Foo();
    explicit Foo(const Foo &a);
    ...
};
} // b
} // a
Q_DECLARE_METATYPE(a::b::Foo)
ElCraneo
  • 495
  • 1
  • 6
  • 11
  • 1
    show your header, where you placed Q_DECLARE_METATYPE(Foo) – ixSci Feb 20 '13 at 17:06
  • It was originally a subclass of QVariant, but that didn't work either. – ElCraneo Feb 20 '13 at 18:09
  • 2
    It might be the use of `explicit` on those constructors that's causing the problem. Also, just to make sure, did you call `qRegisterMetaType():`? – Dan Milburn Feb 20 '13 at 18:45
  • Removing the `explicit` keyword caused it to work correctly. Any idea why that was happening? I want to toss you the answer credit, but I can't do that on comments. – ElCraneo Feb 20 '13 at 18:59
  • http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean I'd suppose an implicit conversion is being made? – Thadeux Feb 20 '13 at 20:44

1 Answers1

0

As Dan Milburn suggested, removing the explicit keywords solved the problem. It appears an implicit conversion was occurring in the value call, therefore having only explicit constructors prevented this.

ElCraneo
  • 495
  • 1
  • 6
  • 11