What does Q_D macro do in Qt

QtMacros

Qt Problem Overview


I am exploring Qt source code and came across this Q_D macro. Could some one please explain what it does?

Almost all the time it should return a widget of the type given as a parameter which is the d variable. Need more clarification on this.

Qt Solutions


Solution 1 - Qt

In short, Qt uses private implementation to manage data. For classes that do this, there usually is an X class and an XPrivate class. The Q_D macro defines the "d" pointer so if you write d->whatever, you have access to that private data part.

This article should pretty much cover most of your questions:

https://wiki.qt.io/D-Pointer

Solution 2 - Qt

Q_D is part of Qt's implementation of d-pointers, or the pimpl pattern (private implementation). A bit more information on all of this can be found on the Qt DevNet wiki: https://wiki.qt.io/D-Pointer

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTharangaView Question on Stackoverflow
Solution 1 - QtTim MeyerView Answer on Stackoverflow
Solution 2 - QtleinirView Answer on Stackoverflow