Issue of using moc on a custom type in a library
-
Basically, I created a library containing a type:
struct MyCustomType { ... }; Q_DECLARE_METATYPE(MyCustomType);Then in my application, I have a signal "void sendMsg(MyCustomType)" and I qRegisterMetaType<MessageTableData>().
The code compiles and runs ok. But when I try to emit the signal, I got this error:
QObject::connect: Cannot queue arguments of type 'MyCustomType'
(Make sure 'MyCustomType' is registered using qRegisterMetaType().)I did some research. I think for Qt5, a custom type, moc should have a section to automatically register the type like:
void MyApp::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
...
else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
... reinterpret_cast<int>(_a[0]) = qRegisterMetaType< MyCustomType >();
}But I am missing this section in my moc generated file. Is this a known issue for a type in lib and any solution to this? Thanks!
-
Is your type in a namespace?
-
I've done this like:
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(); ~MainWindow(); signals: void sigYourSignal(size_t size, QByteArray ba); } int main (int argc, char *argv[]) { QApplication app(argc, argv); MainWindow mainWin; mainWin.show(); return app.exec(); } MainWindow::MainWindow() { qRegisterMetaType<size_t>("size_t"); qRegisterMetaType<QByteArray>("QByteArray"); }Then you can use this types with your signals as normal parameters.
A.T.
-
@tzma said in Issue of using moc on a custom type in a library:
Better be?
No but this could have been a reason.
Are you sure you really call qRegisterMetaType somehwere before you use it in a signal? -
@tzma said in Issue of using moc on a custom type in a library:
Better be?
No but this could have been a reason.
Are you sure you really call qRegisterMetaType somehwere before you use it in a signal?@Christian-Ehrlicher Yes. I have a valid ID returned from qRegisterMetaType call.
-
I would suggest that you create a simple main.cpp with a queued connection of your type to see if it occours there too. This has also the advantage that we can try to reproduce it.