Results 1 to 4 of 4

Thread: QT Button Example

  1. #1
    Join Date
    Nov 2011
    Posts
    30
    Qt products
    Qt3
    Platforms
    MacOS X

    Default QT Button Example

    I have a "simple" question and I am tired or programming like I am in C. I am doing a dialog with several radio button groups and I would like to get the values when the user presses okay. I have been looking at the example

    QT Group Box Example

    (Code below)


    This example does a really nice job on showing how to build a button box correctly. However, it does not show how to access the current values when the user presses ok. I could go in and add numerous hacks and events to get me this data but I am tired of programming like this is C code and not C++. But again, I would like to know the correct method to do this and "pretend" I know how to program correctly.

    Can any one give me an idea on how to access the radio button values in created in "createFirstExclusiveGroup" ??

    Ken


    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. #include "window.h"
    4.  
    5. Window::Window(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. QGridLayout *grid = new QGridLayout;
    9. grid->addWidget(createFirstExclusiveGroup(), 0, 0);
    10. grid->addWidget(createSecondExclusiveGroup(), 1, 0);
    11. grid->addWidget(createNonExclusiveGroup(), 0, 1);
    12. grid->addWidget(createPushButtonGroup(), 1, 1);
    13. setLayout(grid);
    14.  
    15. setWindowTitle(tr("Group Boxes"));
    16. resize(480, 320);
    17. }
    18.  
    19. QGroupBox *Window::createFirstExclusiveGroup()
    20. {
    21. QGroupBox *groupBox = new QGroupBox(tr("Exclusive Radio Buttons"));
    22.  
    23. QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1"));
    24. QRadioButton *radio2 = new QRadioButton(tr("R&adio button 2"));
    25. QRadioButton *radio3 = new QRadioButton(tr("Ra&dio button 3"));
    26.  
    27. radio1->setChecked(true);
    28.  
    29. QVBoxLayout *vbox = new QVBoxLayout;
    30. vbox->addWidget(radio1);
    31. vbox->addWidget(radio2);
    32. vbox->addWidget(radio3);
    33. vbox->addStretch(1);
    34. groupBox->setLayout(vbox);
    35.  
    36. return groupBox;
    37. }
    38.  
    39. QGroupBox *Window::createSecondExclusiveGroup()
    40. {
    41. QGroupBox *groupBox = new QGroupBox(tr("E&xclusive Radio Buttons"));
    42. groupBox->setCheckable(true);
    43. groupBox->setChecked(false);
    44.  
    45. QRadioButton *radio1 = new QRadioButton(tr("Rad&io button 1"));
    46. QRadioButton *radio2 = new QRadioButton(tr("Radi&o button 2"));
    47. QRadioButton *radio3 = new QRadioButton(tr("Radio &button 3"));
    48. radio1->setChecked(true);
    49. QCheckBox *checkBox = new QCheckBox(tr("Ind&ependent checkbox"));
    50. checkBox->setChecked(true);
    51.  
    52. QVBoxLayout *vbox = new QVBoxLayout;
    53. vbox->addWidget(radio1);
    54. vbox->addWidget(radio2);
    55. vbox->addWidget(radio3);
    56. vbox->addWidget(checkBox);
    57. vbox->addStretch(1);
    58. groupBox->setLayout(vbox);
    59.  
    60. return groupBox;
    61. }
    62.  
    63. QGroupBox *Window::createNonExclusiveGroup()
    64. {
    65. QGroupBox *groupBox = new QGroupBox(tr("Non-Exclusive Checkboxes"));
    66. groupBox->setFlat(true);
    67.  
    68. QCheckBox *checkBox1 = new QCheckBox(tr("&Checkbox 1"));
    69. QCheckBox *checkBox2 = new QCheckBox(tr("C&heckbox 2"));
    70. checkBox2->setChecked(true);
    71. QCheckBox *tristateBox = new QCheckBox(tr("Tri-&state button"));
    72. tristateBox->setTristate(true);
    73. tristateBox->setCheckState(Qt::PartiallyChecked);
    74.  
    75. QVBoxLayout *vbox = new QVBoxLayout;
    76. vbox->addWidget(checkBox1);
    77. vbox->addWidget(checkBox2);
    78. vbox->addWidget(tristateBox);
    79. vbox->addStretch(1);
    80. groupBox->setLayout(vbox);
    81.  
    82. return groupBox;
    83. }
    84.  
    85. QGroupBox *Window::createPushButtonGroup()
    86. {
    87. QGroupBox *groupBox = new QGroupBox(tr("&Push Buttons"));
    88. groupBox->setCheckable(true);
    89. groupBox->setChecked(true);
    90.  
    91. QPushButton *pushButton = new QPushButton(tr("&Normal Button"));
    92. QPushButton *toggleButton = new QPushButton(tr("&Toggle Button"));
    93. toggleButton->setCheckable(true);
    94. toggleButton->setChecked(true);
    95. QPushButton *flatButton = new QPushButton(tr("&Flat Button"));
    96. flatButton->setFlat(true);
    97.  
    98. QPushButton *popupButton = new QPushButton(tr("Pop&up Button"));
    99. QMenu *menu = new QMenu(this);
    100. menu->addAction(tr("&First Item"));
    101. menu->addAction(tr("&Second Item"));
    102. menu->addAction(tr("&Third Item"));
    103. menu->addAction(tr("F&ourth Item"));
    104. popupButton->setMenu(menu);
    105.  
    106. QAction *newAction = menu->addAction(tr("Submenu"));
    107. QMenu *subMenu = new QMenu(tr("Popup Submenu"));
    108. subMenu->addAction(tr("Item 1"));
    109. subMenu->addAction(tr("Item 2"));
    110. subMenu->addAction(tr("Item 3"));
    111. newAction->setMenu(subMenu);
    112.  
    113. QVBoxLayout *vbox = new QVBoxLayout;
    114. vbox->addWidget(pushButton);
    115. vbox->addWidget(toggleButton);
    116. vbox->addWidget(flatButton);
    117. vbox->addWidget(popupButton);
    118. vbox->addStretch(1);
    119. groupBox->setLayout(vbox);
    120.  
    121. return groupBox;
    122. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: QT Button Example

    Ideally you would just keep the pointers to the radio buttons in members of your widget, then you could simply iterate over the respective container.

    Something like:

    Qt Code:
    1. class Window : public QWidget
    2. {
    3. // ....
    4.  
    5.  
    6. private:
    7. QVector<QRadioButton*> m_buttonsOfFirstGroup;
    8. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QGroupBox *Window::createFirstExclusiveGroup()
    2. {
    3. QGroupBox *groupBox = new QGroupBox(tr("Exclusive Radio Buttons"));
    4.  
    5. QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1"));
    6. QRadioButton *radio2 = new QRadioButton(tr("R&adio button 2"));
    7. QRadioButton *radio3 = new QRadioButton(tr("Ra&dio button 3"));
    8.  
    9. m_buttonsOfFirstGroup.reserve(3);
    10. m_buttonsOfFirstGroup << radio1 << radio2 << radio3;
    11.  
    12. // ....
    13. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Nov 2011
    Posts
    30
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: QT Button Example

    Okay, thanks. There is no "magic" way to access them from the gui directly. By adding these pointers, it makes life easier.

    Thanks.

  4. #4
    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: QT Button Example

    Well, you could use QObject::findChildren() on each group box but you would then either need to store the groupbox pointers or find them first.

    Storing the pointers to the elements you intend to interact with, in your case the radio buttons, is much cleaner.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 26th April 2011, 11:44
  2. Changing text of button in no relation to button
    By Sabre Runner in forum Newbie
    Replies: 22
    Last Post: 23rd September 2010, 12:29
  3. Replies: 6
    Last Post: 21st August 2010, 21:09
  4. Replies: 1
    Last Post: 2nd August 2010, 05:40
  5. Custom widget: a button within a button
    By Gnurou in forum Qt Programming
    Replies: 7
    Last Post: 18th June 2009, 09:03

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.