Question

I'm trying to add a context menu to a tree with the following code:

void MainWindow::FileTreeContextMenu(const QPoint& pos)
{
    QPoint globalPos = ui->fileTree->viewport()->mapToGlobal(pos);
    QMenu menu;
    menu.addAction("New Group");

    QAction* selectedItem = menu.exec(globalPos);
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->fileTree->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->fileTree,
            SIGNAL(customContextMenuRequested(const QPoint&)),
            this,
            SLOT(FileTreeContextMenu(const QPoint&)));
}

However, when I run this, I get the following error:

QObject::connect: No such slot MainWindow::FileTreeContextMenu(const QPoint&) 
  in ..\src\Fixer\mainwindow.cpp:23
QObject::connect:  (sender name:   'fileTree')
QObject::connect:  (receiver name: 'MainWindow')

What am I doing wrong?

Was it helpful?

Solution

What am I doing wrong?

It can happen if you omit to declare the slot as a real slot. You can either use the Q_SLOTS macros for that or just simply slots. In your case, the latter is more adequate because it is just an application and a mainwindow.

You also need to make sure you do not forget to use the Q_OBJECT macro in your header file.

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