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. Forward declarations for objects that are not widgets
QtWS25 Last Chance

Forward declarations for objects that are not widgets

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.2k 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.
  • I Offline
    I Offline
    isaacEnrique
    wrote on last edited by
    #1

    Greetings.

    I'm developing a GUI tool and I am using forward declarations for widgets in my class (QPushButton, QToolButtons, QLabel, ect). I understand that use forward declarations reduces compile time, so I use them whenever I can, is this an error?.

    Based on the above, my question is whether it is desirable to use forward declarations to data members that are not widgtes (QVector, QList, QRect, QImage, ect)?. Particularly, I need you in my class one QVector<QRectF> ... What is better, declare a member variable of that type directly or use forward declarations and pointers?

    Thanks in advance for any help and / or suggestions.

    Isaac Pérez
    Programming is understanding.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Any class which you would like to use should be included in with #include <QPushButton>. It is about resolving usage of particular class. If you are using the QVector, QImage etc, you should include them in header file.

      In general if you have class and using it, defined else where you should include. Forward declaration is generally used when you are writing your own class in and whose definition is somewhere else.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • I Offline
        I Offline
        isaacEnrique
        wrote on last edited by
        #3

        I have seen in several places (including the Qt examples that come with QtCreator) using forward declarations for Qt classes themselves. Sometimes directly:

        class QLabel;
        class QPushButton;

        and sometimes using the macro QT_FORWARD_DECLARE_CLASS(). Member variables using pointers is then declared.

        According to what I've read, the reason is that (among other things) reduce the time of compilation of programs.

        However, I've only seen it used for widgets, and so I have doubts about whether it is advisable to use forward declarations for other classes (no only widgets classes). Common sense makes me think that for those other classes also results in a reduction in compile time, however, I don't know consider whether that reduction in the time of compilation worthwhile for all cases, for example, using forward declarations, I will have QVector* instead of QVector.

        If I properly understood what you told me, you suggest me to forget completely forward declarations and include in my header files all I need (<QLabel>, <QPushButton>, ect)... Or am I wrong?

        Isaac Pérez
        Programming is understanding.

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          Hi,

          Forward declarations become useful when you are developing a very large project where compilation can take many minutes (or even hours). However, you can't really feel any benefits when your project is small.

          Remember also: Forward declarations save you compilation time, but costs you editing time. You need to write _class MyClass;_in the .h file and _#include "myclass.h"_ in the .cpp file. If you later decide that you don't want to use MyClass anymore, you'll need to remove 2 lines from 2 files.

          [quote]for example, using forward declarations, I will have QVector* instead of QVector.[/quote]Do NOT use pointers just so that you can use forward declarations. You should design your classes first, and THEN see which components can be forward-declared. Not everything should be forward-declared.

          In particular, Qt's container classes (QVector, QString, QByteArray, etc.) are value-based classes; you rarely need to use pointers with them.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cincirin
            wrote on last edited by
            #5

            You can use forward declaration for variables only for pointers and references. You must include the header if variable is not declared as pointer or reference. Besides compilation speed forward declaration is used for mutual recursion.

            1 Reply Last reply
            0

            • Login

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