It’s a 20-to-1 shot, but what the hell:
Try changing void verifica(int y, int x); to static void verifica(int y, int x);.
If you don’t get error messages like “accessing non-static member in static function†then you have your quick fix.
Otherwise you're going to have to learn something.
The form MyWidget::verifica (in the context you’ve used it) requires that verifica is a static function. A static function has no connection to any particular instance of its class. If that’s not true of verifica then you can’t use the static keyword, and you can’t call it without reference to a specific instance of MyWidget.
(If you don’t know the difference between a class and an instance of the class, we really can’t help you here... you have no choice but to learn more about how C++ works before you try to program in it.)
C++ has no concept of “parent†instances. Even though the pointers to your Buttons are declared within a MyWidget — and even if you have only one MyWidget in your program — your Buttons don’t know that. When you try to access an ordinary (non-static) member of MyWidget from a member function in Button, it has to know which MyWidget. You know... it seems obvious... but the code does not know, and you have to tell it.
So, how can you do that?
1. Change the constructor for Button from Button(QWidget *parent); to Button(MyWidget *parent);. To make that work, you will need to add a “forward declarationâ€: class MyWidget; before the declaration of Button.
2. Add a variable of type MyWidget* — say, MyWidget *container; — to Button and save the value of parent there.
3. Where you construct your Buttons, make sure you pass a pointer to the containing MyWidget as the parent. For example, if you create your Buttons within a member function of MyWidget, use new Button(this) to construct them.
4. When you call verifica in a member function of Button, call it like this: container->verifica(y,x).
Bookmarks