Results 1 to 3 of 3

Thread: best object creation method

  1. #1
    Join Date
    Apr 2014
    Posts
    53
    Thanks
    9

    Default best object creation method

    Hi,
    I would like to ask , which declaration of object is faster?

    Qt Code:
    1. QLabel lbl_name;
    2. lbl_name.setText("Name:");
    To copy to clipboard, switch view to plain text mode 
    or this:
    Qt Code:
    1. QLabel *lbl_name;
    2. lbl_name= new QLabel("Name:");
    To copy to clipboard, switch view to plain text mode 

    Or in other words, is it best to use pointers to objects or objects themselves ?
    I am going to use this for creating dialogs and windows in my app.
    TIA

  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: best object creation method

    When creating instances of subclasses of QObject it is almost always required to use pointers.
    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
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: best object creation method

    The fastest option is most likely a combination
    Qt Code:
    1. QLabel label("text");
    To copy to clipboard, switch view to plain text mode 
    i.e. on the stack and passing the text to the constructor instead of calling a setter.

    However, as wysota has already said, widgets are most often created on the heap because they usually form a tree and their respective parent widget will attempt to delete them on its own destruction.
    Which would not go together well with an object created on the stack

    Cheers,
    _

Similar Threads

  1. TypeError: Object [object Object] has no method 'sendData'
    By TheIndependentAquarius in forum Qt Quick
    Replies: 2
    Last Post: 30th November 2013, 05:54
  2. QThread and New Object Creation
    By pratham_shah in forum Qt Programming
    Replies: 6
    Last Post: 26th April 2013, 11:11
  3. Input Method Editor Creation Issue
    By Asim Umair in forum Qt Programming
    Replies: 0
    Last Post: 16th October 2012, 12:14
  4. Cancelling an Object Creation
    By bbad68 in forum Newbie
    Replies: 1
    Last Post: 4th March 2010, 20:22
  5. method creation
    By MarkoSan in forum Newbie
    Replies: 6
    Last Post: 30th May 2007, 08:47

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.