我尝试在Qt小部件中实现使用D_ptr的PIMPL方法。
以下代码是我实施的。
class GuiCentralHandler : public QWidget
{
Q_OBJECT
public:
GuiCentralHandler (QWidget *parent = 0);
~GuiCentralHandler ();
protected:
GuiCentralHandlerPrivate * const d_ptr;
private: //class methods
Q_DECLARE_PRIVATE(GuiCentralHandler )
};
GuiCentralHandler ::GuiCentralHandler (QWidget *parent)
:QWidget(parent),d_ptr(new GuiCentralHandlerPrivate (this))
{
}
GuiCentralHandler ::~GuiCentralHandler ()
{
Q_D(GuiCentralHandler );
delete &d_ptr;
}
我的私人d_ptr是
class GuiCentralHandlerPrivate
{
Q_DECLARE_PUBLIC(GuiCentralHandlerPrivate )
public:
GuiCentralHandlerPrivate (GuiCentralHandler *parent);
protected:
GuiCentralHandler * const q_ptr;
};
GuiCentralHandlerPrivate ::GuiCentralHandlerPrivate (GuiCentralHandler *parent)
: q_ptr(parent)
{
}
但是当我调用GuiCentralHandler ::~GuiCentralHandler ()
的析构函数时
它崩溃了。如何从主窗口小部件中删除d_ptr
或d_func。
请指出我在这个实现中出错的地方。
答案 0 :(得分:1)