Results 1 to 6 of 6

Thread: How emit signal when a QCheckBox inserted in a QList<QCheckBox*> change its status?

  1. #1
    Join Date
    Mar 2018
    Posts
    33
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default How emit signal when a QCheckBox inserted in a QList<QCheckBox*> change its status?

    I wrote the code attacched & it work (in editobject.cpp find the text "QUESTION:" and read my comment) but i want to know if is it possible with just a connect signal/call (i make my apologise for my bad english) to have the discrimination of what QCheckBox inserted in a QList<QCheckBox*> change its status?
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How emit signal when a QCheckBox inserted in a QList<QCheckBox*> change its statu

    Read about QObject::sender().

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

    andreaQt (2nd October 2020)

  4. #3
    Join Date
    Mar 2018
    Posts
    33
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How emit signal when a QCheckBox inserted in a QList<QCheckBox*> change its statu

    please more info

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

    Default Re: How emit signal when a QCheckBox inserted in a QList<QCheckBox*> change its statu

    QObject::sender() gives you the pointer to the QObject (in your case the QCheckBox) instance that emitted the signal that the slot is handling. So if you have multiple check boxes connected to the same slot, you can use sender() to determine which checkbox sent the signal.

    Qt Code:
    1. // Your member variable that stores your checkboxes
    2. QList<QCheckbox *> myListOfCheckboxes;
    3.  
    4. // Slot connected to QCheckBox::toggled() signal
    5. void MyWidget::onCheckboxToggled( bool bChecked )
    6. {
    7. int whichBox = myListOfCheckboxes.indexOf( sender() );
    8. }
    To copy to clipboard, switch view to plain text mode 
    <=== 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.

  6. #5
    Join Date
    Mar 2018
    Posts
    33
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How emit signal when a QCheckBox inserted in a QList<QCheckBox*> change its statu

    OK but i have no idea about to write (to catch the signal):
    connect (list_check_box, ????, ...., ????);
    or how the slot is called?

    and when i try to implement in my code:
    Qt Code:
    1. void EditObject::onCheckboxToggled(bool bChecked)
    2. {
    3. int whichBox = list_check_box->indexOf(sender());
    4. }
    To copy to clipboard, switch view to plain text mode 
    i have this error -> error: reference to type 'QCheckBox *const' could not bind to an rvalue of type 'QObject *'
    Last edited by andreaQt; 2nd October 2020 at 19:10.

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How emit signal when a QCheckBox inserted in a QList<QCheckBox*> change its statu

    Qt Code:
    1. for ( auto index; index < numberOfCheckBoxes; ++index )
    2. connect( list_check_box[ index ], &QCheckBox::toggled, this, &MyWidget::onCheckboxToggled );
    To copy to clipboard, switch view to plain text mode 

    Of course, you can't call connect() until after you have created all of your checkboxes and put them into the QList.

    Qt Code:
    1. QCheckBox * pCB = qobject_cast< QCheckBox * >( sender() );
    2. int whichBox = list_check_box.indexOf( pCB );
    To copy to clipboard, switch view to plain text mode 

    list_check_box should be defined as "QList< QCheckBox *>" so you use "." to call methods, not "->". There is no reason to define list_check_box as a "QList<QCheckBox *> *".
    <=== 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.

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

    andreaQt (2nd October 2020)

Similar Threads

  1. Replies: 1
    Last Post: 30th September 2016, 18:07
  2. Change position according to QCheckBox State
    By champ in forum Qt Programming
    Replies: 5
    Last Post: 6th June 2010, 19:37
  3. QCheckBox change color
    By vajindarladdad in forum Newbie
    Replies: 4
    Last Post: 6th October 2009, 09:33
  4. Disabling the user to change QCheckBox checkstate
    By ChiliPalmer in forum Qt Programming
    Replies: 1
    Last Post: 26th September 2009, 17:20
  5. How to change defualt color of QCheckbox box?
    By darshan.hardas in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2008, 14:24

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.