Question

I'm trying to call a function via a context menu embedded inside of a QTablewidget. It does not call the function in the slot. Could I be missing something ? The context menus do appear but clicking does nothing.

    QMenu *contextMenu = new QMenu;
    QModelIndex cell = ui->tableWidget->indexAt(pos);

    if(cell.isValid())

    {
            contextMenu->addAction("Burn Case", this, SLOT(burnCase()));

            contextMenu->addAction("Export Case", this, SLOT(exportCase(cell.row())));

            contextMenu->addAction("Delete Case", this, SLOT(deleteCase(cell.row())));

            contextMenu->exec(ui->tableWidget->mapToGlobal(pos));
    }

UPDATE:

The slot without a parameter being passed appears to be working, but not the ones with parameters

Was it helpful?

Solution

SLOT(exportCase(cell.row()))) is not valid connect argument. The string passed in SLOT() macro must contain the slot's name and its argument types. You can't pass argument values directly. So, if your slot accepts int argument, the statement would be SLOT(exportCase(int)).

More than that, you can't connect a signal to a slot with mismatching arguments. QMenu::addAction connects your slot to the QAction::triggered(bool) signal. So you can only use a slot with no arguments or a slot with a single bool argument.

Any run-time connect issues can be detected using return value of connect(). You'd better check it if you're experiencing some issues. Also, Qt Creator's autocomplete could help you to use SIGNAL() and SLOT() macros.

See also connect documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top