Results 1 to 3 of 3

Thread: QCheckBox assignation fails for public member variable, but not for local variable

  1. #1
    Join Date
    Oct 2017
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QCheckBox assignation fails for public member variable, but not for local variable

    I am not a complete newbie to Qt, but I do have a potentially very simple, very perplexing issue.

    I have my Main Window and a separate Dialog Window, both containing a checkbox. Same functionality. When one checkbox is checked, both should be checked. This "syncing" is triggered by a signal/slot mechanism, but as the dialog window is the child of the main window, the checked values are assigned as shown below.

    This works for all checkboxes, all set up the same, except one.

    In my dialog window's class, I have declared:

    Qt Code:
    1. namespace Ui {
    2. class DialogWinow;
    3. }
    4.  
    5. class DialogWindow : public QWidget
    6. {
    7. ...
    8. public:
    9.  
    10. bool checked1;
    11. bool checked2;
    12. ...
    To copy to clipboard, switch view to plain text mode 
    In my MainWindow class/constructor I have:

    Qt Code:
    1. private:
    2. DialogWindow *dialog_window;
    To copy to clipboard, switch view to plain text mode 
    And in the setup for the MainWindow/DialogWindow, I have these assignations:

    Qt Code:
    1. dialog_window->checked1 = ui->checkBox1->isChecked();
    2. dialog_window->checked2 = ui->checkBox2->isChecked();
    To copy to clipboard, switch view to plain text mode 

    The crazy thing is that the first one of these works just fine. The second one is not working (the value of dialog_window->checked2 remains the same), and even more strange, this works:
    Qt Code:
    1. bool checked_temp = ui->checkBox2->isChecked();
    2. dialog_window->checked2 = checked_temp;
    To copy to clipboard, switch view to plain text mode 

    I have tried a few variations, for example this does not work:

    Qt Code:
    1. dialog_window->checked2 = (bool)ui->checkBox2->isChecked();
    To copy to clipboard, switch view to plain text mode 

    Could this bug be some quirk of Qt Designer? Have I checked some box for this QCheckBox in particular that is causing this assignation to fail? The settings for this QCheckBox seem to be the same as all my other (working) checkboxes.

    Thanks in advance for any advice!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QCheckBox assignation fails for public member variable, but not for local variabl

    Hard to tell without seeing the real code.
    If you have use this method on more than one checkbox and all are working except this one, to me it seems you might have a typo somewhere in a name of a variable.
    So go through all your variables makes sure you don't have a typo.
    Another possibly is that your boolean variables are being changed by a signal/slot triggered in a way you didn't anticipate (this seems to be likely the explanation to why the example with the temp works.

    Your design is very much a spaghetti design and it begs to these kinds of problems.
    A better approach would be to have a non ui object to contain the actual boolean value, and have it emit a signal when the value changes - which the checkboxes (or indeed any number of listeners) will connect to, and a slot for setting the value also each setting UI element can connect to.
    This will allow you to have one central place managing the state of the boolean value - and much easier overview of what is happening, and if needed an easy way to debug.
    This way the value is not kept by the checkboxex, but by a dedicated object, the checkboxes are only a UI element that can change it.
    It frees you from the need to manage what each checkbox is doing and makes the UI just a UI not part of the business logic/data

    Qt Code:
    1. class BooleanManager : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. ....//constructor etc
    6.  
    7. public Q_SIGNALS:
    8. void valueToggled(bool);
    9.  
    10. public Q_SLOTS:
    11. void onValueToggled(bool);
    12. private:
    13. bool m_boolVal;
    14. };
    To copy to clipboard, switch view to plain text mode 

    EDIT:
    Actually this is not a complete idea - the way it is posted here is problematic, since it can end in a signal endless loop.
    To over come this you will have to put some guarding code in your onValueToggled() slot:
    - only emit the valueToggled() signal if the value indeed toggled.
    Last edited by high_flyer; 15th October 2017 at 02:35.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Bellaella (20th October 2017)

  4. #3
    Join Date
    Oct 2017
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QCheckBox assignation fails for public member variable, but not for local variabl

    Thanks, high_flyer. I believe you are correct about the unexpected signal/slot behavior. I am still debugging, but using the local variable as a workaround for now.

Similar Threads

  1. Replies: 2
    Last Post: 17th January 2013, 23:48
  2. How to use a public Qwebelement variable
    By luisvt in forum Qt Programming
    Replies: 1
    Last Post: 28th October 2012, 11:22
  3. How can I access public variable parent Form
    By validator in forum Qt Programming
    Replies: 14
    Last Post: 18th December 2008, 22:12
  4. is it legal to declare a pthread_t variable as local?
    By nass in forum General Programming
    Replies: 1
    Last Post: 14th February 2007, 14:27
  5. Std iterator as private member variable
    By Michiel in forum General Programming
    Replies: 5
    Last Post: 21st April 2006, 16:27

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.