Solved 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:
- 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?- 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.
-
@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:
- 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".
- 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:
-
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. -
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.
-
-
The book to start: http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf
The 2nd Book: http://www.tka4.org/materials/lib/Articles-Books/C++/Summerfield_M-Advanced_Qt_Programming__Creating_Great_Software_with_C++_and_Qt_4-Prentice_Hall(2010).pdfSee https://forum.qt.io/topic/70914/a-complete-reference-on-qt5 for a few notes on them
For Qt5 See:
https://forum.qt.io/topic/80656/a-new-book-on-qt -
@Uberlinc said in Header files and general questions.:
- 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 aresetColumnWidth(int, int)
andsetRowHeight(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
- https://www.amazon.com/Programming-Prentice-Source-Software-Development/dp/0132354160
- https://www.amazon.com/Advanced-Qt-Programming-Creating-Development/dp/0321635906
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.
- SAMS Teach Yourself Qt Programming in 24 Hours by Daniel Soln.
-
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
-
@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.
-
@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 ownQt 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!
-
@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.
-
C++ GUI Programming with Qt 4, Second Edition
Blanchette and Summerfield
Chapter 2. Creating Dialogs. -
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
-
No, I definitely have, not 2, but 3 (three) files:
finddialog.h
finddialog.cpp
main.cppI 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.
-
Hi,
You can download the source code from here:
https://github.com/GaoHongchen/CPPGUIProgrammingWithQt4The 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 -
@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,
- Open the code in Qt Creator
- Hover your mouse over the line
#include <QtWidgets>
- 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.