Header files and general questions.



  • Okay, so I'm trying to teach myself Qt from scratch.
    I have basic C++ skills and so I'm building on these.

    As there appears to be no actual "Learn the latest version of Qt from scratch" I am trying to learn from an outdated book (half the code doesn't work - I suspect a fair bit has changed in 18 years) and updated book which seems less focused on raw code and more focused on the color of the drapes in your new GUI.
    Also, I'm searching for bits and pieces online.

    So, my question is this:

    1. The old book I am working on mainly uses header files in the format #include "qsomethingfile.h" but I notice that there, more often than not, is an available corresponding header file #include <QSomethingFile>

    Which should I use?
    Is one a superset of the other?

    1. One of the books that I am working from is more concerned with using the design tool to create a GUI interface. From what I can work out, data pertaining to these created objects exists in a QML(?) file off to the side and is not represented in hard code in the made files (even though you can create the very same thing in hard code in the main files.)

    Which is the preferred way? GUI interface and then merely reference these objects in the main code, or code up the whole thing from scratch?

    Many thanks,

    Uberlinc.


  • Moderators

    @Uberlinc said in Header files and general questions.:

    Okay, so I'm trying to teach myself Qt from scratch.
    I have basic C++ skills and so I'm building on these.

    Hi @Uberlinc, and welcome!

    As there appears to be no actual "Learn the latest version of Qt from scratch" I am trying to learn from an outdated book (half the code doesn't work - I suspect a fair bit has changed in 18 years)

    The Qt engineers tried to make the Qt 4 -> Qt 5 transition as clean and painless as possible, so quite a bit of Qt 4 code is still valid for Qt 5.

    and updated book which seems less focused on raw code and more focused on the color of the drapes in your new GUI.

    Not my favourite approach, but some people like it.

    Also, I'm searching for bits and pieces online.

    This helps too. This forum is a good place for quick questions.

    So, my question is this:

    1. The old book I am working on mainly uses header files in the format #include "qsomethingfile.h" but I notice that there, more often than not, is an available corresponding header file #include <QSomethingFile>

    Which should I use?
    Is one a superset of the other?

    The best way is #include <QSomething>. 99.9% of the time, "QSomething" is the name of the actual class you want to use, so it's easy to remember what to #include.

    And then, if you use your IDE to open the file that you #include'd, you'll find that <QSomething> only has one line which #includes "qsomething.h".

    1. One of the books that I am working from is more concerned with using the design tool to create a GUI interface. From what I can work out, data pertaining to these created objects exists in a QML(?) file off to the side and is not represented in hard code in the made files (even though you can create the very same thing in hard code in the main files.)

    Do you mean .ui.qml files? These didn't exist back in Qt 4.

    Which is the preferred way? GUI interface and then merely reference these objects in the main code, or code up the whole thing from scratch?

    While learning, I think it's more helpful to learn how to code the whole thing from scratch. This way, things are simpler and clearer.

    After I became competent and was able to code everything from scratch without referring to a tutorial, I switched over to the design tool because it's much faster than writing raw code.



  • Many thanks.

    The two books I am using are:

    1. SAMS Teach Yourself Qt Programming in 24 Hours by Daniel Soln.
      (Publish date is May 2000 even though it's the only one I could find online!)
      I like this book in that it is a step-by-step guide and teaches base coding without using the GUI interface at all.

    2. Qt 5 C++ GUI Programming Cookbook by Lee Zhi Eng.
      (This is up to data, but seems almost exclusively obsessed with the GUI design side of things.)

    I would happily code from scratch, but the SAMS book doesn't seem to work as the examples are old and the compiler throws up errors saying "not declared" etc for many functions within classes even though I've copied the code verbatim.

    For example, I am currently attempting a simple QTableView example.
    The book uses qtableview.h
    It also uses functions such as setCellWidth(int), setCellHeight(int) and setNumCols(int) etc that Qt does not seem to recognise. ('setCellHeight' was not declared in this scope)
    Even if I change the #include to <QTableView> it does not work.

    Is this book of any use to me at all?
    Is there a suitable equivalent to learn Qt C++ (I'd prefer this over other languages) from scratch without having to scrounge the internet for relevant up-to-date examples?

    Thanks,

    Uberlinc.




  • Moderators

    @Uberlinc said in Header files and general questions.:

    1. SAMS Teach Yourself Qt Programming in 24 Hours by Daniel Soln.
      (Publish date is May 2000 even though it's the only one I could find online!)
      I like this book in that it is a step-by-step guide and teaches base coding without using the GUI interface at all.

    Ah... the year 2000 is in the era of Qt 2 (https://en.wikipedia.org/wiki/Qt_version_history ). That book is useless for Qt 5, I'm afraid.

    You can still learn from a Qt 4 book because the transition from Qt 4 to Qt 5 has only minor incompatibilities, but Qt 2 is far too different from Qt 5.

    I would happily code from scratch, but the SAMS book doesn't seem to work as the examples are old and the compiler throws up errors saying "not declared" etc for many functions within classes even though I've copied the code verbatim.

    For example, I am currently attempting a simple QTableView example.
    The book uses qtableview.h
    It also uses functions such as setCellWidth(int), setCellHeight(int) and setNumCols(int) etc that Qt does not seem to recognise. ('setCellHeight' was not declared in this scope)

    setCellWidth(int), setCellHeight(int), and many other functions from Qt 2 no longer exist in Qt 5. The closest living relatives in Qt 5 are setColumnWidth(int, int) and setRowHeight(int, int).

    You can see how QTableView has evolved over the years:

    Is there a suitable equivalent to learn Qt C++ (I'd prefer this over other languages) from scratch without having to scrounge the internet for relevant up-to-date examples?

    If you want physical copies of @VRonin's links, see

    Amazon will also suggest a variety of alternative Qt 5 books. I haven't read any of them myself, so I can't comment on their quality or give personal recommendations.



  • Many thanks for these suggestions.
    I am quite dismayed that the 70-odd bucks I spent on 2 books has been all in waste and that they are (essentially) of no use to me.

    I am heartened by the support and suggestions you've made.
    I will have a good look at those books that VRonin has suggested.
    They may be prove to be quite useful.
    Thank you!

    I will (obviously) check back in here with irritating questions from time to time when I simply don't get it!

    Many thanks.

    Uberlinc.



  • @Uberlinc from time to time, whenever you feel the topic/issue you posted is solved, please don't forget to mark it as such. Thank you


  • Moderators

    @Uberlinc said in Header files and general questions.:

    I will (obviously) check back in here with irritating questions from time to time when I simply don't get it!

    Please do! This is what the forum is for.

    All the best.



  • 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.



  • 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.



  • @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.


  • Moderators

    @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:



  • 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.



  • @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!


  • Moderators

    @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.


Log in to reply
 

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