Results 1 to 5 of 5

Thread: Processing multiple checkboxes

  1. #1
    Join Date
    Oct 2011
    Location
    Netherlands
    Posts
    13
    Thanks
    7
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Processing multiple checkboxes

    Hello!

    I'm a student and currently am working at a small company as an intern. I have to control some LEDs through usage of Qt, and made an interface to be able to do so. The interface issue concerned contains four checkboxes, and a button. It's supposed to send bytes to the MOXA board after having pressed the Send Data button.

    My function checks whether the checkbox is enabled, and if so, send the bytes. However, it only seems to work when using one checkbox at a time. I want to be able to select all of them, so all four functions are processed, and thus, all four LEDs go on (or off).

    I've done this, but this doesn't seem to work (enabling multiple checkboxes at once, that is):

    Qt Code:
    1. void ETClient::on_pushSendData_clicked()
    2. {
    3. bool dio0 = this->ui->chkDIO0->isChecked();
    4. bool dio1 = this->ui->chkDIO1->isChecked();
    5. bool dio2 = this->ui->chkDIO2->isChecked();
    6. bool dio3 = this->ui->chkDIO3->isChecked();
    7.  
    8. if(dio0 == true) this->dio0_enabled();
    9. if(dio1 == true) this->dio1_enabled();
    10. if(dio2 == true) this->dio2_enabled();
    11. if(dio3 == true) this->dio3_enabled();
    12. else ;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Any help would be greatly appreciated!
    Last edited by -Kyr0s-; 14th October 2011 at 16:01.

  2. #2
    Join Date
    Nov 2009
    Posts
    14
    Thanks
    4

    Default Re: Processing multiple checkboxes

    However, it only seems to work when using one checkbox at a time. I want to be able to select all of them, so all four functions are processed, and thus, all four LEDs go on (or off).
    Could you clarify this point? When you debug this with all checkboxes checked, what are your bool values?

  3. #3
    Join Date
    Oct 2011
    Location
    Netherlands
    Posts
    13
    Thanks
    7
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Processing multiple checkboxes

    Hello,

    Thanks for the swift reply – sorry for not replying any sooner than I have right now.

    I've put breakpoints on the entire code that I've provided above.

    Oddly enough, the bool values (when it processes the first definition of isChecked() - line 3 as seen above) are:

    -dio0: 128
    -dio1: 188
    -dio2: 106
    -dio3: false

    When I continue to the next definition (line 4), the values are:

    -dio0: false
    -dio1: same
    -dio2: same
    -dio3: same

    Then: false, false, same same -> false, false, false, same -> false, false, false, false.

    Any suggestions? I can't seem to see why it does that.

    Here's the entire code of the file, in-case needed:

    Qt Code:
    1. #include "etclient.h"
    2. #include "ui_etclient.h"
    3. #include "about.h"
    4.  
    5. ETClient::ETClient(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::ETClient)
    8. {
    9. this->ui->setupUi(this);
    10.  
    11. this->connect(ui->menuAbout,SIGNAL(triggered()),this,SLOT(show_about()));
    12.  
    13. this->connect(ui->pushConnect,SIGNAL(clicked()),this,SLOT(pre_begin()));
    14. this->connect(ui->pushDisconnect,SIGNAL(clicked()),this,SLOT(close_session()));
    15. this->connect(&client,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(connection_invalid()));
    16. this->connect(&client,SIGNAL(connected()),this,SLOT(start_transfer()));
    17.  
    18. this->ui->pushDisconnect->setShown(false);
    19. }
    20.  
    21. ETClient::~ETClient()
    22. {
    23. client.close();
    24. delete(this->ui);
    25. }
    26.  
    27. void ETClient::show_about()
    28. {
    29. About *about = new About();
    30. about->show();
    31. }
    32.  
    33. void ETClient::pre_begin()
    34. {
    35. QString device_addr = this->ui->txtbxIP->text();
    36. int device_port = this->ui->txtbxPort->text().toInt();
    37.  
    38. this->begin(device_addr,device_port);
    39. }
    40.  
    41. void ETClient::begin(QString host, quint16 port_n)
    42. {
    43. QHostAddress addr(host);
    44.  
    45. client.connectToHost(addr,port_n);
    46. }
    47.  
    48. void ETClient::start_transfer()
    49. {
    50. QString conSucc = "Connected successfully to: ";
    51. QString device_addr = this->ui->txtbxIP->text();
    52.  
    53. this->ui->txtbxIP->setText(conSucc%device_addr);
    54. this->ui->txtbxIP->setEnabled(false);
    55. this->ui->txtbxPort->setEnabled(false);
    56.  
    57. this->ui->pushConnect->setShown(false);
    58. this->ui->pushDisconnect->setShown(true);
    59.  
    60. this->ui->chkDIO0->setEnabled(true);
    61. this->ui->chkDIO1->setEnabled(true);
    62. this->ui->chkDIO2->setEnabled(true);
    63. this->ui->chkDIO3->setEnabled(true);
    64.  
    65. this->on_pushSendData_clicked();
    66. }
    67.  
    68. void ETClient::connection_invalid()
    69. {
    70. QString strInvalid = "Failed to connect. ";
    71. QString device_addr = this->ui->txtbxIP->text();
    72.  
    73. this->ui->txtbxIP->setText("Invalid IP.");
    74. this->ui->txtbxIP->isActiveWindow() == true;
    75. }
    76.  
    77. void ETClient::close_session()
    78. {
    79. QString device_addr = this->ui->txtbxIP->text();
    80.  
    81. client.abort();
    82.  
    83. this->ui->txtbxIP->setEnabled(true);
    84. this->ui->txtbxPort->setEnabled(true);
    85.  
    86. this->ui->pushDisconnect->setShown(false);
    87. this->ui->pushConnect->setShown(true);
    88.  
    89. this->ui->chkDIO0->setEnabled(false);
    90. this->ui->chkDIO1->setEnabled(false);
    91. this->ui->chkDIO2->setEnabled(false);
    92. this->ui->chkDIO3->setEnabled(false);
    93. }
    94.  
    95. void ETClient::on_pushSendData_clicked()
    96. {
    97. bool dio0 = this->ui->chkDIO0->isChecked();
    98. bool dio1 = this->ui->chkDIO1->isChecked();
    99. bool dio2 = this->ui->chkDIO2->isChecked();
    100. bool dio3 = this->ui->chkDIO3->isChecked();
    101.  
    102. if(dio0 == true) this->dio0_enabled();
    103. if(dio1 == true) this->dio1_enabled();
    104. if(dio2 == true) this->dio2_enabled();
    105. if(dio3 == true) this->dio3_enabled();
    106. else;
    107. }
    108.  
    109. /*
    110.   *
    111.   * DIO management functions (DIO 0,1,2,3 ENABLED & DISABLED)
    112.   *
    113.   */
    114.  
    115. void ETClient::dio0_enabled()
    116. {
    117. int nullptr = 0;
    118. QByteArray bytes;
    119.  
    120. bytes.push_back(2); // command number
    121. bytes.push_back(2); // version
    122. bytes.push_back(nullptr); // byte used for response
    123. bytes.push_back(3); // data length
    124. bytes.push_back(nullptr); // DIO number (0 = 1ste, 1 = 2de, 2 = 3de, 3 = 4de)
    125. bytes.push_back(1); // i/o mode (0 input, 1 output)
    126. bytes.push_back(nullptr); // signal (0 = aan, 1 = uit)
    127.  
    128. client.write(bytes);
    129. }
    130.  
    131. void ETClient::dio1_enabled()
    132. {
    133. int nullptr = 0;
    134. QByteArray bytes;
    135.  
    136. bytes.push_back(2); // command number
    137. bytes.push_back(2); // version
    138. bytes.push_back(nullptr); // byte used for response
    139. bytes.push_back(3); // data length
    140. bytes.push_back(1); // DIO number
    141. bytes.push_back(1); // i/o mode (0 input, 1 output)
    142. bytes.push_back(nullptr); // signal (0, 1)
    143.  
    144. client.write(bytes);
    145. }
    146.  
    147. void ETClient::dio2_enabled()
    148. {
    149. int nullptr = 0;
    150. QByteArray bytes;
    151.  
    152. bytes.push_back(2); // command number
    153. bytes.push_back(2); // version
    154. bytes.push_back(nullptr); // byte used for response
    155. bytes.push_back(3); // data length
    156. bytes.push_back(2); // DIO number
    157. bytes.push_back(1); // i/o mode (0 input, 1 output)
    158. bytes.push_back(nullptr); // signal (0, 1)
    159.  
    160. client.write(bytes);
    161. }
    162.  
    163. void ETClient::dio3_enabled()
    164. {
    165. int nullptr = 0;
    166. QByteArray bytes;
    167.  
    168. bytes.push_back(2); // command number
    169. bytes.push_back(2); // version
    170. bytes.push_back(nullptr); // byte used for response
    171. bytes.push_back(3); // data length
    172. bytes.push_back(3); // DIO number
    173. bytes.push_back(1); // i/o mode (0 input, 1 output)
    174. bytes.push_back(nullptr); // signal (0, 1)
    175.  
    176. client.write(bytes);
    177. }
    178.  
    179.  
    180. void ETClient::dio0_disabled()
    181. {
    182. int nullptr = 0;
    183. QByteArray bytes;
    184.  
    185. bytes.push_back(2); // command number
    186. bytes.push_back(2); // version
    187. bytes.push_back(nullptr); // byte used for response
    188. bytes.push_back(3); // data length
    189. bytes.push_back(nullptr); // DIO number
    190. bytes.push_back(1); // i/o mode (0 input, 1 output)
    191. bytes.push_back(1); // signal (0, 1)
    192.  
    193. client.write(bytes);
    194. }
    195.  
    196. void ETClient::dio1_disabled()
    197. {
    198. int nullptr = 0;
    199. QByteArray bytes;
    200.  
    201. bytes.push_back(2); // command number
    202. bytes.push_back(2); // version
    203. bytes.push_back(nullptr); // byte used for response
    204. bytes.push_back(3); // data length
    205. bytes.push_back(1); // DIO number
    206. bytes.push_back(1); // i/o mode (0 input, 1 output)
    207. bytes.push_back(1); // signal (0, 1)
    208.  
    209. client.write(bytes);
    210. }
    211.  
    212. void ETClient::dio2_disabled()
    213. {
    214. int nullptr = 0;
    215. QByteArray bytes;
    216.  
    217. bytes.push_back(2); // command number
    218. bytes.push_back(2); // version
    219. bytes.push_back(nullptr); // byte used for response
    220. bytes.push_back(3); // data length
    221. bytes.push_back(2); // DIO number
    222. bytes.push_back(1); // i/o mode (0 input, 1 output)
    223. bytes.push_back(1); // signal (0, 1)
    224.  
    225. client.write(bytes);
    226. }
    227.  
    228. void ETClient::dio3_disabled()
    229. {
    230. int nullptr = 0;
    231. QByteArray bytes;
    232.  
    233. bytes.push_back(2); // command number
    234. bytes.push_back(2); // version
    235. bytes.push_back(nullptr); // byte used for response
    236. bytes.push_back(3); // data length
    237. bytes.push_back(3); // DIO number
    238. bytes.push_back(1); // i/o mode (0 input, 1 output)
    239. bytes.push_back(1); // signal (0, 1)
    240.  
    241. client.write(bytes);
    242. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your time!

  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: Processing multiple checkboxes

    Quote Originally Posted by -Kyr0s- View Post
    My function checks whether the checkbox is enabled, and if so, send the bytes. However, it only seems to work when using one checkbox at a time. I want to be able to select all of them, so all four functions are processed, and thus, all four LEDs go on (or off).
    Nothing in that code would appear to turn LEDs off (assuming dioX_enabled() turns them on).
    Any suggestions? I can't seem to see why it does that.
    You have never initialised the bools, so they contain whatever rubbish data is in the memory location allocated to them until you set them.

  5. #5
    Join Date
    Oct 2011
    Location
    Netherlands
    Posts
    13
    Thanks
    7
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Processing multiple checkboxes

    Quote Originally Posted by ChrisW67 View Post
    Nothing in that code would appear to turn LEDs off (assuming dioX_enabled() turns them on).
    That's correct, yes. I've not used the dioX_disabled() functions as of yet, as I want to make sure that dioX_enabled() works before doing so. I use another app (not made by me) to disable the LEDs in-case needed.
    You have never initialised the bools, so they contain whatever rubbish data is in the memory location allocated to them until you set them.
    I'm not quite sure what you mean. I want to be able to select or deselect them when connecting, without having set them to enabled – they need to be unchecked by default when starting the application/connecting.

    I've tried however, to do so as you suggested, but the same issue occurs: only one is being processed – so, for instance, it's not possible to select all four of them, and enable all LEDs. Only the first is being turned on (or the second, third, fourth; just as long as it's only one checkbox).

Similar Threads

  1. Event Processing
    By QbelcorT in forum Qt for Embedded and Mobile
    Replies: 10
    Last Post: 16th April 2009, 00:48
  2. Image Processing using Qt
    By danielperaza in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2008, 19:15
  3. XML processing instruction
    By mattia in forum Newbie
    Replies: 1
    Last Post: 26th February 2008, 12:37
  4. checkboxes
    By abrou in forum Newbie
    Replies: 2
    Last Post: 1st February 2008, 19:52
  5. QTreeView with checkboxes
    By shad in forum Qt Programming
    Replies: 1
    Last Post: 4th May 2006, 14:29

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.