Results 1 to 4 of 4

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,333
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    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.

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

    ZajtiM (31st July 2022)

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
  •  
Qt is a trademark of The Qt Company.