Results 1 to 9 of 9

Thread: Why can I only show a QLabel by declaring it as a pointer?

  1. #1
    Join Date
    Nov 2012
    Posts
    20
    Thanked 1 Time in 1 Post

    Default Why can I only show a QLabel by declaring it as a pointer?

    This code displays text on my qwidget:

    Qt Code:
    1. QLabel *button = new QLabel(this);
    2. button->setText("dog");
    3. button->show();
    To copy to clipboard, switch view to plain text mode 

    but this does not:

    Qt Code:
    1. QLabel button (this);
    2. button.setText("dog");
    3. button.show()
    To copy to clipboard, switch view to plain text mode 

    I'm a bit confused why.

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Why can I only show a QLabel by declaring it as a pointer?

    It has nothing to do with the way you allocate it, more likely its a scope problem.
    Show the full code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2012
    Posts
    20
    Thanked 1 Time in 1 Post

    Default Re: Why can I only show a QLabel by declaring it as a pointer?

    Qt Code:
    1. Widget::Widget(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::Widget)
    4. {
    5. ui->setupUi(this);
    6. setFixedSize(QSize(700,300));
    7.  
    8. QLabel label (this);
    9. label.setText("dog");
    10. label.show();
    11.  
    12.  
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Why can I only show a QLabel by declaring it as a pointer?

    Basics of C / C + +. What happens to an object label after leaving the Widget constructor?

  5. #5
    Join Date
    Nov 2012
    Posts
    20
    Thanked 1 Time in 1 Post

    Default Re: Why can I only show a QLabel by declaring it as a pointer?

    It gets destroyed!

    Still not sure why a pointer helps though.
    Last edited by tom989; 16th November 2012 at 16:45.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Why can I only show a QLabel by declaring it as a pointer?

    Once more basics of C/C++ : because deleting of pointer DON'T destroy object.

  7. #7
    Join Date
    Nov 2012
    Posts
    20
    Thanked 1 Time in 1 Post

    Default Re: Why can I only show a QLabel by declaring it as a pointer?

    Calm down. Oh and C isn't relevant here because it has no classes/objects.

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

    Default Re: Why can I only show a QLabel by declaring it as a pointer?

    Quote Originally Posted by tom989 View Post
    Oh and C isn't relevant here because it has no classes/objects.
    C also has something to do with issue like this because it has functions that have scope (constructor is also function).

    Anyway a small explanation about Lesiok's comment, a better way to say is: pointer getting out of scope doesn't destroy the object (i talk about normal pointers not the smart ones obviously) - don't confuse what he said with calling C++'s delete pointer.
    So:
    1) when you allocate the object in a function scope it get destroyed at the end of scope (so it doesn't has a chance to actually show on screen)
    and
    2) when you use a pointer you allocate the object on the heap and only the pointer gets out of scope (the actual object still exist and it will be showed eventually) //this practice (to let the pointer to a heap object go out of scope without calling delete) is usually not a good idea - but Qt has the parent-child mechanism that keep a (copy of) the pointer and will delete it when the parent will be deleted (or goes out of scope). You can read a little more about parent-child in QObjects here and also i recommend to read a C++ book (search of "Thinking in C++" and you will find two free volumes that are free to download)

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Why can I only show a QLabel by declaring it as a pointer?

    Quote Originally Posted by tom989 View Post
    Calm down. Oh and C isn't relevant here because it has no classes/objects.
    Of course C has objects. And the issue is not related to "classes" since if you had an "int" here, it would behave exactly the same way.

    Quote Originally Posted by tom989 View Post
    Calm down. Oh and C isn't relevant here because it has no classes/objects.
    Of course C has objects. And the issue is not related to "classes" since if you had an "int" here, it would behave exactly the same way.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 13th February 2012, 16:54
  2. show the position in qlabel
    By ready in forum Qt Programming
    Replies: 8
    Last Post: 28th March 2011, 08:58
  3. QLabel pointer - seg fault
    By Tomasz in forum Newbie
    Replies: 3
    Last Post: 26th November 2010, 15:53
  4. QLabel and Mouse pointer Issue
    By newb in forum Qt Programming
    Replies: 3
    Last Post: 24th July 2010, 12:31
  5. Show Image on a QLabel
    By ^NyAw^ in forum Newbie
    Replies: 11
    Last Post: 15th April 2008, 16:45

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.