Results 1 to 5 of 5

Thread: class calling other class functions?

  1. #1
    Join Date
    May 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Talking class calling other class functions?

    So, i want to call a function of my main class "mywidget" and i don't know how...please help me fast

    here are the codes:
    class Buttonublic QPushButton
    {
    public:
    int y,x;
    Button(QWidget *parent);
    void apasat();
    };
    class MyWidgetublic 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

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: class calling other class functions?

    'verifica' isn't a static member function so where is your class instance variable?

  3. #3
    Join Date
    May 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: class calling other class functions?

    is posted in newbie section...i don't know what you mean

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: class calling other class functions?

    He meant that you can't do this:
    Qt Code:
    1. void Button::apasat()
    2. {
    3. MyWidget::verifica(y,x);
    4. }
    To copy to clipboard, switch view to plain text mode 

    You need to create an object of type MyWidget, something like this:
    Qt Code:
    1. MyWidget obj; //create an instance of MyWidget class
    2. obj.verifica(); // run the member function;
    To copy to clipboard, switch view to plain text mode 

    An if MyWidget is an GUI widget you might want to allocate memory on the heap for objects, like this:
    Qt Code:
    1. MyWidget *ptr = new MyWidget(parent); //alocate with "new" and give a parent
    2. ptr->verifica(); // call member function with pointer
    To copy to clipboard, switch view to plain text mode 

    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.

  5. #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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.