Important: Please read the Qt Code of Conduct -

QFont error on compile time

  • What I want to have is for each custom text object to have it's only QFont. Why is this not allowed in the header file or in a constructor?

        QFont font;

    Error given is:
    element.cpp:37:107: error: variable has incomplete type 'QFont'
    qmetatype.h:2090:1: note: forward declaration of 'QFont'
    qmetatype.h:155:18: note: expanded from macro 'QT_FOR_EACH_STATIC_GUI_CLASS'

    Why is this allowed?

        QFont* font;

  • @jkwok678
    For a start you need to #include <QFont> to allow for QFont f.

    OTOH, it's enough to go class QFont;, with no #include, to be allowed to go QFont *f.

    My guess is your order of includes put you in the second situation, doubtless from the

    qmetatype.h:2090:1: note: forward declaration of 'QFont'

  • Lifetime Qt Champion

    @jkwok678 said in QFont error on compile time:

    Why is this allowed?

    C++ basics - the compiler needs to know the size of the type when it sees it. Since the size of a pointer is known, forward declaring a pointer is possible.

Log in to reply