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. Q_Object for specialization class

Q_Object for specialization class

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 469 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.
  • H Offline
    H Offline
    Hu Junhao
    wrote on last edited by
    #1
    template <typename T>
    class A
    {
        T a;
    };
    template<>
    class A<int> : public QObject
    {
        Q_OBJECT
    };
    

    moc will not generate code for A<int>
    I realize that Q_OBJECT will not generate code for template class. but A<int> is not

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      This limitation is mentioned in the moc documentation.

      https://doc.qt.io/qt-6/moc.html#limitations

      moc does not handle all of C++. The main problem is that class templates cannot have the Q_OBJECT macro. Here is an example:

      class SomeTemplate<int> : public QFrame
      {
          Q_OBJECT
          ...
      
      signals:
          void mySignal(int);
      };
      

      Asking a question about code? http://eel.is/iso-c++/testcase/

      H 1 Reply Last reply
      0
      • jeremy_kJ jeremy_k

        This limitation is mentioned in the moc documentation.

        https://doc.qt.io/qt-6/moc.html#limitations

        moc does not handle all of C++. The main problem is that class templates cannot have the Q_OBJECT macro. Here is an example:

        class SomeTemplate<int> : public QFrame
        {
            Q_OBJECT
            ...
        
        signals:
            void mySignal(int);
        };
        
        H Offline
        H Offline
        Hu Junhao
        wrote on last edited by Hu Junhao
        #3

        @jeremy_k Any alternative solution?
        More detailed code like this

        enum MagagerType
        {
            Interface,
        };
        
        class Document
        {
            template<MagagerType T>
            friend class Manager;
        };
        
        template <MagagerType T>
        class Manager
        {
        };
        template<>
        class Manager<MagagerType::Interface> : public QObject
        {
            Q_OBJECT
        };
        
        
        C 1 Reply Last reply
        0
        • H Hu Junhao

          @jeremy_k Any alternative solution?
          More detailed code like this

          enum MagagerType
          {
              Interface,
          };
          
          class Document
          {
              template<MagagerType T>
              friend class Manager;
          };
          
          template <MagagerType T>
          class Manager
          {
          };
          template<>
          class Manager<MagagerType::Interface> : public QObject
          {
              Q_OBJECT
          };
          
          
          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @Hu-Junhao What are you trying to achieve with this arrangement?

          H 1 Reply Last reply
          0
          • C ChrisW67

            @Hu-Junhao What are you trying to achieve with this arrangement?

            H Offline
            H Offline
            Hu Junhao
            wrote on last edited by
            #5

            @ChrisW67 As the former code shows, I have a Document class, and serveral Manager classes, all Manager classes is friend class of Document. What I have tried to achieve is to make as few changes as possible to the Document class。

            Christian EhrlicherC 1 Reply Last reply
            0
            • H Hu Junhao

              @ChrisW67 As the former code shows, I have a Document class, and serveral Manager classes, all Manager classes is friend class of Document. What I have tried to achieve is to make as few changes as possible to the Document class。

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

              And why do you need a Q_OBJECT macro then? As already said it does not work with templates (and will not).

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

              H 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                And why do you need a Q_OBJECT macro then? As already said it does not work with templates (and will not).

                H Offline
                H Offline
                Hu Junhao
                wrote on last edited by
                #7

                @Christian-Ehrlicher I need signals and slots, maybe there is another solution for me not to write friend declaration every time?

                Christian EhrlicherC 1 Reply Last reply
                0
                • H Hu Junhao

                  @Christian-Ehrlicher I need signals and slots, maybe there is another solution for me not to write friend declaration every time?

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

                  @Hu-Junhao said in Q_Object for specialization class:

                  I need signals and slots

                  Then use the new signal/slot syntax.

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

                  H 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @Hu-Junhao said in Q_Object for specialization class:

                    I need signals and slots

                    Then use the new signal/slot syntax.

                    H Offline
                    H Offline
                    Hu Junhao
                    wrote on last edited by Hu Junhao
                    #9

                    @Christian-Ehrlicher You mean inherits QObject but without macro Q_OBJECT maco? Compile error on

                    Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro<typename SignalType::Object>::Value, "No Q_OBJECT in the class with the signal");
                    
                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      Hu Junhao
                      wrote on last edited by
                      #10

                      Sorry for all, after considering, this is absolutely a fake requirement. Manager Class will always add member to Document class. Avoid changes to Document class is meaningless.

                      1 Reply Last reply
                      0
                      • H Hu Junhao has marked this topic as solved on

                      • Login

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