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. Header files and general questions.
Forum Updated to NodeBB v4.3 + New Features

Header files and general questions.

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 6 Posters 10.1k Views 1 Watching
  • 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.
  • U Uberlinc

    Okay... so, I got the code to work.
    I replaced, what was in the book as:

    class QCheckBox;
    class QLabel;
    class QLineEdit;
    class QPushButton;

    with:

    #include <QPushButton>
    #include <QLabel>
    #include <QLineEdit>
    #include <QCheckBox>
    #include <QBoxLayout>

    Is this likely to be a mis-print in the original code in the book or has Qt5 changed from Qt4 such that this was correct for the previous version?

    Thanks.

    aha_1980A Offline
    aha_1980A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on last edited by
    #11

    @Uberlinc it is still very common to have the class forward declarations in the headers and the includes only in the C++ files as it massively speeds up compiling.

    as a requirement, you can only use pointers or references ro the forward declared classes in the header file.

    this is nothing Qt specific but common C++ practice.

    Qt has to stay free or it will die.

    1 Reply Last reply
    3
    • U Uberlinc

      Okay, so I've started on the "C++ GUI Programming with Qt4, Second Edition" book by Jasmin Blanchette and Mark Summerfield."

      Trying to write out the code and already hitting issues.

      Not sure if it is simply typos by me (possible, but I've gone over the code and can't find them!) or is Qt4 different sufficiently that even the simplest of code needs some tweaking to make it work?

      I can post the code I'm trying to use here, and maybe someone can see if it's commensurate with Qt 5 that I am using?

      If I don't know anything about the language, how can I pick up where the language needs changing for parts where the book is outdated?

      Thanks.

      Uberlinc.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #12

      @Uberlinc said in Header files and general questions.:

      Not sure if it is simply typos by me (possible, but I've gone over the code and can't find them!) or is Qt4 different sufficiently that even the simplest of code needs some tweaking to make it work?

      The one major difference that will likely impact you is that widgets used to be in the Qt GUI module in Qt 4. Now, they are in their own Qt Widgets module in Qt 5.

      Setting up the project is a bit different, but after that's done the code should still be mostly compatible.

      how can I pick up where the language needs changing for parts where the book is outdated?

      The changes are described at http://doc.qt.io/qt-5/portingguide.html. For the beginning parts of the book, you probably won't be affected by anything described here except for the Qt Widgets module change.

      @Uberlinc said in Header files and general questions.:

      I replaced, what was in the book as:

      class QCheckBox;
      class QLabel;
      class QLineEdit;
      class QPushButton;

      with:

      #include <QPushButton>
      #include <QLabel>
      #include <QLineEdit>
      #include <QCheckBox>
      #include <QBoxLayout>

      Is this likely to be a mis-print in the original code in the book or has Qt5 changed from Qt4 such that this was correct for the previous version?

      If you examine the book in depth, you will probably find that class QCheckBox; is written in the .h file while #include <QCheckBox> is written in the .cpp file.

      As @aha_1980 mentioned, the former is called a forward declaration. See the following for more info on how to use them and why:

      • https://stackoverflow.com/questions/9906402/should-one-use-forward-declarations-instead-of-includes-wherever-possible
      • https://stackoverflow.com/questions/4757565/what-are-forward-declarations-in-c
      • https://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration

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

      1 Reply Last reply
      5
      • U Offline
        U Offline
        Uberlinc
        wrote on last edited by
        #13

        I've read up a fair bit about forward declarations, and also re-read over the chapter from which I am working.
        The chapter does indeed mention forward declarations, but I'm still fuzzy as to why the code, as they define it, did not work.

        If I include the forward declarations of:
        class QCheckBox;
        class QLabel;
        class QLineEdit;
        class QPushButton;

        ... that's all well and good, but it doesn't answer where the code for these classes is defined.
        If you don't include <QCheckbox> ..... <QPushbutton> then where are they kept?

        The code worked once I commented out these forward declarations and simply included the include file for each of them, but I understand that this is a cumbersome way of doing it.

        Where else is the full class definition of the above classes if not in its own QFunction include file?

        If they are included in another, larger include file that the code has already specified, then why didn't the code work?
        The only include files in the actual specified code are QDialog, QtGUI and QApplication.

        Thanks for your help.

        Regards,

        Uberlinc.

        aha_1980A JKSHJ 2 Replies Last reply
        0
        • U Uberlinc

          I've read up a fair bit about forward declarations, and also re-read over the chapter from which I am working.
          The chapter does indeed mention forward declarations, but I'm still fuzzy as to why the code, as they define it, did not work.

          If I include the forward declarations of:
          class QCheckBox;
          class QLabel;
          class QLineEdit;
          class QPushButton;

          ... that's all well and good, but it doesn't answer where the code for these classes is defined.
          If you don't include <QCheckbox> ..... <QPushbutton> then where are they kept?

          The code worked once I commented out these forward declarations and simply included the include file for each of them, but I understand that this is a cumbersome way of doing it.

          Where else is the full class definition of the above classes if not in its own QFunction include file?

          If they are included in another, larger include file that the code has already specified, then why didn't the code work?
          The only include files in the actual specified code are QDialog, QtGUI and QApplication.

          Thanks for your help.

          Regards,

          Uberlinc.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #14

          @Uberlinc said in Header files and general questions.:

          I've read up a fair bit about forward declarations, and also re-read over the chapter from which I am working.
          The chapter does indeed mention forward declarations, but I'm still fuzzy as to why the code, as they define it, did not work.

          If I include the forward declarations of:
          class QCheckBox;
          class QLabel;
          class QLineEdit;
          class QPushButton;

          ... that's all well and good, but it doesn't answer where the code for these classes is defined.
          If you don't include <QCheckbox> ..... <QPushbutton> then where are they kept?

          Well, they are kept in their header files, but at the point of forward declaration the compiler just knows that a class QCheckBox exists. Nothing more! you can't do more with the class at this point than declare a pointer to it. that is important to understand.

          The code worked once I commented out these forward declarations and simply included the include file for each of them,

          as said, to use the class, you need the include. but that's typically in the C++ file only, not in the header.

          but I understand that this is a cumbersome way of doing it.

          it is at least as legal to do so as to do fwd-declaration. you will see both in the wild.

          Where else is the full class definition of the above classes if not in its own QFunction include file?

          indeed.

          If they are included in another, larger include file that the code has already specified, then why didn't the code work?
          The only include files in the actual specified code are QDialog, QtGUI and QApplication.

          ????

          Thanks for your help

          you're welcome!

          Qt has to stay free or it will die.

          1 Reply Last reply
          4
          • U Uberlinc

            I've read up a fair bit about forward declarations, and also re-read over the chapter from which I am working.
            The chapter does indeed mention forward declarations, but I'm still fuzzy as to why the code, as they define it, did not work.

            If I include the forward declarations of:
            class QCheckBox;
            class QLabel;
            class QLineEdit;
            class QPushButton;

            ... that's all well and good, but it doesn't answer where the code for these classes is defined.
            If you don't include <QCheckbox> ..... <QPushbutton> then where are they kept?

            The code worked once I commented out these forward declarations and simply included the include file for each of them, but I understand that this is a cumbersome way of doing it.

            Where else is the full class definition of the above classes if not in its own QFunction include file?

            If they are included in another, larger include file that the code has already specified, then why didn't the code work?
            The only include files in the actual specified code are QDialog, QtGUI and QApplication.

            Thanks for your help.

            Regards,

            Uberlinc.

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #15

            @Uberlinc said in Header files and general questions.:

            The chapter does indeed mention forward declarations, but I'm still fuzzy as to why the code, as they define it, did not work.

            Which chapter? We could have a closer look.

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

            1 Reply Last reply
            0
            • U Offline
              U Offline
              Uberlinc
              wrote on last edited by
              #16

              C++ GUI Programming with Qt 4, Second Edition
              Blanchette and Summerfield
              Chapter 2. Creating Dialogs.

              VRoninV 1 Reply Last reply
              0
              • U Uberlinc

                C++ GUI Programming with Qt 4, Second Edition
                Blanchette and Summerfield
                Chapter 2. Creating Dialogs.

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #17

                The code in that chapter is still 100% valid please note the:

                The source code is spread across two files: finddialog.h and finddialog.cpp

                I think you just put everything in the same file

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                3
                • U Offline
                  U Offline
                  Uberlinc
                  wrote on last edited by
                  #18

                  No, I definitely have, not 2, but 3 (three) files:
                  finddialog.h
                  finddialog.cpp
                  main.cpp

                  I believe that I have copied the code verbatim, but as it is interspersed between explanatory text, it is possible that I missed the declaration that qualifies these classes.

                  Can't find where, though!

                  Can anyone point out in the original text where the declaration is that qualifies the classes in question?
                  Is it one line like #include <QDoesItAll> or is it a block of several lines that I've missed?

                  Thanks,

                  Uberlinc.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mpergand
                    wrote on last edited by mpergand
                    #19

                    Hi,

                    You can download the source code from here:
                    https://github.com/GaoHongchen/CPPGUIProgrammingWithQt4

                    The project is in chap02>find folder
                    You need to proceed two modifications:

                    • in the find.pro file, add at the top:
                    QT       += core gui
                    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                    
                    • in the findDialog.cpp, at the top do the following:
                    //#include <QtGui>
                    #include <QtWidgets>
                    

                    That's all

                    More info here:
                    https://wiki.qt.io/Transition_from_Qt_4.x_to_Qt5

                    JKSHJ 1 Reply Last reply
                    1
                    • M mpergand

                      Hi,

                      You can download the source code from here:
                      https://github.com/GaoHongchen/CPPGUIProgrammingWithQt4

                      The project is in chap02>find folder
                      You need to proceed two modifications:

                      • in the find.pro file, add at the top:
                      QT       += core gui
                      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                      
                      • in the findDialog.cpp, at the top do the following:
                      //#include <QtGui>
                      #include <QtWidgets>
                      

                      That's all

                      More info here:
                      https://wiki.qt.io/Transition_from_Qt_4.x_to_Qt5

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by JKSH
                      #20

                      @mpergand said in Header files and general questions.:

                      • in the findDialog.cpp, at the top do the following:
                      //#include <QtGui>
                      #include <QtWidgets>
                      

                      Right.

                      #include <QtGui> is a quick way to include every single class in the Qt GUI module. Since QCheckBox et al. have been moved to the Qt Widgets module, you can replace this line with #include <QtWidgets> -- this will include every single class in the Qt Widgets module.

                      To see how this works,

                      1. Open the code in Qt Creator
                      2. Hover your mouse over the line #include <QtWidgets>
                      3. Press F2. You will see that you are actually #include-ing many many classes.

                      So to summarize:

                      • Write class QCheckBox; in your .h file, to allow your class to contain a pointer to QCheckBox.
                      • Write #include <QtWidgets> or #include <QCheckBox> in your .cpp file to allow your code to instantiate a QCheckBox.

                      Personally, I prefer to include individual classes when developing a proper project. However, including the whole module is quick an easy for rapid prototyping or for tutorials.

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

                      1 Reply Last reply
                      1
                      • U Offline
                        U Offline
                        Uberlinc
                        wrote on last edited by
                        #21

                        Woo-hoo!
                        It works!
                        Thank you!

                        So, clearly there is a slight change in the code.
                        This explains why it didn't work beforehand.

                        Many thanks!

                        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