Results 1 to 5 of 5

Thread: class calling other class functions?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: class calling other class functions?

    Quote Originally Posted by Hardstyle View Post
    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).
    Last edited by Coises; 2nd June 2010 at 02:50.

Similar Threads

  1. QtConncurrent - calling function within the class
    By jacek_ in forum Qt Programming
    Replies: 5
    Last Post: 28th October 2009, 17:37
  2. calling Gui treeWidget in Thread class
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2007, 09:14
  3. DLL exporting functions, variables, class
    By Shuchi Agrawal in forum Newbie
    Replies: 1
    Last Post: 25th April 2007, 11:40
  4. class functions are not listing [ in VC++ editor]
    By joseph in forum Qt Programming
    Replies: 8
    Last Post: 4th April 2007, 11:43
  5. Static functions and class members
    By Raistlin in forum General Programming
    Replies: 5
    Last Post: 22nd December 2006, 10:00

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.