Results 1 to 9 of 9

Thread: Error accessing pointer to class member in main

  1. #1
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Error accessing pointer to class member in main

    In a subclass of QMainWindow, I have created a pointer to a QPushButton:
    Qt Code:
    1. class appWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. appWindow();
    7. QPushButton *quit;
    To copy to clipboard, switch view to plain text mode 

    I am trying to use this button to exit the application in main.cpp:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication VocabAide(argc, argv);
    4. appWindow mainWindow; //instance of class appWindow
    5.  
    6. QObject::connect(mainWindow.*quit, SIGNAL(clicked()), &VocabAide, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 

    appwindow.h is included in main.cpp. I am receiving a single error: ‘quit’ was not declared in this scope.

    I'm wondering if the problem has to do with class instance mainWindow not being a child of the QApplication. I don't understand how QApplication fits (or how I make it fit) into the parent-child hierarchy since it doesn't derive from QObject.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Error accessing pointer to class member in main

    How about mainWindow->quit...

    (You need to brush up your C++ skills... Have you ever seen a ".*" reference anywhere?)

  3. #3
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Error accessing pointer to class member in main

    I've tried mainWindow->quit already. I get the error:

    base operand of ‘->’ has non-pointer type ‘appWindow’

    My understanding is that the object->member construction only works in the case in which the object is a pointer, which mainWindow is not.

    I got .* from http://www.oopweb.com/CPP/Documents/...usplus15.html:

    "To use a pointer to a member in combination with an object the pointer to member field selector ( .*) must be used."

  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: Error accessing pointer to class member in main

    Just
    Qt Code:
    1. connect(mainWindow.quit, //...
    To copy to clipboard, switch view to plain text mode 
    should do the trick. //you don't need to deference the quit pointer, since the connection takes pointers as parameters.

    And don't forget to take and pass a parent to the QMainWindow's constructor. //just to avoid future problems like memory leak/object slicing.

  5. #5
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Error accessing pointer to class member in main

    Quote Originally Posted by Zlatomir View Post
    Just

    And don't forget to take and pass a parent to the QMainWindow's constructor. //just to avoid future problems like memory leak/object slicing.
    How does that work? QMainWidget can take a QWidget as a parent, but QApplication is not derived from QWidget. Is it necessary to create an intermediate widget in main to pass as a parent?

    Also, there is still a problem. I'm getting:

    error: no matching function for call to ‘QObject::connect(QPushButton*&, const char [11], QApplication*, const char [8])

    The first argument, QPushButton*&, looks like a problem with mainWindow.quit.
    Last edited by starkid; 15th October 2011 at 18:37. Reason: tried coding suggestion

  6. #6
    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: Error accessing pointer to class member in main

    No, you don't need an "intermediary" widget, you pass 0 (NULL), the only thing is that you are responsible to delete the widgets that don't get a parent (or get that NULL) so that you trigger the proper deletion of it's children widgets, this can be done either by allocating the "mainWindow" on the stack in the main function or call delete if you allocated it on stack.

    LE: Full example code:
    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QPushButton>
    4.  
    5. class appWindow : public QMainWindow
    6. {
    7. Q_OBJECT
    8. public:
    9. appWindow(QWidget* parent = 0);
    10. QPushButton *quit;
    11. };
    12.  
    13. appWindow::appWindow(QWidget* parent) : QMainWindow(parent)
    14. {
    15. quit = new QPushButton("quit", this);
    16. }
    17.  
    18. int main(int argc, char *argv[])
    19. {
    20. QApplication app(argc, argv);
    21.  
    22. appWindow w;
    23. QObject::connect(w.quit, SIGNAL(clicked()), &app, SLOT(quit()));
    24. w.show();
    25.  
    26. return app.exec();
    27. }
    28.  
    29. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by Zlatomir; 15th October 2011 at 18:50. Reason: added a full example code

  7. The following user says thank you to Zlatomir for this useful post:

    starkid (15th October 2011)

  8. #7
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Error accessing pointer to class member in main

    Quote Originally Posted by Zlatomir View Post
    the only thing is that you are responsible to delete the widgets that don't get a parent (or get that NULL)
    I don't understand how passing NULL prevents memory leak if I have to delete the object to which it is passed. Wouldn't I also have to delete it explicitly if I passed nothing at all?
    Last edited by starkid; 15th October 2011 at 19:11.

  9. #8
    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: Error accessing pointer to class member in main

    Quote Originally Posted by starkid View Post
    I don't understand how passing NULL prevents memory leak if I have to delete the object to which it is passed. Wouldn't I also have to delete it explicitly if I passed nothing at all?
    If you pass nothing (pass the default NULL) and your widget doesn't get a parent (by for example if you add your widget into a layout) you need to delete your widget, i out case it is deleted because it is allocated on stack.

    If you allocate it on the heap you do something like:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. appWindow *w = new appWindow(); //default parent is null, so our widget doesn't have a parent
    6. QObject::connect(w->quit, SIGNAL(clicked()), &app, SLOT(quit())); //use the arrow
    7. w->show();
    8.  
    9. int ret_value = app.exec(); //hold the value returned by QApplication exec
    10. delete w; //delete the widget, this line will trigger the deletion of the quit pointer (because it is a child of your widget)
    11. return ret_value; //return the value...
    12. }
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Error accessing pointer to class member in main

    Thanks for the full example. I forgot to include QPushButton in main.cpp. Works fine now.

Similar Threads

  1. Pointer to non static member functions within the class
    By Lykurg in forum General Programming
    Replies: 3
    Last Post: 24th April 2011, 08:37
  2. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 15:53
  3. Replies: 2
    Last Post: 29th June 2009, 07:09
  4. Replies: 3
    Last Post: 19th February 2008, 14:10
  5. Accessing a class Object by pointer in another class
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2008, 16:59

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.