Core Qt analysis: Find the source code of QObject

Source: Internet
Author: User

I was planning to take "the path to Qt learning" as a similar tutorial, so I didn't plan to put some content related to the source code in that series. So let's take a look at a new beginning today! It is estimated that this series will not be very fast, because the company has to do a flex project demonstration recently, so there is not much time, and the source code is not so easy to see. Beans are not C/C ++, so it seems a little troublesome.

As an independent series, "Qt source code analysis" roughly analyzes the source code of Qt. I personally think that the purpose of analysis is not to write a Qt, but to learn its clever design. Just as we learn the design and implementation of the compiler, we do not need to write a compiler, but learn a lot of useful algorithms and design ideas. These are more important and more realistic than writing a Qt.

Qt source code analysis is not a step-by-step series! Because the source code does not seem to have a gradual process, you can only look at how much to say. However, beans will try to express what they want to say clearly!

Of course, such a series certainly cannot stand on the shoulders of giants to see the problem. Some similar series are listed below:

  • Blog

Like many source code analysis articles, this series may be hard for beginners to understand, but interested children's shoes should also be learned everywhere ~

The first question we will talk about below is where to find the Qt source code.

The Qt you installed should be LGPL. This version includes the complete Qt source code. Open your Qt installation directory and you will see the directory named at the time of 2010.02.1. This is the version number of Qt. The date directory corresponding to version 4.6.1 is 2010.02.1. Enter this Directory, which contains a qt directory. In this qt directory, you will find the src directory. Here is the complete Qt source code. The code we want to analyze can be found here!

Let's start with the main class QObject.

Follow the path we mentioned earlier to go To the qt/src folder. You may have known the directory names here, because almost all the folder names here correspond to the names of Qt modules: gui, network, multimedia, and so on. We start with the core QtCore. This module corresponds to the corelib folder.

First, we need to look for the QObject class. QObject is selected because it is the core class of Qt. Another important reason is that QObject is a typical Qt class, we can learn the design idea of Qt through this class.

Recall that the statement we used when compiling the Qt code is

 
 
  1. #include <QObject> 

When we run the # include statement, the Preprocessor looks for the qt/include/QtCore directory. Here we find the QObject file, and there is only one statement:

 
 
  1. #include "qobject.h" 

Then we found the qobject. h in the same directory. There is only one sentence:

 
 
  1. #include "../../src/corelib/kernel/qobject.h" 

This path is the location of the qobject we found above!

So we can go back to corelib to see the kernel folder. You should know the name, which is the core of Qt corelib. Here, we can find four files with qobject headers:

  • Qobject. h: Class Definition of QObject. This is the file referenced by the QObject file, that is, the actual header file we use;
  • Qobject. cpp: implementation code of QObject;
  • Qobjectdefs. h: This file defines a lot of macros used and defines the QMetaObject class, which is the basis for implementing signal-slot;
  • Qobject_p.h: Auxiliary Data class for QObject;

In fact, we will see two other files: qobjectcleanuphandler. h and qobjectcleanuphandler. cpp. However, if you open these two files, you will find that a QObjectCleanupHandler class is defined here, and this class inherits QObject, so this is just a common tool class, not in our current discussion. Therefore, we can think that the QOjbect class is implemented by four files: qobject. h, qobject. cpp, qobjectdefs. h, and qobject_p.h.

This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/322343

Related Article

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.