Results 1 to 4 of 4

Thread: How to check un-check a checkbox when clicking on text

  1. #1
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default How to check un-check a checkbox when clicking on text

    Hi,
    I created a group of checkboxes in a combo box. It works fine, but I would also like to click on the text next to the check box besides the check box itself to check and uncheck the box.
    Any ideas how to do it. Multiselect doesn't seem to do the trick.
    QList<QStandardItem *> typeCheckBoxes;
    QStringList typeNames;
    QStandardItemModel *typeModel;
    typeNames << "ujkkl" << "alkjbs" << "djklef" << "hijkl";
    for(int i = 0; i < selectedNames.size(); i++)
    {
    typeCheckBoxes << new QStandardItem();
    typeCheckBoxes[i]->setText(typeNames[i]);
    typeModel->setItem(i, 0, typeCheckBoxes[i]);
    typeCheckBoxes[i]->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    typeCheckBoxes[i]->setData(Qt::Checked, Qt::CheckStateRole);
    typeCheckBoxes[i]->setCheckable(true);
    }
    Thanks in advance.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,309
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to check un-check a checkbox when clicking on text

    You could try connecting a slot to the QComboBox::activated() or QComboBox::currentIndexChanged() signal. In your slot, you look at the check state of the item at that index and flip it. However, I do not know how you can distinguish between a click on the text vs. a click on the checkbox. You might have to turn off the ItemIsUserCheckable flag and simply use the combobox signal to toggle the checkbox state.
    <=== 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. #3
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to check un-check a checkbox when clicking on text

    Thanks, but unfortunately nothing happens when I click in the label, just the checkbox. I'll see if I can figure something else out.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,309
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to check un-check a checkbox when clicking on text

    unfortunately nothing happens when I click in the label
    Sounds like the model is intercepting these signals and is eating them. The only other thing I can suggest is adding an event filter on the combobox to look for mouse press events (or better mouse press / release pairs), but then that gets ugly because you'd have to deal with the open / closed state of the drop-down, and if open, determining from a pixel mouse position which specific item was clicked.

    Unlike QAbstractItemView, the combobox does not have any concept of a QItemSelectionModel (or at least it doesn't expose this in the public interface) so there is no way to interact with the selection state.
    <=== 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: 3
    Last Post: 15th February 2017, 10:57
  2. Replies: 4
    Last Post: 9th December 2015, 18:24
  3. Replies: 3
    Last Post: 1st April 2011, 05:58
  4. Accessing check state of CheckBox in QTableWidget
    By lnxusr in forum Qt Programming
    Replies: 6
    Last Post: 22nd November 2009, 02:13
  5. Replies: 5
    Last Post: 6th March 2008, 20:04

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.