Results 1 to 7 of 7

Thread: Grey Out Spin Box

  1. #1
    Join Date
    Jun 2011
    Posts
    197
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Grey Out Spin Box

    I can't seem to find any mention in the documentation about spin boxes having a property of being greyed out.

    I have tried:

    Qt Code:
    1. this->ui->mySpinBox->setReadOnly(true);
    To copy to clipboard, switch view to plain text mode 

    &

    Qt Code:
    1. this->ui->mySpinBox->setEnabled(false);
    To copy to clipboard, switch view to plain text mode 

    But this didn't work.

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

    Default Re: Grey Out Spin Box

    setEnabled(false) should grey it out.
    But it will also disable it. (takes no user input)
    If this is not working probably somewhere else in your code it is being enabled again.
    You can override the changeEvent() method to see when it gets enabled again.
    Last edited by high_flyer; 20th September 2017 at 16:44.
    ==========================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. #3
    Join Date
    Jun 2011
    Posts
    197
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Grey Out Spin Box

    Bingo it works, thanks for that!

    My problem was that my loop wasn't getting entered where the setEnabled(false) was being called. Who would have thought that the QCheckBox is 0 when disabled and 2 when endabled? I'm guessing 1 is when it's in its tri'th state. I obviously was checking for 1.

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

    Default Re: Grey Out Spin Box

    Who would have thought that the QCheckBox is 0 when disabled and 2 when endabled?
    Anyone who'd read the documentation:
    http://doc.qt.io/qt-5/qt.html#CheckState-enum

    I'm guessing 1 is when it's in its tri'th state.
    No need to guess.
    It's bad practice to guess and use naked enum values for several reasons:
    First, (in general) there is no guarantee the naked value will stay the same in future releases which will break your code if you use the naked values.
    Second, its less readable, just like you don't know what '1' '2' or any other values means, so will anyone else who reads your code, or you in the future.
    On the other hand if you use the named enum values anyone reading the code knows what the value is, and no one needs to care what the naked value behind it is, and the code will continue to work even if the naked value in the enum will change in the future.
    And, be aware that enum does not have to be an int, it also could be a bool for example which can bring about really odd behavior if your are working with naked values expecting ints.

    Use the named enum values, you will stay safe that way.
    ==========================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.

  5. #5
    Join Date
    Jun 2011
    Posts
    197
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Grey Out Spin Box

    Quote Originally Posted by high_flyer View Post
    Anyone who'd read the documentation:
    http://doc.qt.io/qt-5/qt.html#CheckState-enum
    Yep, that's where I learned about it after I realised my loop wasn't being entered as expected.

    Only guessing because I don't need to know this at this stage, but I know that the documentation is where to find it in the future if I need it .

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

    Default Re: Grey Out Spin Box

    Only guessing because I don't need to know this at this stage, but I know that the documentation is where to find it in the future if I need it
    This comment was mostly meant to point out to not use of raw int values for the enums - which is why you where needing to guess.
    Using the named enum values will save you and others reading your code the need to guess what these values mean.
    ==========================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.

  7. #7
    Join Date
    Jun 2011
    Posts
    197
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Grey Out Spin Box

    Ah I see where you were going with that now.

Similar Threads

  1. Replies: 0
    Last Post: 27th July 2011, 14:11
  2. Replies: 4
    Last Post: 22nd September 2010, 23:34
  3. QT Creator Spin Box for hex values
    By JJ31069 in forum Newbie
    Replies: 6
    Last Post: 30th April 2009, 15:06
  4. code become grey
    By weixj2003ld in forum Qt Programming
    Replies: 3
    Last Post: 16th April 2009, 15:44
  5. QTableView & spin
    By fpujol in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2007, 00:51

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.