Results 1 to 5 of 5

Thread: Best way to access one element (widget or layout)

  1. #1
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Best way to access one element (widget or layout)

    Hello,

    I would llike to have an advice about the way to access elements in QT.

    Most of the tutorials I've seen show classes with private pointers to access the elements like this:

    Qt Code:
    1. class X {
    2.  
    3. private:
    4. QLineEdit * toto;
    5. QLineEdit * toto2;
    6. QLineEdit * toto3;
    7. ....
    8. }
    To copy to clipboard, switch view to plain text mode 

    If the GUI is complex, there is quickly a long list of variables.

    I have also found "findChild" functions.

    What is the best way to manipulate elements ? Are there differences in terms of memory occupation, access time... ?

    Thanks in advance.

  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 way to access one element (widget or layout)

    The best way is through private members (e.g. pointers) and proper encapsulation.
    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
    Aug 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Best way to access one element (widget or layout)

    Ok, thanks for your answer! I'm going to continue with this way to implement my code.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Best way to access one element (widget or layout)

    There's a small overhead of a pointer-per-widget, but member variables are the way to do it for the vast majority of cases. If you use Designer and uic then the generated code uses member variables in exactly the fashion you describe.

    The alternative is to not keep those pointers, but uniquely name each widget, and then use QObject::findChild() to walk recursively down the QObject ownership tree looking for a match (or matches) on name and type. A whole tree walk comparing and casting is much slower then direct access via a stored pointer. This approach is useful for dynamically constructed UIs where the structure cannot be known in advance (perhaps using QUiLoader).

  5. #5
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Best way to access one element (widget or layout)

    Thanks a lot, ChrisW67, for you additional answer.

Similar Threads

  1. how to access layout within another layout in qt
    By lakeat in forum Qt Programming
    Replies: 3
    Last Post: 8th January 2013, 06:57
  2. Replies: 2
    Last Post: 29th December 2011, 12:27
  3. Access DOM element from AxWidget
    By giusepped in forum Qt Programming
    Replies: 0
    Last Post: 21st October 2011, 16:03
  4. how to access ith element of an qstring list?
    By aurora in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2011, 09:37
  5. Replies: 0
    Last Post: 3rd August 2011, 12:07

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.