Consider the following minimal example:
namespace foo { struct MyStruct {}; }
Q_DECLARE_METATYPE(foo::MyStruct)
namespace bar { struct MyStruct {}; }
Q_DECLARE_METATYPE(bar::MyStruct)
// later on near the entry point of the program:
qRegisterMetaType<foo::MyStruct>("MyStruct");
qRegisterMetaType<bar::MyStruct>("MyStruct");
This code potentially leads to a crash. When a slot is invoked that accepts const foo::MyStruct&
as an argument, Qt's meta object system provides const bar::MyStruct&
instead due to alias ambiguity.
I know that the obvious fix is to change MyStruct
's name to something else to avoid the clash entirely. The problem there is that I suspect I shouldn't have to do that, and under certain conditions I may not even be able to do that (imagine both structs provided by independent plugins that are unaware of each other).
That being said, I have a couple of questions:
I tried changing both aliases to contain namespace qualifiers, e.g.
qRegisterMetaType<foo::MyStruct>("foo::MyStruct")
, but then Qt silently behaves as if I had not registered the meta type at all. This seems like a clean solution to me. Why is that not permitted? And why do I get no warnings?If I do a twist on the previous approach and change the aliases to something like
foo_MyStruct
,fooMyStruct
orMyStruct2
, or even do not pass any alias to the function, this doesn't seem to work either. Does the alias argument have to be present and match the symbol name exactly?What other options do I have? (short of renaming the structs)
Cheers!