vendredi 5 février 2021

Slot recursion in QMenu by lambda

I have the following situation. I have a menu, for each item, I create a slot using a lambda function.

for(auto &command : mainCommands) {
    commandMenu->addAction(command.name, [&, command](){ slotCommandButtonClicked(command, commandBtn); });
}

The team structure has children, and therefore I need to generate a slot with the same actions for each descendant. I did it like this:

void MainWindow::slotCommandButtonClicked(const Command &command, CommandButton *commandButton) const {
... some code
if(command.hasChild) {
    commandButton->childButton->setVisible(true);

    auto commandMenu = new QMenu(commandBtn);
    auto childCommands = DbProxy::getChildCommands(command.id);
    for(auto &childCommand : childCommands) {
        commandMenu->addAction(childCommand.name,
                               [&, childCommand](){ slotCommandButtonClicked(childCommand, commandButton->childButton);});
    }
    commandButton->childButton->setMenu(commandMenu);
}else {
    commandButton->childButton->setVisible(false);
}
... somecode
}

The first buttons do everything as needed, the descendant menu is loaded, but when clicked, the application crashes with error: Process finished with exit code 139 (interrupted by signal 11: SIGSEGV) . I can't figure out what I'm doing wrong, and can I do that at all? (Full code link https://github.com/arxitect/git-helper/blob/main/app/MainWindow.cpp#L147)

Aucun commentaire:

Enregistrer un commentaire