2017-06-21 7 views
2

qRegisterMetaType()を使用してクラスを登録する必要があり、Q_COREAPP_STARTUP_FUNCTIONを使用したかったです。Q_COREAPP_STARTUP_FUNCTION with static class memberメソッド

私は(静的にリンクされていない)ライブラリでこれが必要なので、main()に登録したくありません。

私はこれに複数のケースがあり、ルート名前空間を汚染したくありません。コンパイラは同じ名前の複数のメソッドを必要とせず、新しいメソッドを追加するたびに一意のメソッド名について考える必要はありません。

したがって、私のクラスの静的メンバーメソッド!

しかし、この例ではコンパイルされません:

MyClass::registerMetaType() {} 

Q_COREAPP_STARTUP_FUNCTION(MyClass::registerMetaType) 

は、なぜ私は、静的メンバメソッドを使用することはできませんし、これが正しくない場合:.cppファイル内implementionと

class MyClass { 
public: 
    // ... 
    static void registerMetaType(); 
} 

これを解決する方法は、より良いものは何ですか?

UPDATE コンパイラエラーメッセージ:

/path/to/myclass.cpp:183:1: error: no ‘void MyClass::registerMetaType_ctor_function()’ member function declared in class ‘MyClass’ 
Q_COREAPP_STARTUP_FUNCTION(MyClass::registerMetaType) 
^ 
In file included from /path/to/qt5-5.6.0/include/QtCore/QtGlobal:1:0, 
       from /path/to/myclass.h:18, 
       from /path/to/myclass.cpp:15: 
/path/to/myclass.cpp:183:1: error: qualified name does not name a class before ‘{’ token 
Q_COREAPP_STARTUP_FUNCTION(MyClass::registerMetaType) 
^ 
/path/to/myclass.cpp:183:1: error: invalid type in declaration before ‘;’ token 
Q_COREAPP_STARTUP_FUNCTION(MyClass::registerMetaType) 
^ 
/path/to/myclass.cpp:183:1: error: definition of ‘MyClass::registerMetaType_ctor_function_ctor_instance_’ is not in namespace enclosing ‘MyClass’ [-fpermissive] 
/path/to/myclass.cpp:183:1: error: ‘static’ may not be used when defining (as opposed to declaring) a static data member [-fpermissive] 
/path/to/myclass.cpp:183:1: error: ‘const int MyClass::registerMetaType_ctor_function_ctor_instance_’ is not a static member of ‘class MyClass’ 
/path/to/myclass.cpp:183:28: error: uninitialized const ‘MyClass::registerMetaType_ctor_function_ctor_instance_’ [-fpermissive] 
Q_COREAPP_STARTUP_FUNCTION(MyClass::registerMetaType) 
+0

あなたはコンパイラエラーを投稿することができますか?おそらく 'Q_COREAPP_STARTUP_FUNCTION(&MyClass :: registerMetaType)'が必要でした。 – drescherjm

+0

'&'の有無にコンパイルエラーがあります。私の質問の更新を参照してください。 –

答えて

0

私はQtはメンバ関数のサポートを追加するための機能要求を報告している。問題は、静的メンバ関数で::であるよう https://bugreports.qt.io/browse/QTBUG-66902

が見えるとマクロの使用。

とにかく、「汚染ルート名前空間」を克服し、シンボルを複製する別の解決策は、名前空間を使用することです:

namespace { // no duplicate symbol across units 
namespace detail { // avoid "polluting" global namespace of this .cpp file 

    void registerMetaTypes() { 
     qRegisterMetaType(MyClass*); 
    } 

    Q_COREAPP_STARTUP_FUNCTION(registerMetaTypes) 
} // namespace detail 
} // anonymous namespace