Results 1 to 3 of 3

Thread: QGridLayout: Getting the list of QWidget added

  1. #1
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGridLayout: Getting the list of QWidget added

    Suppose I have something like:

    Qt Code:
    1. QGridLayout layout;
    2. layout.addWidget(new QWidget());
    3. layout.addWidget(new QWidget());
    To copy to clipboard, switch view to plain text mode 

    What is the method that I can use to get the list of the added QWidgets ?

    Something like the imaginary "getAddedWidgets()" below:

    Qt Code:
    1. QList<QWidget*> addedWidgets = layout.getAddedWidgets();
    2. Q_ASSERT( addedWidgets.size() == 2 );
    To copy to clipboard, switch view to plain text mode 


    Added after 1 29 minutes:


    The example code below shows how you could iterate over each item:

    Qt Code:
    1. QGridLayout layout;
    2.  
    3. // add 3 items to layout
    4. layout.addItem(new QSpacerItem(2,3), 0, 0, 1, 1);
    5. layout.addWidget(new QWidget);
    6. layout.addWidget(new QWidget);
    7.  
    8. // sanity checks
    9. Q_ASSERT(layout.count() == 3);
    10. Q_ASSERT(layout.itemAt(0));
    11. Q_ASSERT(layout.itemAt(1));
    12. Q_ASSERT(layout.itemAt(2));
    13. Q_ASSERT(layout.itemAt(3) == NULL);
    14.  
    15. // iterate over each, only looking for QWidgetItem
    16. for(int idx = 0; idx < layout.count(); idx++)
    17. {
    18. QLayoutItem * item = layout.itemAt(idx);
    19. if(dynamic_cast<QWidgetItem *>(item)) <-- Note! QWidgetItem, and not QWidget!
    20. item->widget()->hide(); <-- widget() will cast you a QWidget!
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ShaChris23; 12th November 2010 at 01:54.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGridLayout: Getting the list of QWidget added

    dynamic_cast should be avoided when dealing with QObjects.
    Use qobject_cast instead.

  3. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGridLayout: Getting the list of QWidget added

    Quote Originally Posted by tbscope View Post
    dynamic_cast should be avoided when dealing with QObjects.
    Use qobject_cast instead.
    why so? is dynamic_cast is slower than qobject_cast? I do have RTTI always in my compiler, so given that, can you plz elaborate?

Similar Threads

  1. Replies: 1
    Last Post: 28th October 2010, 22:36
  2. Set size when insert a QWidget into a QGridLayout
    By Tondog in forum Qt Programming
    Replies: 3
    Last Post: 4th June 2010, 06:35
  3. Replies: 1
    Last Post: 19th April 2010, 11:27
  4. Replies: 0
    Last Post: 9th March 2010, 08:44
  5. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 13:01

Tags for this Thread

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.