Search code examples
c++qtsignals-slots

Function pointer as SLOT argument


I know that it has similar topics but I beginner C++ programmer and cannot understand their because they have few distinctions from my problem.
I define so type

typedef void (MainWindow::*MenuListener)(QAction *);

To I can pass as argument a method of my class but in function I need to pass it into SLOT. How I can do it? Below my function:

void MainWindow::showMenu(int count, MenuListener listener)
{
    if(0 < count) {
        int flags = ContextMenu::Remove;

        if(1 == count) { flags |= ContextMenu::Edit; }

        ContextMenu menu("ContextMenu", flags, this);
        connect(&menu, SIGNAL(triggered(QAction*)), this, SLOT(listener));
        menu.exec();
    }
}

Function showMenu() is the method of MainWindow class such as function that I try to pass like listener argument.


Solution

  • Time to learn 3 years old Qt's New Signal Slot Syntax:

    connect(&menu, &MainWindow::triggered, this, listener);
    

    These old SIGNAL and SLOT macros won't accept function pointers, they expect function signatures.