Q_D pointer: private pointer in Qt

Q_D pointer: private pointer in Qt

When developing Qt applications, in order to protect the private data of objects and improve code robustness, QObject and QSharedData are often used. However, Q_D pointers are a more efficient and easy-to-use method that avoids copying data, protects objects from external access, and reduces memory allocation and copying. In this article, we will introduce in detail the principles, advantages and usage of Q_D pointers.

What is Q_D pointer?

The Q_D pointer is a private pointer in the Qt framework, used to hide the implementation of an object and protect its data. The principle of Q_D pointer is to put the implementation of the object in a separate class (such as MyClassPrivate), and then store the Q_D pointer in the main class as a single member variable pointing to that class. This means that only the class can access this pointer, and the object's implementation can be easily changed without changing the public interface.

The following is a simple example using the Q_D pointer:

#include <QExplicitlySharedDataPointer>
#include <iostream>

class MyClassPrivate;
class MyClass {
    
    
public:
    MyClass();
    ~MyClass();

    void setNumber(int number);
    int getNumber() const;

private:
    QExplicitlySharedDataPointer<MyClassPrivate> const d_ptr;
};

class MyClassPrivate {
    
    
public:
    int m_number;
};

MyClass::MyClass() : d_ptr(new MyClassPrivate)
{
    
     }

MyClass::~MyClass()
{
    
     }

void MyClass::setNumber(int number) {
    
    
    d_ptr->m_number = number;
}

int MyClass::getNumber() const {
    
    
    return d_ptr->m_number;
}

int main() {
    
    
    MyClass* myObject = new MyClass;

    myObject->setNumber(42);
    std::cout << "My object's number is: " << myObject->getNumber() << std::endl;

    delete myObject;
    return 0;
}

In this example, we have defined a main class named "MyClass" and a secondary class named "MyClassPrivate". In MyClass, we use Q_D pointer as private member variable and store it in const variable d_ptr. Then, in MyClassPrivate, we define a simple data member m_number and access it from MyClass using the setNumber() and getNumber() methods.

Finally, in the main() function, we create a new MyClass object and set and retrieve the object's number using the setNumber() and getNumber() methods. Finally, we delete the object using the delete operator to avoid memory leaks.

Advantages of Q_D pointers

There are many benefits to using Q_D pointers as a development model for Qt applications:

  • Private implementation: Q_D pointers avoid direct access and changes to object state, thereby protecting the object and its data and reducing the risk of administrators required for operations.
  • Avoid copying: Q_D pointers avoid copying objects, thereby reducing memory allocation and copy operations, saving time and resources.
  • Testability: Using Q_D pointers makes classes easier to test and debug. Q_D pointers reduce the complexity of the public interface, making it easier to build automated tests.

Notes on using the Q_D pointer

When using the Q_D pointer, you need to pay attention to the following matters:

  • The implementation needs to be defined separately: Auxiliary types are defined separately to ensure that the auxiliary type can only be accessed through the Q_D pointer.
  • Pointers are not thread-safe: Q_D pointers still need to be locked, and there are many options for achieving this goal, such as signals and slots mechanisms and mutexes, etc.
  • Depends on QObject: Since the Q_D pointer requires access to QObject's meta-information, it can only be used with classes derived from QObject.

in conclusion

Q_D pointers are a Qt application development pattern used to protect object implementations and prevent data copying. It makes it easy to develop secure code that is more efficient, has better testability, and reduces memory allocations. With Q_D pointers, we can encapsulate and manage data in complex objects, helping us develop more robust Qt applications faster.

Guess you like

Origin blog.csdn.net/qq_25549309/article/details/131710323