Results 1 to 9 of 9

Thread: how to refer a dynamically added table widget inside a scroll area widget?

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default how to refer a dynamically added table widget inside a scroll area widget?

    I have created a scroll area , inside that scroll area added a QToolBox...
    And in run time i'm adding pages to QToolBox....
    Each page contains a scroll area, inside that scroll area , number of QCheckBoxes are present....
    Now i want to get pointer to particular QCheckBox...by passing pagename and CheckBox name...

    Is that possible? If so please give some hint...
    my ui file
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>FilterColl</class>
    4. <widget class="QDialog" name="FilterColl">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>686</width>
    10. <height>615</height>
    11. </rect>
    12. </property>
    13. <property name="sizePolicy">
    14. <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
    15. <horstretch>0</horstretch>
    16. <verstretch>0</verstretch>
    17. </sizepolicy>
    18. </property>
    19. <property name="windowTitle">
    20. <string>Dialog</string>
    21. </property>
    22. <layout class="QGridLayout" name="gridLayout">
    23. <item row="0" column="0" colspan="4">
    24. <widget class="QScrollArea" name="scrollArea">
    25. <property name="widgetResizable">
    26. <bool>true</bool>
    27. </property>
    28. <widget class="QWidget" name="scrollAreaWidgetContents">
    29. <property name="geometry">
    30. <rect>
    31. <x>0</x>
    32. <y>0</y>
    33. <width>666</width>
    34. <height>566</height>
    35. </rect>
    36. </property>
    37. <layout class="QGridLayout" name="gridLayout_2">
    38. <item row="0" column="0">
    39. <widget class="QToolBox" name="toolBox">
    40. <property name="sizePolicy">
    41. <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
    42. <horstretch>0</horstretch>
    43. <verstretch>0</verstretch>
    44. </sizepolicy>
    45. </property>
    46. <property name="currentIndex">
    47. <number>0</number>
    48. </property>
    49. <widget class="QWidget" name="page_2">
    50. <property name="geometry">
    51. <rect>
    52. <x>0</x>
    53. <y>0</y>
    54. <width>648</width>
    55. <height>521</height>
    56. </rect>
    57. </property>
    58. <attribute name="label">
    59. <string>STATUS</string>
    60. </attribute>
    61. </widget>
    62. </widget>
    63. </item>
    64. </layout>
    65. </widget>
    66. </widget>
    67. </item>
    68. <item row="1" column="3">
    69. <spacer name="horizontalSpacer">
    70. <property name="orientation">
    71. <enum>Qt::Horizontal</enum>
    72. </property>
    73. <property name="sizeHint" stdset="0">
    74. <size>
    75. <width>40</width>
    76. <height>20</height>
    77. </size>
    78. </property>
    79. </spacer>
    80. </item>
    81. <item row="1" column="1">
    82. <widget class="QPushButton" name="pushButton_OK">
    83. <property name="text">
    84. <string>OK</string>
    85. </property>
    86. </widget>
    87. </item>
    88. <item row="1" column="2">
    89. <widget class="QPushButton" name="pushButton_CANCEL">
    90. <property name="text">
    91. <string>CANCEL</string>
    92. </property>
    93. </widget>
    94. </item>
    95. <item row="1" column="0">
    96. <spacer name="horizontalSpacer_2">
    97. <property name="orientation">
    98. <enum>Qt::Horizontal</enum>
    99. </property>
    100. <property name="sizeHint" stdset="0">
    101. <size>
    102. <width>40</width>
    103. <height>20</height>
    104. </size>
    105. </property>
    106. </spacer>
    107. </item>
    108. </layout>
    109. </widget>
    110. <resources/>
    111. <connections/>
    112. </ui>
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to refer a dynamically added table widget inside a scroll area widget?

    In my case i suggest you to use SIGNAL`s and SLOT`s they are very useful for your purpose i guess.

    http://developer.qt.nokia.com/forums/viewthread/4646

    take a look at that, i think there is a variety of solutions

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

    Default Re: how to refer a dynamically added table widget inside a scroll area widget?

    It's a classic OOP problem that has nothing to do with Qt. If you encapsulate one object inside another one, you need to provide means (e.g. methods) in the external object to access the internal one.
    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.


  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to refer a dynamically added table widget inside a scroll area widget?

    Qt provides such means.

    Qt Code:
    1. QWidget* find( const QString& pageTitle, const QString& childName )
    2. {
    3. QCheckBox* ret = NULL;
    4. for( int i = 0; i < tool_box_widget->count(); ++i )
    5. {
    6. if( tool_box_widget->itemText( i ) == pageTitle )
    7. {
    8. ret = tool_box_widget->widget( i ).findChild( childName );
    9. }
    10. }
    11. return ret;
    12. }
    To copy to clipboard, switch view to plain text mode 
    It's not a working solution but should push you in a right direction.

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

    Default Re: how to refer a dynamically added table widget inside a scroll area widget?

    Never use findChild() unless you have no other choice (and almost always you do). Besides it only works for QObjects parented to the external object which is not always the case.

    Qt Code:
    1. class Ext {
    2. public:
    3. Int* intObj() const { return m_internalObject; }
    4. private:
    5. Int* m_internalObject;
    6. };
    To copy to clipboard, switch view to plain text mode 
    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.


  6. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to refer a dynamically added table widget inside a scroll area widget?

    What's wrong with findChild(), doc doesn't mention anything?

    Anyway, my point was that Qt already provides means of traversing widgets hierarchy, and knowing what you're looking for you don't need anything else.

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

    Default Re: how to refer a dynamically added table widget inside a scroll area widget?

    Quote Originally Posted by Spitfire View Post
    What's wrong with findChild(), doc doesn't mention anything?
    Consider the following two samples and try to come up with your own conclusions.


    Qt Code:
    1. Object * Cls::goodWay() const {
    2. return m_obj;
    3. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. template<class T> QObject *findChildSimulationHelper(QObject *parent, QString name) {
    2. QObjectList list = parent->children();
    3. foreach(QObject *obj, list) {
    4. if(qobject_cast<T*>(obj) && obj->objectName()==name) return obj;
    5. QObject *found = findChildSimulationHelper(obj);
    6. if(found) return found;
    7. }
    8. return 0;
    9. }
    10.  
    11. QObject *Cls::badWay() const {
    12. return findChildSimulationHelper<SomeClass>("someName");
    13. };
    To copy to clipboard, switch view to plain text mode 

    If you still don't get it, I'll give you a hint. Imagine the external object holds 100 widgets each of which holds 100 more widgets.

    Anyway, my point was that Qt already provides means of traversing widgets hierarchy, and knowing what you're looking for you don't need anything else.
    qFindChild() is not a way to traverse widget hierarchy.
    Last edited by wysota; 2nd February 2012 at 17:12. Reason: spelling corrections
    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.


  8. #8
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to refer a dynamically added table widget inside a scroll area widget?

    It's a slow, semi-acurate method of finding a child you're looking for - I know that.

    I wouldn't use it on a widget containing 100 widgets containing 100 widgets each, but when you have a 5 tabs, 10 boxes each, why not?
    It will do the job, will not do any harm (unless I'm missing something), and performance is not an issue either.
    There's assumption there that names are unique though.

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

    Default Re: how to refer a dynamically added table widget inside a scroll area widget?

    Quote Originally Posted by Spitfire View Post
    I wouldn't use it on a widget containing 100 widgets containing 100 widgets each, but when you have a 5 tabs, 10 boxes each, why not?
    Because if you do that 10 times per second for two consecutive hours it's still hell slower than just returning a stored pointer which costs at most three machine instructions.

    It will do the job,
    That's not so certain. What if you have two or more objects with the same name (which can easily happen if your widget contains other widgets composed from some more widgets)? What if the object you are looking for is not a QObject subclass?

    and performance is not an issue either.
    That's true if your application doesn't do much besides looking for items. However such applications are very boring.

    There's assumption there that names are unique though.
    Why assume anything if you have a perfectly good solution that doesn't need any assumptions?
    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.


Similar Threads

  1. Replies: 5
    Last Post: 14th December 2011, 12:51
  2. Qt Designer Size of table widget inside Tab Widget is not flexible.
    By akash in forum Qt Tools
    Replies: 2
    Last Post: 14th September 2011, 12:45
  3. Replies: 3
    Last Post: 8th December 2010, 06:42
  4. Replies: 0
    Last Post: 27th July 2010, 13:48
  5. QRubberBand painting in the scroll area widget
    By SkripT in forum Qt Programming
    Replies: 7
    Last Post: 17th January 2006, 17:48

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.