Results 1 to 3 of 3

Thread: button doesn't show up

  1. #1
    Join Date
    Dec 2010
    Posts
    16
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default button doesn't show up

    Hello,

    I am practicing the Qt tutorial here
    http://doc.qt.nokia.com/4.3/tutorial-t4.html

    Note that the QPushButton quit in constructor of MyWidget is a pointer. The example code works fine. But the button doesn't show up in the window after I changed the pointer to a stack variable, like

    Qt Code:
    1. QPushButton quit(tr("Quit"), this);
    2. quit.setGeometry(62, 40, 75, 30);
    3. quit.setFont(QFont("Times", 18, QFont::Bold));
    4.  
    5. connect(&quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 

    Can any expert point out why the button is not displayed after the change? Why is pointer important here? Thank you.

  2. #2
    Join Date
    Jan 2010
    Location
    Perth, Australia
    Posts
    37
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: button doesn't show up

    Your stack variable is a LOCAL variable in the constructor. Memory for local variables get destroyed when the function ends -- thus the button only exists from Line #1 of your post (when it's created) to the "}" after Line #5 (when it gets destroyed).

    To stop the button from getting destroyed, you need to use the "new" keyword to allocate it on the heap instead of on the stack. This way, it continues to exist even after the function ends.

    Note that the QPushButton quit in constructor of MyWidget is a pointer
    Small technicality: The QPushButton is not a pointer; the QPushButton is an object in the heap, and the pointer tells you the QPushButton's exact location within the heap.
    Last edited by hackerNovitiate; 13th December 2010 at 07:45. Reason: updated contents

  3. #3
    Join Date
    Dec 2010
    Posts
    16
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: button doesn't show up

    thank you.

Similar Threads

  1. QGraphicsTextItem doesn't show up
    By chapu in forum Qt Programming
    Replies: 6
    Last Post: 12th July 2010, 00:48
  2. QCalenderWidget doesn't show the month name and Day name.
    By jthacker in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2010, 20:43
  3. Application running but doesn't show up!!!
    By joandelason in forum Newbie
    Replies: 4
    Last Post: 21st March 2010, 09:37
  4. Custom Widget doesn t show.
    By Frej in forum Qt Tools
    Replies: 12
    Last Post: 11th March 2010, 10:48
  5. QSystemTrayIcon doesn't show icon ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 25th January 2007, 19:17

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.