Qt source code analysis 1

Source: Internet
Author: User

Today, we started to analyze the source code of QT, starting with a very simple program.

#include <QtGui/QApplication>int main(argc, argv){    QApplication a(argc, argv);    MainWindow w;    w.show();     return a.exec();}

 

 
First, look at the qapplication section. Find the qapplication In the API section and find that there is only one sentence in this file # inlcude
"Qapplication. H"

Later, I checked some other files in similar forms and found that only this sentence exists. For example, in qpainterevent, only # include "qpainterevent. H"

Therefore, we can imagine that these uppercase statements, similar to qapplication, represent classes, rather than files. They hide the content contained in the file.

After adding the file named after the class name, you can use this class, which is really convenient.

Then, let's think about why "qapplication. H" instead of # include <qapplication. h>"

 
After reading a blog, you will know that <> this will cause you to start searching for header files from the system API and find them in the current file directory (that is, the Directory of qappliaction) of the file.

"" Will cause the compiler to start searching from the current file directory of the file, and then find it in the system API.

So in order to speed up compilation, QT used "" to include its own classes and learned it.

Let's look at the main () function. The function is written in the form of two parameters, which will be considered in 2.

Let's look at the source code of application. h.

By convention, first a declaration of gplblablabla..., and then a definition of qapplication_h

#include <QtCore/qcoreapplication.h>#include <QtGui/qwindowdefs.h>#include <QtCore/qpoint.h>#include <QtCore/qsize.h>#include <QtGui/qcursor.h>#ifdef QT_INCLUDE_COMPAT# include <QtWidgets/qdesktopwidget.h>#endif#ifdef Q_NO_USING_KEYWORD#include <QtGui/qpalette.h>#endif#include <QtGui/qguiapplication.h>

Some header files are included here, and when q_no_using_keyword is defined, add the header file qpalette. h.

In the following two sentences, qt_begin_header qt_begin_namespace defines a bunch of classes, and qt_end_namespace and qt_end_header

Why?

After checking it, I found that this is a commonly used small skill in large projects and seems to involve compilation principles. I don't quite understand this,

I only know that some classes are sometimes used in the. cpp file. These classes are not defined in. H files because they do not exist in. H files,

So # include <...> in the CPP File but if you change this method, it will reduce the relevance during file compilation, reduce the number of re-compiled classes during file modification, and reduce the Compilation Time.

I used to directly # include all the header files used by. H, and # include the. cpp files without the include classes in. H files.

Later, I tried it. In the. h file, only class XXX is used for classes not allowed to be initialized. In the. cpp file, I just need to initialize the class and then include it again.

However, some classes to be initialized in. h cannot be like this. For example, the class to be inherited must be directly # include in the header file. Otherwise, a syntax error will be reported during compilation.

Try his namespace method again.

There is no problem during compilation, but why does it feel that the compilation time is getting longer ..... Pai_^

Let's take a look at the class code.

