Results 1 to 5 of 5

Thread: Blocksignals for all widgets within a QDialog

  1. #1
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1

    Default Blocksignals for all widgets within a QDialog

    Hello,

    I have a QDialog which contains several widgets. The QDialog is used to display the various properties of a member object of type MyObject.

    The QDialog is modeless and can be udated on the fly with a new object using:
    Qt Code:
    1. void MyDialog::SetMyObject(MyObject* i_pObject)
    2. {
    3. if (m_pObject != i_pObject)
    4. {
    5. m_pObject = i_pObject;
    6.  
    7. ModelToUI();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    The ModelToUI() function will update every widgets according to the content of m_pObject:
    Qt Code:
    1. void MyDialog::ModelToUI()
    2. {
    3. if (m_pObject!= NULL)
    4. {
    5. m_pObjectNameEdit->setText(m_pObject->GetName());
    6. [....]
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    This is where I am running into a problem, every time the ModelToUI() function is called and setText() is called, it will also launch textChanged signal which I have connected. Then, it will do a bunch of processing for every widgets.

    This is what I am trying to avoid. I am aware of the :
    Qt Code:
    1. m_pObjectNameEdit->blockSignals(true);
    2. m_pObjectNameEdit->setText(m_pObject->GetName());
    3. m_pObjectNameEdit->blockSignals(false);
    To copy to clipboard, switch view to plain text mode 
    But I was wondering if there was a way to block ALL signals for every children widgets contained in a dialog without having to use blockSignals for every single on of them. Something I could use in SetMyObject() function just before and after the ModelToUI() call.

    I am also aware that in this particular case connecting textEdited instead of textChanged would solve the problem, but I also have a bunch of QRadioButton and QSpinBox widgets.

    Any help would be appreciated, thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Blocksignals for all widgets within a QDialog

    Have a flag checked in your custom slots that will prevent the calculations when dialog is updated. Set the flag at the beginning of updating the dialog and clear it after you're done with changes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    muzzled (12th January 2011)

  4. #3
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1

    Default Re: Blocksignals for all widgets within a QDialog

    Quote Originally Posted by wysota View Post
    Have a flag checked in your custom slots that will prevent the calculations when dialog is updated. Set the flag at the beginning of updating the dialog and clear it after you're done with changes.
    This is what I somehow ended up doing, thank you.

    In case anyone is looking for another solution, I had a partial one which implied looping through every child control and block the signals. However, it was also calling the blockSignals on QGroupBox and QLabel, which was totally useless so I ended up with the solution proposed by wysota.

    Qt Code:
    1. QList<QWidget *> widgetList = this->findChildren<QWidget *>();
    2. QList<QWidget *>::const_iterator widgetIter (widgetList.begin());
    3. QList<QWidget *>::const_iterator lastWidget (widgetList.end());
    4.  
    5. while ( widgetIter != lastWidget)
    6. {
    7. (*widgetIter)->blockSignals(true);
    8. ++widgetIter;
    9. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Blocksignals for all widgets within a QDialog

    If you have to block signals then it usually means your design is somehow screwed up.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Oct 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Blocksignals for all widgets within a QDialog

    To reduce amount of boilerplate I use the modification of the code here:
    http://stackoverflow.com/questions/3...556892#3556892

    Qt Code:
    1. template<class T>
    2. class QSignalBlocker
    3. {
    4. T * const o;
    5. public:
    6. explicit QSignalBlocker( T * oo )
    7. : o(oo)
    8. {
    9. }
    10. QSignalBlockerCallProxy<T> operator->()
    11. {
    12. if (o)
    13. o->blockSignals( true );
    14. return o;
    15. }
    16. ~QSignalBlocker()
    17. {
    18. if(o)
    19. o->blockSignals(false);
    20. }
    21. };
    22.  
    23. template<class T>
    24. QSignalBlocker<T> SilentCall(T* o)
    25. {
    26. return QSignalBlocker<T>(o);
    27. }
    To copy to clipboard, switch view to plain text mode 

    the usage is:

    Qt Code:
    1. SilentCall(ui.linedit_whatever)->setText(QString("Whatever"));
    To copy to clipboard, switch view to plain text mode 

    Only need to wrap the element name with SilentCall()...

    fix It has been brought to my attention that I misunderstood the necessity of using CallProxy in Stroustrooup's article and that it is unnecessary here Simplified.
    Last edited by Zekses; 12th October 2011 at 21:54.

Similar Threads

  1. Replies: 2
    Last Post: 16th December 2010, 12:52
  2. Replies: 5
    Last Post: 19th April 2010, 00:31
  3. closing a Qdialog called from a Qdialog
    By OverTheOCean in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2009, 09:02
  4. Replies: 0
    Last Post: 15th May 2009, 16:38
  5. Tab Order Settings in a QDialog widgets
    By vinnu in forum Qt Programming
    Replies: 10
    Last Post: 21st September 2006, 17:25

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.