Results 1 to 4 of 4

Thread: How-to detect any (from 160) QCheckBox stateChanged

  1. #1
    Join Date
    Sep 2021
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question How-to detect any (from 160) QCheckBox stateChanged

    Hello,

    i have application with 160 QCheckBoxes. Is there a way to detect if any of 160 checkboxes has changed their state? I mean, is there a way to use only one connect() to detect stateChanged for any of 160 boxes? I don't want to add 160 connect() lines. Or are there any other options, other than connect()?

    All checkboxes are added / created in QT Designer.

    I need to detect when specific checkbox is selected, so i can disable opposite checkbox. To prevent user from accidentally selecting opposite checkboxes.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,235
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How-to detect any (from 160) QCheckBox stateChanged

    I need to detect when specific checkbox is selected, so i can disable opposite checkbox. To prevent user from accidentally selecting opposite checkboxes.
    This sounds like you should be using QRadioButton instead of QCheckBox and put the pairs of buttons into exclusive QButtonGroup. You can design the UI in Qt Designer, but the buttons have to be added to the QButtonGroup using code.

    If you have to use QCheckBox, then you can connect all of them to the same slot. Inside the slot, you can determine which check box was changed by using QObject::sender() and doing a qobject_cast< QCheckBox * > to retrieve the actual QCheckBox instance.

    To make it easy to determine the matching pairs of check boxes, I would make a QMap< QCheckBox *, QCheckBox * > with all of the pairs added. Once you have the pointer to the check box that was clicked, you can look up the opposite box using the map.

    You cannot avoid writing the code to connect the check boxes to the slot or to build the lookup map. I think it is a bad idea to make signal-slot connections in Qt Designer, because then those connections are hidden away in the UI file where you can forget them. When I was first learning Qt, I would do this, and it caused a large number of bugs and difficulty in debugging them because I could not determine why a slot was being called (or called twice) because a connection was hidden inside the UI code.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    ZajtiM (31st July 2022)

  4. #3
    Join Date
    Sep 2021
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How-to detect any (from 160) QCheckBox stateChanged

    Thank you for your reply d_stranz.

    On image you can see how checkboxes are setup. They are not exactly in "pair", that's why checkboxes and not radiobuttons.

    Here are examples, how it should work:
    If you select "?len" top left 6, it should disable all other checkboxes in top left 6 "column".
    If you select "konus" top left 6, it should disable all other checkboxes in top left 6 "column".
    If you select "prevleka" OR "prevleka" and "anker" top left 6, it should disable "?len" AND "konus" top left 6.

    QCheckBox.png

    Quote Originally Posted by d_stranz
    ...
    You cannot avoid writing the code to connect the check boxes to the slot or to build the lookup map. I think it is a bad idea to make signal-slot connections in Qt Designer, because then those connections are hidden away in the UI file where you can forget them. When I was first learning Qt, I would do this, and it caused a large number of bugs and difficulty in debugging them because I could not determine why a slot was being called (or called twice) because a connection was hidden inside the UI code.
    Regarding writing the code to connect all checkboxes. I would definitely prefer to do it with code, rather than in QT Designer. I just wanted to see if it was possible to avoid 160 lines of connect(). Sort of like connect() with a "wildcard" selector.
    Last edited by ZajtiM; 31st July 2022 at 17:33.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,235
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How-to detect any (from 160) QCheckBox stateChanged

    Sort of like connect() with a "wildcard" selector.
    That is not possible as far as I know.

    I just wanted to see if it was possible to avoid 160 lines of connect().
    If you use Qt Designer to create the layout and add the labels and lines but not the check boxes, you can create the check boxes in code, place them in the layout, and add them to a vector of QCheckBox *. Then you can use a for() loop to connect each check box to the slot in a single statement.

    Here are examples, how it should work:
    That sounds very complicated. Maybe you should look at QStateMachine and develop a set of transitions that implement your enable / disable logic.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 5
    Last Post: 2nd October 2020, 19:19
  2. QCheckBox
    By sajis997 in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2012, 14:15
  3. About QCheckBox
    By liushuo0826 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 16th April 2010, 06:14
  4. QCheckBox value
    By Koas in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2009, 13:33
  5. QCheckbox
    By sonuani in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2008, 13:12

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.