0

Currently, I am learning C++ GUI--Qt and I am confused about the header files.

For example, I want to use QHBoxLayout in my constructor function to manage layout. QHBoxLayout and QVBoxLayout inherit from QBoxLayout.Hence, QBoxLayout header file should not contain definition about QHBoxLayout or QVBoxLayout.But it works fine unexpectedly.

So I guess it may be one feature of Qt or does it works for native C++ Language?

#include "dialog.h"
#include <QBoxLayout>

Dialog::Dialog(int tempCelsius, QWidget *parent)
    : QDialog(parent)
{   
    QHBoxLayout *mainLayout = new QHBoxLayout();
    QVBoxLayout *leftLayout = new QVBoxLayout();
    QVBoxLayout *rightLayout = new QVBoxLayout();
    celsiusGroupBox = new QGroupBox(this);
    fahrenheitGroupBox = new QGroupBox(this);
    celsiusDia = new QDial(celsiusGroupBox);
    fahrenheitDia = new QDial(fahrenheitGroupBox);
    celsiusLDNumber = new QLCDNumber(celsiusGroupBox);
    fahrenheitLDNumber = new QLCDNumber(fahrenheitGroupBox);
    celsiusGroupBox->setTitle("Celsius");
    fahrenheitGroupBox->setTitle("Fahrenheit");
    leftLayout->addWidget(celsiusDia);
    leftLayout->addWidget(celsiusLDNumber);
    celsiusGroupBox->setLayout(leftLayout);
    rightLayout->addWidget(fahrenheitDia);
    rightLayout->addWidget(fahrenheitLDNumber);
    fahrenheitGroupBox->setLayout(rightLayout);
    celsiusGroupBox->setLayout(leftLayout);
    mainLayout->addWidget(celsiusGroupBox);
    mainLayout->addWidget(fahrenheitGroupBox);
    setLayout(mainLayout);
}

ChufanSuki
  • 83
  • 1
  • 6
  • Note that because of the way C++'s preprocessor works, you can accidentally "pull in" types through other header files that include the file you want. So just because "it works fine" doesn't always mean it is correct. – 0x5453 Feb 04 '20 at 14:43
  • I do not propose this as a duplicate, but you might find the discussion helpful: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – Yunnosch Feb 04 '20 at 14:55
  • So is `#include ` `#include ` similar to `#include ` that adds almost all classes? – ChufanSuki Feb 05 '20 at 15:05

2 Answers2

1

Actually, QBoxLayout has all three classes defined in it. Qt does this, afaik, to reduce the number of includes. This is common in C++ for very small classes to just be put in with their parent class. Both QHBoxLayout and QVBoxLayout are only "special" in their constructor/destructor. You can open up the implementation of the header files and have a look. (note that <QBoxLayout> just includes <qboxlayout.h>

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
0

First of all,header files do not contain the definition of classes. They only contain the declarations of classes, which are later defined in a corresponding .cpp file. These are compiled into static and dynamic libraries which is probably lQt5Widgets, when you build the library from scratch or download the pre compiled binaries. This is then linked to your program, so that all names can be resolved. So this actually works not because the definitions are found in the <QBoxLayout>, but because <QBoxLayout>contains the declaration for QHBoxLayout and QVBoxLayout. The following code extract from <qboxlayout.h> shows their declaration..


113 class Q_WIDGETS_EXPORT QHBoxLayout : public QBoxLayout
114 {
115     Q_OBJECT
116 public:
117     QHBoxLayout();
118     explicit QHBoxLayout(QWidget *parent);
119     ~QHBoxLayout();
120 
121 
122 private:
123     Q_DISABLE_COPY(QHBoxLayout)
124 };
125 
126 class Q_WIDGETS_EXPORT QVBoxLayout : public QBoxLayout
127 {
128     Q_OBJECT
129 public:
130     QVBoxLayout();
131     explicit QVBoxLayout(QWidget *parent);
132     ~QVBoxLayout();
133 
134 
135 private:

It works because they are declared in <QBoxLayout> and you are probably linking it with the correct library also where their definitions are found.

Kobby Owen
  • 85
  • 1
  • 8
  • 1
    The class is defined in the header file. The class's functions are typically declared in the header, and defined in the source. But the class itself is defined. See `[class]/1,2` and `[class.name]/1` of standard. – ChrisMM Feb 04 '20 at 15:35