ChufanSuki
ChufanSuki

Reputation: 93

Include C++ header file

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);
}

Upvotes: 0

Views: 417

Answers (2)

Kobby Owen
Kobby Owen

Reputation: 95

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.

Upvotes: 0

ChrisMM
ChrisMM

Reputation: 10021

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>

Upvotes: 1

Related Questions