QT5.3 did not support "forward declaration"??(Solved)



  • Hi Guys

    Run the example project from the book "C++ GUI Programming with Qt4".

    Under QT5 (Qt/Qt5.3.1/Tools/mingw482_32/bin), got many errors like "forward declaration of 'class QLineEdit'"

    Under QT4 ( the same mingw compiler with QT5.3), no errors.

    What happened?



  • @
    09:45:12: Running steps for project find...
    09:45:12: Configuration unchanged, skipping qmake step.
    09:45:12: Starting: "C:\Qt\Qt5.3.1\Tools\mingw482_32\bin\mingw32-make.exe"
    C:/Qt/Qt5.3.1/Tools/mingw482_32/bin/mingw32-make -f Makefile.Debug
    mingw32-make[1]: Entering directory 'D:/qt_test/qt-book/chap02/build-find-Desktop_Qt_5_3_MinGW_32bit-Debug'
    g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I..\find -I"C:\Qt\Qt5.3.1\5.3\mingw482_32\include" -I"C:\Qt\Qt5.3.1\5.3\mingw482_32\include\QtWidgets" -I"C:\Qt\Qt5.3.1\5.3\mingw482_32\include\QtGui" -I"C:\Qt\Qt5.3.1\5.3\mingw482_32\include\QtCore" -I"debug" -I"." -I"C:\Qt\Qt5.3.1\5.3\mingw482_32\mkspecs\win32-g++" -o debug\finddialog.o ..\find\finddialog.cpp
    ..\find\finddialog.cpp: In constructor 'FindDialog::FindDialog(QWidget*)':
    ..\find\finddialog.cpp:8:41: error: invalid use of incomplete type 'class QLabel'
    label = new QLabel(tr("Find &what:"));
    ^
    In file included from ..\find\finddialog.cpp:3:0:
    ..\find\finddialog.h:7:7: error: forward declaration of 'class QLabel'
    class QLabel;
    ^
    ..\find\finddialog.cpp:9:20: error: invalid use of incomplete type 'class QLineEdit'
    lineEdit = new QLineEdit;
    ^
    In file included from ..\find\finddialog.cpp:3:0:
    ..\find\finddialog.h:8:7: error: forward declaration of 'class QLineEdit'
    class QLineEdit;
    ^
    ..\find\finddialog.cpp:10:10: error: invalid use of incomplete type 'class QLabel'
    label->setBuddy(lineEdit);
    ^
    In file included from ..\find\finddialog.cpp:3:0:
    ..\find\finddialog.h:7:7: error: forward declaration of 'class QLabel'
    class QLabel;
    ^
    ..\find\finddialog.cpp:12:51: error: invalid use of incomplete type 'class QCheckBox'
    caseCheckBox = new QCheckBox(tr("Match &case"));
    ^
    In file included from ..\find\finddialog.cpp:3:0:
    ..\find\finddialog.h:6:7: error: forward declaration of 'class QCheckBox'
    class QCheckBox;
    ^
    ..\find\finddialog.cpp:13:60: error: invalid use of incomplete type 'class QCheckBox'
    backwardCheckBox = new QCheckBox(tr("Search &backward"));
    ^
    In file included from ..\find\finddialog.cpp:3:0:
    ..\find\finddialog.h:6:7: error: forward declaration of 'class QCheckBox'
    class QCheckBox;
    ^
    ..\find\finddialog.cpp:15:45: error: invalid use of incomplete type 'class QPushButton'
    findButton = new QPushButton(tr("&Find"));
    ^
    In file included from C:\Qt\Qt5.3.1\5.3\mingw482_32\include\QtWidgets/QDialog:1:0,
    from ..\find\finddialog.h:4,
    from ..\find\finddialog.cpp:3:
    @

    [andreyc EDIT]: Added @ around log messages.



  • widgets are moved to separate lib in called widgets in Qt 5.0 if you are using the pro file directly from Qt 4.0 make the following entry in the pro file.

    QT += widgets.

    Also ensure that #include <QLabel> exist in the header files.



  • Thanks.
    QT += widgets. is already included.
    forward declaration is not supported as:

    //#include <QtGui>
    //class QCheckBox;
    //class QLabel;
    //class QLineEdit;
    //class QPushButton;

    must be replaced by:

    #include <QDialog>
    #include <QLabel>
    #include <QPushButton>
    #include <QLineEdit>
    #include <QCheckBox>
    #include <QHBoxLayout>

    QT4.82 support forward declaration, QT5.3 doesn't.



  • Glad that you solved the problem.

    As a side note a forward declaration is a C++ feature not Qt hence Qt cannot support nor not support it.

    All these classes were indirectly defined in QtGui before Qt5.
    In Qt5 they belong to QtWidgets

    So if you would forward declare class QLabel in a header and then include file QLabel or QtWidgets in a source file everything would work.



  • Great Thanks.
    Yes, use QtWidgets to replace QtGui
    All the rest of include files can be replaced.


  • Lifetime Qt Champion

    Hi,

    On a side note, using module wide include is bad practice since it pulls in every class and thus will slow down compile time. Including the class specific header should be enough.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.