Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

where to #including QtObjects ? (Forward declaration)



  • Hi !!..

    I saw that could not possible to use forward declaration the way:

    //MyHeader.h
    class X{
    class QPushButton * getMyButton();
    };
    
    //Source
    #include "MyHeader.h"
    #include <QPushButton> 
    QPushButton * X::getMyButton(){
           return new QPushButton;
    }
    

    It gives me the error "Return type of out-of-line definition of X::getMyButton() differs from that in the declaration"

    If I remove the "class" word and include the QPushButton directly on header, the problem goes away.... So this points me to a question...
    Do I´m forced to #include all my QtObjects inside the header file ?... or is there any way that I can do forward declaration in Qt ?..

    Thanks so much...


  • Lifetime Qt Champion

    Hi
    When use pointers to widgets, you can forward like

    class QPushButton ; // forward
    
    //MyHeader.h
    class X{
    QPushButton * getMyButton();
    };
    
    


  • Good, thanks friend...


Log in to reply