• 2429阅读
  • 7回复

请教new的时候这个this有无必要还是都可以呢? [复制链接]

上一主题 下一主题
离线ustone
 

只看楼主 倒序阅读 楼主  发表于: 2018-06-08

离线kaon

只看该作者 1楼 发表于: 2018-06-08
http://doc.qt.io/qt-5/qaction.html#QAction-1

Constructs an action with some text and parent. If parent is an action group the action will be automatically inserted into the group.
离线ustone

只看该作者 2楼 发表于: 2018-06-08
回 kaon 的帖子
kaon:http://doc.qt.io/qt-5/qaction.html#QAction-1
Constructs an action with some text and parent. If parent is an action group the action will be automatically inserted into the group. (2018-06-08 14:25) 

这个我看懂了,但是我的问题还是没有解决,fileMenu->addAction(openAct);会把fileMenu作为openAct的parent吗?如果这样,openAct就有两个parent了?
离线kaon

只看该作者 3楼 发表于: 2018-06-08
http://doc.qt.io/qt-5/qwidget.html#addAction
The ownership of action is not transferred to this QWidget.
离线ustone

只看该作者 4楼 发表于: 2018-06-08
回 kaon 的帖子
kaon:http://doc.qt.io/qt-5/qwidget.html#addAction
The ownership of action is not transferred to this QWidget.
 (2018-06-08 18:24) 

那代码就对了。
QAction的构造函数,那个参数是parent,
QAction(const QString &text, QObject *parent = nullptr)
构造的时候parent同时也是owner吗?
离线ustone

只看该作者 5楼 发表于: 2018-06-08
我查了一下,
void QLayout::addWidget(QWidget *w)
Adds widget w to this layout in a manner specific to the layout. This function uses addItem().
void QLayout::addItem(QLayoutItem *item)
。。。
Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.

看起来感觉跟addAction不一致,是不是这个道理,action可以被多个widget添加(比如菜单、工具栏、右键菜单等),所以ownership不转移?
离线kaon

只看该作者 6楼 发表于: 2018-06-09
回 ustone 的帖子
ustone:那代码就对了。
QAction的构造函数,那个参数是parent,
QAction(const QString &text, QObject *parent = nullptr)
构造的时候parent同时也是owner吗? (2018-06-08 21:45) 

对。
widget需要layout来作为他的parent,合情合理
action,是代表动作类,行动类,所以,无需widget来控制他的parent
我是这么理解的
离线ustone

只看该作者 7楼 发表于: 2018-06-09
回 kaon 的帖子
kaon:对。
widget需要layout来作为他的parent,合情合理
action,是代表动作类,行动类,所以,无需widget来控制他的parent
我是这么理解的 (2018-06-09 14:49) 

ok。
快速回复
限100 字节
 
上一个 下一个