English 中文(简体)
为什么“q_ptr”点子被分配到QObject的“那个”点?
原标题:Why the "q_ptr" pointer is assigned to "this" pointer of QObject?
  • 时间:2012-04-18 04:20:39
  •  标签:
  • qt

和标题一样,“q_ptr”点子被分配到QObject的“那个”点? 来源代码。

QObject::QObject(QObjectPrivate &dd, QObject *parent)
: d_ptr(&dd)
{
>>Q_D(QObject);
>>d_ptr->q_ptr = this;/*question*/
.......

然后,在使用<代码>Q_Q()时,在源代码中,如重:

Q_Q(QWidget)

它将归还由q_fun(以下)职能处理的q点:

QWidget*q_func() {return static_cast<QWidget*>(q_ptr);}

我们都知道,static_cast 如果父母将子女带给子女,则不安全。

我对<代码>/*question*/code>感到非常沮丧。 看到任何伪装告诉我什么秘密? 感谢!

最佳回答
d_ptr->q_ptr = this;/*question*/

这就是私人执行对象(PPRIL idiom)被告知其正在与(非私人编码)一起工作的物体。 此处为有关Qt和的有用链接(d_ptr)。

<代码>Q_Q 宏观回归点至<代码>QObject,因此,你可以发出信号(除其他事项外)。 关于<代码>static_castbit,这是安全的,因为对<代码>所创造的每类产品,宏观定义是不同的。 Q_DECLARE_PRIVATE and Q_DECLARE_public XIs: the result is, static_cast are 所有这些都投向正确的一类。 我再次建议阅读这一联系。

问题回答

暂无回答




相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...

热门标签