PDA

View Full Version : class calling other class functions?



Hardstyle
31st May 2010, 18:10
So, i want to call a function of my main class "mywidget" and i don't know how...please help me fast:D

here are the codes:


class Button:public QPushButton
{
public:
int y,x;
Button(QWidget *parent);
void apasat();
};




class MyWidget:public QWidget
{
private:
QPushButton *quit;
Button *but[20][20];
public:
MyWidget(QWidget *parent=0,const char *name=0);
void verifica(int y, int x);
};


and ti try to call the function like this:

void Button::apasat()
{
MyWidget::verifica(y,x);
}

help me plz

squidge
31st May 2010, 18:21
'verifica' isn't a static member function so where is your class instance variable?

Hardstyle
31st May 2010, 18:47
is posted in newbie section...i don't know what you mean:D

Zlatomir
31st May 2010, 19:07
He meant that you can't do this:

void Button::apasat()
{
MyWidget::verifica(y,x);
}

You need to create an object of type MyWidget, something like this:


MyWidget obj; //create an instance of MyWidget class
obj.verifica(); // run the member function;


An if MyWidget is an GUI widget you might want to allocate memory on the heap for objects, like this:


MyWidget *ptr = new MyWidget(parent); //alocate with "new" and give a parent
ptr->verifica(); // call member function with pointer


If you don't understand take at least a tutorial of c++ first, and than a Qt one (or Qt examples), but i strongly recommend a book.

Coises
2nd June 2010, 03:38
please help me fast

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).