Results 1 to 3 of 3

Thread: Doubt about QLabel * usage.

  1. #1
    Join Date
    Nov 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Doubt about QLabel * usage.

    Hi everyone,

    I am developing a program which creates dynamically labels and saves their handles in a QList.

    The labels are created OnMouse click and are destroyed OnMouse double click.

    Actually the program runs without problems, but I wonder if I am doing a correct usage of pointers in order to prevent runtime errors: I would like to know if the memory correspondent to the created label pointers will exists till mouseDoubleClick_slot is called.

    Here below an extract of my code.

    Qt Code:
    1. QList<QLabel*> *tipList; // declared in the .h file
    2.  
    3. void CommandWindow::mouseRelease_slot(QMouseEvent* e)
    4. {
    5. QLabel *tip = new QLabel(ui->customPlot);
    6. tipList->append(tip);
    7.  
    8. // ...
    9. }
    10.  
    11. void CommandWindow::mouseDoubleClick_slot(QMouseEvent* e)
    12. {
    13. if (tipList->size())
    14. {
    15. for(int i=0; i<(tipList->size()); i++)
    16. {
    17. QLabel *tipTmp = tipList->at(i);
    18. delete tipTmp;
    19. }
    20. tipList->clear();
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Doubt about QLabel * usage.

    Yes, it will exist.

    A simpler alternative to your mouse double click event is:

    Qt Code:
    1. {
    2. qDeleteAll(tipList);
    3. tipList->clear();
    4. }
    To copy to clipboard, switch view to plain text mode 

    By the way, keeping the list itself as a pointer usually does not make sense.
    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.


  3. #3
    Join Date
    Nov 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Doubt about QLabel * usage.

    Hi Wysota,

    Thanks for your answer. I have followed you suggestion, I have replaced the * QList declaration with a QList.

    My program seems to run as expected.

Similar Threads

  1. Replies: 1
    Last Post: 29th September 2009, 19:44
  2. Replies: 1
    Last Post: 2nd August 2008, 15:46
  3. doubt
    By AnithaRagupathy in forum Qt Programming
    Replies: 1
    Last Post: 8th February 2008, 06:06
  4. doubt
    By deepa.selvaraj in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2007, 09:23
  5. QT 4.1 in OSX I have a doubt
    By askot in forum Installation and Deployment
    Replies: 9
    Last Post: 25th February 2006, 06:14

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.