Results 1 to 5 of 5

Thread: Difference of Programming Style

  1. #1
    Join Date
    Nov 2010
    Location
    Jakarta, Indonesia
    Posts
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Difference of Programming Style

    Dear All,

    I am beginner in Qt, and wanna ask about programming style :

    What's difference of :

    QDialog xdialog(this);
    QHBoxLayout myLayout ;
    myLayout.addWidget(widget,0,0);
    dialog.setLayout(myLayout); // error happened

    with

    QDialog xdialog(this);
    QHBoxLayout *myLayout = new QHBoxLayout;
    myLayout->addWidget(widget,0,0);
    xdialog.setLayout(myLayout); // no error occurs


    Why we use 1st script, it returns error ? How to fix it if we want to use 1st script too ?

    Thanks

  2. #2
    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: Difference of Programming Style

    It's all about what parameter the setLayout(...) expects.
    To correct the first "script" you can call it like this: xdialog.setLayout(&myLayout);

    But in most of the cases you need the layouts and widgets on the heap (use the pointers and new to allocate memory), maybe you should read about dynamic memory allocation in C++ first.

    LE: this isn't about programming style, it's about what life-time of objects you need (stack or heap) and usage of pointers.

  3. #3
    Join Date
    May 2010
    Posts
    61
    Thanks
    2
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Difference of Programming Style

    Hi alphazero,

    If you look at the QWidget::setLayout documentation you will see that this function expects a pointer to a QLayout.

    The 2nd option that you are presenting works just because of this, because you are using a pointer. And this is the best way to do it.

    If you still want to use the 1st option, we can give the function what it expects.
    Do this by :
    Qt Code:
    1. dialog.setLayout([B]&[/B]myLayout);
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Wladek
    One second is long, everything longer than two seconds is definitely too long.

  4. #4
    Join Date
    Nov 2010
    Location
    Jakarta, Indonesia
    Posts
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Difference of Programming Style

    Hi Zlatomir and wladek
    thanks for your fast replies,

    it works using & , and I still learning about C++ and Qt too..

  5. #5
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Difference of Programming Style

    I tend to favor compact code so what you presented would look more like
    Qt Code:
    1. QDialog xdialog(this);
    2. xdialog.setLayout(new QGridLayout);
    3. xidalog.layout()->addWidget(someWidgetPtr);
    To copy to clipboard, switch view to plain text mode 

    and that IS about style.

Similar Threads

  1. Do Qt Style Sheets support List-style-image?
    By daiheitan in forum Qt Programming
    Replies: 6
    Last Post: 12th March 2010, 01:41
  2. Replies: 2
    Last Post: 9th February 2010, 16:27
  3. Problems with Gradient style used through Style sheet on ARM platform
    By puneet.narsapur in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 25th January 2010, 12:48
  4. Replies: 7
    Last Post: 16th February 2009, 21:59
  5. Replies: 1
    Last Post: 7th February 2007, 00:12

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.