开发者

Qt - Some questions about a header file

开发者 https://www.devze.com 2023-02-24 08:00 出处:网络
I have some questions about this header file from the C++ GUI Programming with Qt 4 book: #ifndef GOTOCELLDIALOG_H //Check if GOTOCELLDIALOG_H has not been defined previously

I have some questions about this header file from the C++ GUI Programming with Qt 4 book:

#ifndef GOTOCELLDIALOG_H //Check if GOTOCELLDIALOG_H has not been defined previously
#define GOTOCELLDIALOG_H
#include <QDialog>
#include "ui_gotocelldialog.h"

class GoToCellDialog: 开发者_如何学Cpublic QDialog,
public Ui::GoToCellDialog {
    Q_OBJECT
    public: GoToCellDialog(QWidget * parent = 0);
    private slots: void on_lineEdit_textChanged();
};

#endif

1- I know what #define means. But, what is its use here? I mean, why are we trying to #define a header file?

2- Since I inherited QDialog and Ui::GoToCellDialog, why are we #includeing them?

3- What is Q_OBJECT? Why isn't it terminated by a semicolon?

4- What is this constructor GoToCellDialog(QWidget *parent = 0); trying to tell us?

Thanks a lot.


1) This is called a header guard. It makes repeated inclusion of that header idempotent, i.e.

#include "gotocelldialog.h"
#include "gotocelldialog.h"

does the same as

#include "gotocelldialog.h"

This may look weird. In practice, this happens indirectly. Just look at how often "qobject.h" gets included in a single file.

2) You can only use types that are already defined before. The #include brings in the declaration you need.

3) Q_OBJECT is part of the Qt system. It's a macro, and defines a few standard members in your class. Those members support features like signals/slots.

4) Qt widgets may or may not have parents. If they don't have a parent, they're independent windows. If they do have parents, they're part of the parent window and are destroyed with the parent window.

The (QWidget *parent = 0) argument list is C++'s way to denote a default argument. If you do not pass an explicit value for the parent, it will be 0 (NULL). This stands for "no parent". Hence, you can create a parentless GoToCellDialog in two ways: either new GoToCellDialog(0) or new GoToCellDialog.


1) This is pattern to prevent multiple declarations of a same types.

2) If your header files uses types you need to either #include the type definitions or make a forward declaration.

3) Q_OBJECT is a macro that qt's moc tool will use to expand the class declaration to include QObject specific declarations.

4) Each graphical widget has parent/child relationship. Default constructor will then associate your new widget into its parent by passing the parent as a parameter.


  1. The use of #define here is to define the include guard if it was not previously defined yet. The first #ifndef checks if that include guard has already been defined somewhere, if it hasn't it will define it on line 2 with #define.

An include guard per this article:

In the C and C++ programming languages, an #include guard, sometimes called a macro guard, is a particular construct used to avoid the problem of double inclusion when dealing with the #include directive.

  1. You're including them so the linker knows where to find it's definition.

  2. Is a macro...

  3. That constructor tells you it takes a QWidget pointer and this parameter is defaulted to NULL, so you can use the ctor without passing this parameter. That means however you should then NOT use this parameter in the class, as using a NULL pointer is Undefined Behaviour.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号