2

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.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Aspect
  • 35
  • 4
  • 1
    Do you get any debug output when you step over `connect`? When `connect` fails, it usually gives useful information in the debug output. – sashoalm Nov 21 '12 at 08:55

1 Answers1

0

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

warunanc
  • 2,221
  • 1
  • 23
  • 40
  • yeah, thanks now it works, just the menu is created a bit above the mouse, this is the function void MainWindow::contextMenuforNotesArea(const QPoint &pos){ QMenu contextMenu(tr("Context menu"), this); contextMenu.addAction(new QAction(tr("Hello"), this)); contextMenu.exec(mapToGlobal(pos)); } sorry about the mess, but Im new here and dont know why the code didnt appeared as a code, I have written 4 spaces before the lines :/ – Aspect Nov 21 '12 at 15:27
  • you need to call the respective widget's `mapToGlobal(const QPoint & pos)` method, in this case `contextMenu.exec(mapToGlobal(pos))` should be changed as `contextMenu.exec(ui->notesWidget->mapToGlobal(pos))`, check if that works – warunanc Nov 22 '12 at 05:08