class Q_WIDGETS_EXPORT QApplication : public QGuiApplication{    Q_OBJECT    Q_PROPERTY(QIcon windowIcon READ windowIcon WRITE setWindowIcon)    Q_PROPERTY(int cursorFlashTime READ cursorFlashTime WRITE setCursorFlashTime)    Q_PROPERTY(int doubleClickInterval  READ doubleClickInterval WRITE setDoubleClickInterval)    Q_PROPERTY(int keyboardInputInterval READ keyboardInputInterval WRITE setKeyboardInputInterval)#ifndef QT_NO_WHEELEVENT    Q_PROPERTY(int wheelScrollLines  READ wheelScrollLines WRITE setWheelScrollLines)#endif    Q_PROPERTY(QSize globalStrut READ globalStrut WRITE setGlobalStrut)    Q_PROPERTY(int startDragTime  READ startDragTime WRITE setStartDragTime)    Q_PROPERTY(int startDragDistance  READ startDragDistance WRITE setStartDragDistance)#ifndef QT_NO_STYLE_STYLESHEET    Q_PROPERTY(QString styleSheet READ styleSheet WRITE setStyleSheet)#endif#ifdef Q_OS_WINCE    Q_PROPERTY(int autoMaximizeThreshold READ autoMaximizeThreshold WRITE setAutoMaximizeThreshold)#endif    Q_PROPERTY(bool autoSipEnabled READ autoSipEnabled WRITE setAutoSipEnabled)public:#ifdef Q_QDOC    QApplication(int &argc, char **argv);#else    QApplication(int &argc, char **argv, int = ApplicationFlags);#endif    virtual ~QApplication();    static QStyle *style();    static void setStyle(QStyle*);    static QStyle *setStyle(const QString&);    enum ColorSpec { NormalColor=0, CustomColor=1, ManyColor=2 };    static int colorSpec();    static void setColorSpec(int);#if QT_DEPRECATED_SINCE(5, 0)    QT_DEPRECATED static inline void setGraphicsSystem(const QString &) {}#endif#ifdef Q_NO_USING_KEYWORD    static QPalette palette() { return QGuiApplication::palette(); }#else    using QGuiApplication::palette;#endif    static QPalette palette(const QWidget *);    static QPalette palette(const char *className);    static void setPalette(const QPalette &, const char* className = 0);    static QFont font();    static QFont font(const QWidget*);    static QFont font(const char *className);    static void setFont(const QFont &, const char* className = 0);    static QFontMetrics fontMetrics();    static void setWindowIcon(const QIcon &icon);    static QIcon windowIcon();    static QWidgetList allWidgets();    static QWidgetList topLevelWidgets();    static QDesktopWidget *desktop();    static QWidget *activePopupWidget();    static QWidget *activeModalWidget();    static QWidget *focusWidget();    static QWidget *activeWindow();    static void setActiveWindow(QWidget* act);    static QWidget *widgetAt(const QPoint &p);    static inline QWidget *widgetAt(int x, int y) { return widgetAt(QPoint(x, y)); }    static QWidget *topLevelAt(const QPoint &p);    static inline QWidget *topLevelAt(int x, int y)  { return topLevelAt(QPoint(x, y)); }#if QT_DEPRECATED_SINCE(5, 0)    QT_DEPRECATED static inline void syncX() {}#endif    static void beep();    static void alert(QWidget *widget, int duration = 0);    static void setCursorFlashTime(int);    static int cursorFlashTime();    static void setDoubleClickInterval(int);    static int doubleClickInterval();    static void setKeyboardInputInterval(int);    static int keyboardInputInterval();#ifndef QT_NO_WHEELEVENT    static void setWheelScrollLines(int);    static int wheelScrollLines();#endif    static void setGlobalStrut(const QSize &);    static QSize globalStrut();    static void setStartDragTime(int ms);    static int startDragTime();    static void setStartDragDistance(int l);    static int startDragDistance();    static bool isEffectEnabled(Qt::UIEffect);    static void setEffectEnabled(Qt::UIEffect, bool enable = true);#if QT_DEPRECATED_SINCE(5, 0)    QT_DEPRECATED static QLocale keyboardInputLocale()    { return qApp ? qApp->inputMethod()->locale() : QLocale::c(); }    QT_DEPRECATED static Qt::LayoutDirection keyboardInputDirection()    { return qApp ? qApp->inputMethod()->inputDirection() : Qt::LeftToRight; }#endif    static int exec();    bool notify(QObject *, QEvent *);#ifdef QT_KEYPAD_NAVIGATION    static Q_DECL_DEPRECATED void setKeypadNavigationEnabled(bool);    static bool keypadNavigationEnabled();    static void setNavigationMode(Qt::NavigationMode mode);    static Qt::NavigationMode navigationMode();#endifQ_SIGNALS:    void focusChanged(QWidget *old, QWidget *now);public:    QString styleSheet() const;public Q_SLOTS:#ifndef QT_NO_STYLE_STYLESHEET    void setStyleSheet(const QString& sheet);#endif#ifdef Q_OS_WINCE    void setAutoMaximizeThreshold(const int threshold);    int autoMaximizeThreshold() const;#endif    void setAutoSipEnabled(const bool enabled);    bool autoSipEnabled() const;    static void closeAllWindows();    static void aboutQt();protected:    bool event(QEvent *);    bool compressEvent(QEvent *, QObject *receiver, QPostEventList *);private:    Q_DISABLE_COPY(QApplication)    Q_DECLARE_PRIVATE(QApplication)    friend class QGraphicsWidget;    friend class QGraphicsItem;    friend class QGraphicsScene;    friend class QGraphicsScenePrivate;    friend class QWidget;    friend class QWidgetPrivate;    friend class QWidgetWindow;    friend class QETWidget;    friend class QTranslator;    friend class QWidgetAnimator;#ifndef QT_NO_SHORTCUT    friend class QShortcut;    friend class QLineEdit;    friend class QWidgetTextControl;#endif    friend class QAction;#ifndef QT_NO_GESTURES    friend class QGestureManager;#endif};

On the first day, I felt dizzy .......

But take a closer look, there are still a lot of things to learn. For example, when a first macro (constant) is defined, some functions are not defined to reduce the amount of Compilation Time,

This is a little trick to reduce the compilation burden and make the definition of the class dynamic and controllable. It is really exquisite, amazing, and admired.

A lot of static functions and variables make me think it is a singleton mode .... Write so much first

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.