Results 1 to 9 of 9

Thread: Error accessing pointer to class member in main

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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 17:50. Reason: added a full example code

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

    starkid (15th October 2011)

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, 07: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, 14:53
  3. Replies: 2
    Last Post: 29th June 2009, 06:09
  4. Replies: 3
    Last Post: 19th February 2008, 13:10
  5. Accessing a class Object by pointer in another class
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2008, 15: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
  •  
Qt is a trademark of The Qt Company.