c++ - Why QAction is not adding to QMenu, if QMenu is unique_ptr? -


code example:

auto filemenu = std::make_unique<qmenu>(this->menubar()->addmenu("first")); filemenu->addaction("afirst");  auto x = this->menubar()->addmenu("second"); x->addaction("asecond"); 

results:

i have 2 menus in menubar, in first menu - reason, there no actions. second menu correctly has action.

i have tried different approaches, like, class-member pointers, , on, shortest possible example - qaction missing, if qmenu unique_ptr. can explain me? parent window qmainwindow, in case.

system info: win8.1 x64, compiler vs2013, qt 5.4 x32.

in line:

auto filemenu = std::make_unique<qmenu>(this->menubar()->addmenu("first")); 

filemenu becomes new qmenu object (using this constructor). it's quite same as:

std::unique_ptr<qmenu> filemenu(new qmenu(this->menubar()->addmenu("first"))); 

then, add qaction temporary, new menu.

in second case:

auto x = this->menubar()->addmenu("second"); x->addaction("asecond"); 

x become pointer existing menu. that's difference.

anyway, shouldn't hold qobjects using std::unique_ptr. in qt, there convention form tree qobjects assigning parent each of them. parent deletes it's children recursively , shouldn't manage them manually or may cause double free in specific cases.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -