title: rightclick event in Qt to open a context menu
body: <p>I have a segment of code that calls a mousePressEvent. I have the left-click output the coordinates of the cursor, and I have rightclick do the same, but I also want to have the rightclick open a context menu. The code I have so far is:</p>
  
  <pre><code>void plotspace::mousePressEvent(QMouseEvent*event)
  {
      double trange = _timeonright - _timeonleft;
      int twidth = width();
      double tinterval = trange/twidth;
  
      int xclicked = event-&gt;x();
  
      _xvaluecoordinate = _timeonleft+tinterval*xclicked;
  
  
  
      double fmax = Data.plane(X,0).max();
      double fmin = Data.plane(X,0).min();
      double fmargin = (fmax-fmin)/40;
      int fheight = height();
      double finterval = ((fmax-fmin)+4*fmargin)/fheight;
  
      int yclicked = event-&gt;y();
  
      _yvaluecoordinate = (fmax+fmargin)-finterval*yclicked;
  
      cout&lt;&lt;"Time(s): "&lt;&lt;_xvaluecoordinate&lt;&lt;endl;
      cout&lt;&lt;"Flux: "&lt;&lt;_yvaluecoordinate&lt;&lt;endl;
      cout &lt;&lt; "timeonleft= " &lt;&lt; _timeonleft &lt;&lt; "\n";
  
      returncoordinates();
  
      emit updateCoordinates();
  
      if (event-&gt;button()==Qt::RightButton)
      {
              contextmenu-&gt;setContextMenuPolicy(Qt::CustomContextMenu);
  
              connect(contextmenu, SIGNAL(customContextMenuRequested(const QPoint&amp;)),
              this, SLOT(ShowContextMenu(const QPoint&amp;)));
  
              void A::ShowContextMenu(const QPoint &amp;pos) 
              {
                  QMenu *menu = new QMenu;
                  menu-&gt;addAction(tr("Remove Data Point"), this,  
                  SLOT(test_slot()));
  
                  menu-&gt;exec(w-&gt;mapToGlobal(pos));
              }
  
      }   
  
  }
  </code></pre>
  
  <p>I know that my problem is very fundamental in nature, and that 'contextmenu' is not properly declared. I have pieced together this code from many sources, and do not know how to declare something in c++. Any advice would be greatly appreciated.</p>