Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issue of using moc on a custom type in a library
Forum Updated to NodeBB v4.3 + New Features

Issue of using moc on a custom type in a library

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 613 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tzma
    wrote on last edited by VRonin
    #1

    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!

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Is your type in a namespace?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tzma
        wrote on last edited by
        #3

        No it's not in a namespace. Better be?

        Christian EhrlicherC 1 Reply Last reply
        0
        • T Offline
          T Offline
          TheoSys
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • T tzma

            No it's not in a namespace. Better be?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            T 1 Reply Last reply
            1
            • T Offline
              T Offline
              tzma
              wrote on last edited by
              #6

              Hi A.T. Do you mean to treat the custom type as a QByteArray? And what's the purpose of size since QByteArray has size()/length() already?

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @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?

                T Offline
                T Offline
                tzma
                wrote on last edited by
                #7

                @Christian-Ehrlicher Yes. I have a valid ID returned from qRegisterMetaType call.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tzma
                    wrote on last edited by
                    #9

                    Thanks for the suggestion. I simplified the code quite a bit and the problem went away.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved