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. Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.

Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.

Scheduled Pinned Locked Moved Unsolved General and Desktop
macromacrossdklibraryprivate api
11 Posts 4 Posters 2.5k 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.
  • D Dariusz
    4 Jul 2021, 22:26

    Hey

    I'm trying to learn how Qt is build so it has a private&public classes/headers that are able to inherit private-from-private / public-from-public and "work".

    I've gone over https://community.kde.org/Policies/Library_Code_Policy and https://techbase.kde.org/Policies/Library_Code_Policy/Shared_D-Pointer_Example

    But as far as I can tell... that is a little "outdated" and I'm not sure what is the current way of doing it.

    What I understand is that I have to declare Q_DECLAER_PRIVATE(myClassName) in public header and in private header declare Q_DECLARE_PUBLIC(myClassName)

    So that "appear" to work, but then when I try to compile I get > error C2065: 'q_ptr': undeclared identifier in my private header.

    I can also see Q_DECLARE_PRIVATE_D but I've no idea what this one do o.o.
    Is that meant to be used by inherited classes? So say if I do :

    class myGraphicsItem : public QGraphicsItem {
    Q_DECLARE_PRIVATE_D(QGraphicsItem ::d_ptr.data(), myGraphicsItem )
    }
    

    In which case, if I inherit from more than 1 class say >

    class myGraphicsItem : public QObject, public QGraphicsItem{
    Q_DECLARE_PRIVATE_D(QObject::d_ptr.data(), myGraphicsItem )
    Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), myGraphicsItem )
    }
    

    is this correct ?

    How can I learn about this black magic O_O ?

    TIA
    Regards
    Dariusz

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 5 Jul 2021, 05:26 last edited by
    #2

    @Dariusz Check this: https://wiki.qt.io/D-Pointer

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    D 1 Reply Last reply 5 Jul 2021, 08:41
    3
    • J jsulm
      5 Jul 2021, 05:26

      @Dariusz Check this: https://wiki.qt.io/D-Pointer

      D Offline
      D Offline
      Dariusz
      wrote on 5 Jul 2021, 08:41 last edited by
      #3

      @jsulm thats awesome thank you!
      But still stuck :- D
      I made an example project > https://github.com/Dariusz1989/dPointerLearn
      This currently gives me >

      error C2143: syntax error: missing ';' before '*'
      error C7524: 'inline' specifier cannot appear on a block-scope declaration or non-static data member
      error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
      error C2334: unexpected token(s) preceding '{'; skipping apparent function body
      error C2327: 'extra::controllers::inheritedPrivate::inherited': is not a type name, static, or enumerator
      

      I'm lost :- (
      What do I do ?

      J 1 Reply Last reply 5 Jul 2021, 08:51
      0
      • D Dariusz
        5 Jul 2021, 08:41

        @jsulm thats awesome thank you!
        But still stuck :- D
        I made an example project > https://github.com/Dariusz1989/dPointerLearn
        This currently gives me >

        error C2143: syntax error: missing ';' before '*'
        error C7524: 'inline' specifier cannot appear on a block-scope declaration or non-static data member
        error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
        error C2334: unexpected token(s) preceding '{'; skipping apparent function body
        error C2327: 'extra::controllers::inheritedPrivate::inherited': is not a type name, static, or enumerator
        

        I'm lost :- (
        What do I do ?

        J Offline
        J Offline
        JonB
        wrote on 5 Jul 2021, 08:51 last edited by
        #4

        @Dariusz
        You should state which file and which line number the first error occurs on.

        D 1 Reply Last reply 5 Jul 2021, 09:12
        0
        • J JonB
          5 Jul 2021, 08:51

          @Dariusz
          You should state which file and which line number the first error occurs on.

          D Offline
          D Offline
          Dariusz
          wrote on 5 Jul 2021, 09:12 last edited by
          #5

          @JonB said in Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.:

          @Dariusz
          You should state which file and which line number the first error occurs on.

          ops, my bad >

          inherited_p.h(18): error C2143: syntax error: missing ';' before '*'
          inherited_p.h(18): error C7524: 'inline' specifier cannot appear on a block-scope declaration or non-static data member
          inherited_p.h(18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
          inherited_p.h(18): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
          inherited_p.h(20): error C2327: 'extra::controllers::inheritedPrivate::inherited': is not a type name, static, or enumerator
          

          All of it point to

                      Q_DECLARE_PUBLIC(inherited);
          

          and

                      inheritedPrivate(inherited *item);
          

          TIA!

          J 1 Reply Last reply 5 Jul 2021, 09:16
          0
          • D Dariusz
            5 Jul 2021, 09:12

            @JonB said in Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.:

            @Dariusz
            You should state which file and which line number the first error occurs on.

            ops, my bad >

            inherited_p.h(18): error C2143: syntax error: missing ';' before '*'
            inherited_p.h(18): error C7524: 'inline' specifier cannot appear on a block-scope declaration or non-static data member
            inherited_p.h(18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
            inherited_p.h(18): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
            inherited_p.h(20): error C2327: 'extra::controllers::inheritedPrivate::inherited': is not a type name, static, or enumerator
            

            All of it point to

                        Q_DECLARE_PUBLIC(inherited);
            

            and

                        inheritedPrivate(inherited *item);
            

            TIA!

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 5 Jul 2021, 09:16 last edited by
            #6

            @Dariusz said in Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.:

            Q_DECLARE_PUBLIC(inherited);

            Remove ;

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D J 2 Replies Last reply 5 Jul 2021, 09:35
            1
            • J jsulm
              5 Jul 2021, 09:16

              @Dariusz said in Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.:

              Q_DECLARE_PUBLIC(inherited);

              Remove ;

              D Offline
              D Offline
              Dariusz
              wrote on 5 Jul 2021, 09:35 last edited by Dariusz 7 May 2021, 09:36
              #7

              @jsulm said in Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.:

              @Dariusz said in Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.:

              Q_DECLARE_PUBLIC(inherited);

              Remove ;

              Hmmm, now why? As far as I can tell QWidget_p.h has it declared?

              also, it appears to "work" I've pushed git for the public.

              Ok I got the Q_DECLARE_PUBLIC working as well, looks like I had circular include there.

              1 Reply Last reply
              0
              • J jsulm
                5 Jul 2021, 09:16

                @Dariusz said in Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.:

                Q_DECLARE_PUBLIC(inherited);

                Remove ;

                J Offline
                J Offline
                JonB
                wrote on 5 Jul 2021, 09:48 last edited by
                #8

                @jsulm said in Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.:

                Remove ;

                OOI, why?! https://code.woboq.org/qt5/qtbase/src/corelib/global/qglobal.h.html#_M/Q_DECLARE_PUBLIC

                #define Q_DECLARE_PUBLIC(Class)                                    \
                    inline Class* q_func() { return static_cast<Class *>(q_ptr); } \
                    inline const Class* q_func() const { return static_cast<const Class *>(q_ptr); } \
                    friend class Class;
                

                So it gets an extra ; at the end, so what? :)

                D 1 Reply Last reply 5 Jul 2021, 09:56
                0
                • J JonB
                  5 Jul 2021, 09:48

                  @jsulm said in Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.:

                  Remove ;

                  OOI, why?! https://code.woboq.org/qt5/qtbase/src/corelib/global/qglobal.h.html#_M/Q_DECLARE_PUBLIC

                  #define Q_DECLARE_PUBLIC(Class)                                    \
                      inline Class* q_func() { return static_cast<Class *>(q_ptr); } \
                      inline const Class* q_func() const { return static_cast<const Class *>(q_ptr); } \
                      friend class Class;
                  

                  So it gets an extra ; at the end, so what? :)

                  D Offline
                  D Offline
                  Dariusz
                  wrote on 5 Jul 2021, 09:56 last edited by
                  #9

                  @JonB said in Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.:

                  @jsulm said in Learning & Undertanding Q_DECLARE_PRIVATE/PUBLIC macros/sdk.:

                  Remove ;

                  OOI, why?! https://code.woboq.org/qt5/qtbase/src/corelib/global/qglobal.h.html#_M/Q_DECLARE_PUBLIC

                  #define Q_DECLARE_PUBLIC(Class)                                    \
                      inline Class* q_func() { return static_cast<Class *>(q_ptr); } \
                      inline const Class* q_func() const { return static_cast<const Class *>(q_ptr); } \
                      friend class Class;
                  

                  So it gets an extra ; at the end, so what? :)

                  oh you said to remove ; uhh ok, my bad :D

                  Ok so ! Multiple inheritance!

                  As far as I can tell when it comes to Q_DECLARE_PRIVATE_D I have to specify which base -private class I want to inherit. As it appears that I can only grab one.

                          Q_DECLARE_PRIVATE_D(QObject::d_ptr.data(), inherited);
                          //Q_DECLARE_PRIVATE_D(core::base::baseClass::d_ptr, inherited); // I can only inherit 1 base class?
                  

                  With the code above, I can access QObject members, but core::base::baseClass are invalid. Where as if I swap the // around, and use baseClass as target, then I can access baseClass members, but QObject are invalid then.

                  I take in this case... I would have to make a "new" macro, something like Q_DECLARE_PRIVATE2_D & Q_DECLARE_PRIVATE3_D &Q_DECLARE_PRIVATE4_D etc etc & have multi base ptr storage as well like

                              baseClassPrivate *d_ptr;
                              baseClassPrivate *d_ptr2;
                              baseClassPrivate *d_ptr3;
                              baseClassPrivate *d_ptr4;
                  

                  At which point I should look in to map logic and more complex macro. As well as my inherited(inheritedPrivate &d);would need to be more like inherited(QMap<QString,inheritedPrivate >&d); or something like that.

                  So that when I do Q_D / Q_Q I should be able to specify which baseClass source I want to use ?

                  I take Qt is fairly "strict" when it comes to sdk/d-pointers and thus there are no multi-inheritances issues. However if I want to inherit form both Qt & my own base class then I need to solve it ?

                  TIA

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    Dariusz
                    wrote on 5 Jul 2021, 15:34 last edited by
                    #10

                    Welp I hit bu bu.
                    When I do

                    Q_D(const myCLass)
                    d->doStuff();
                    

                    I get

                    Exception: Exception 0xc0000005 encountered at address 0x7ff62fe17f70: User-mode data execution prevention (DEP) violation at location 0x7ff62fe17f70
                    

                    It happens on d->doStuff();

                    but if I do

                    auto d = static_cast<myClassPrivate*>(d_ptr);
                    d->doStuff();
                    

                    then it all works :D SHISH!

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sierdzio
                      Moderators
                      wrote on 6 Jul 2021, 05:41 last edited by
                      #11

                      If doStuff() is not const, then don't declare d pointer as const:

                      Q_D(myClass) // NO "const" here!
                      d->doStuff();
                      

                      (Z(:^

                      1 Reply Last reply
                      3

                      11/11

                      6 Jul 2021, 05:41

                      • Login

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