qtcontextmenu

Custom context menu connecting in Qt


I have a problem with connecting custom menu in QListWidget, the connect function returns false. Here is the code:

I've got some main window class called MainWindow. In its constructor I have this line

connect(ui->notesWidget, SIGNAL(customContextMenuRequested(QPoint &)), 
        this, SLOT(contextMenuforNotesArea(QPoint &)));

where notesWidget is mentioned QListWidget.

ContextMenuforNotesArea(QPoint &) is defined like this

class MainWindow : public QMainWindow
{
public slots:
    void contextMenuforNotesArea(QPoint &pos);
};

void MainWindow::contextMenuforNotesArea(const QPoint &pos){
    QMessageBox::information(this, "a", "the function has been finally called");
    QMenu contextMenu(tr("Context menu"), this);
    contextMenu.addAction(new QAction(tr("Hello"), this));
    contextMenu.exec(mapToGlobal(pos));
}

I have also changed the property contextMenu in the listWidget to customContextMenu through form designer.


Solution

  • Change your SLOT signature as follows in the header file

    void contextMenuforNotesArea(const QPoint &pos);
    

    Also make sure, when you are doing the connection in the constructor, you do it after the calling of setupUi