4

I'm working with OpenCV and Qt 5. I need to pass a mouse callback to a namedwindow for some work I'm doing. However, I can't get it to see any of the private member variables of my class.

Here's some code:

class testWizard : public QWizard
{
  Q_OBJECT


  public:
   testWizard();
  ~testWizard();

   friend void mouseHandler(int, int, int, void*);



   private:

    cv::Mat preview;

    bool drag; 
    cv::Rect rect;   
};

The friend function:

void mouseHandler(int event, int x, int y, void* param)
{

 cv::Point p1, p2;

 if(event == CV_EVENT_LBUTTONDOWN && !drag)
 {
   p1 = cv::Point(x,y);
   drag = true;
 }

 if(event == CV_EVENT_LBUTTONDOWN && drag)
 {
   cv::Mat temp;
   preview.copyTo(temp);
 }

}

I don't know what I'm doing wrong. I'm pretty sure this is the correct way to declare this. It is telling me that preview, and drag are undeclared identifiers. Unfortunately I need to do it this way since I need access to the private members and passing a pointer to a member function isn't possible because of the hidden this argument.

Can anyone help? Thank you!

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
John
  • 347
  • 1
  • 7
  • 14

1 Answers1

4

With the friend declaration your function would have access to the members of a testWizard object. However, you still need to provide an object or a pointer to such an object to access the variables:

testWizard* wizard = getTestWizard(); // no idea how to do that
if(event == CV_EVENT_LBUTTONDOWN && !wizard->drag) { ... }
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Ah yes, thank you. Are you required to have a constructor with no arguments or can you do it with any constructor type? – John Aug 25 '13 at 03:20
  • @John: I have no idea how the framework is used! You may be able to pass a pointer to the `testWizard` as the `param` argument or instead of registering a function pointer you may be able to register a function object holding a pointer to the object. You certainly don't want to construct a `testWizard` as that wouldn't have any specific value. – Dietmar Kühl Aug 25 '13 at 03:26