Results 1 to 8 of 8

Thread: Understanding QWidget's Background/Foreground role

  1. #1
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Question Understanding QWidget's Background/Foreground role

    Hi,

    I have some problems understanding the sense of QPalette::ColorRole.

    I read the documentation a lot of times, and this is how I understood it:
    Every QWidget uses a palette (QPalette) for design settings (BackgroundRole --> background color, ForegroundRole --> foreground color). Also, every QWidget has three states:
    1. active (its parent window has focus)
    2. inactive (another window has focus)
    3. disabled
    For each of these states, the QWidget's palette has a design setting (so each QWidget has three background colors, depending on its state).

    Am I right so far?

    Then, I was wondering about the fact that QPalette::ColorRole has more than one value for background settings:
    - QPalette::Window
    - QPalette::Base
    - QPalette::Button
    ...

    My guess is, that there are pre-defined colors for those "groups". Documentation says, QPalette::Window is used for basically window design, so I think it stands for grey background???
    QPalette::Base is used by editable QWidgets (TextEdit/LineEdit, Table-/Listview and so on), and they all have a white background, so I guess this ColorRole stands for white background???

    If all I mentioned above is right and I'm on the right way, then I don't understand why I can't do the following to change a QLineEdits background color to grey (like window background):
    Qt Code:
    1. QLineEdit* edit;
    2. edit->setBackgroundRole(QPalette::Window);
    To copy to clipboard, switch view to plain text mode 
    What I also do not understand is why the following works to set a red background:
    Qt Code:
    1. QPalette palette;
    2. palette.setColor(QPalette::Base, QColor(255,0,0));
    3. QLineEdit* edit;
    4. edit->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 
    , but this doesn't:
    Qt Code:
    1. QPalette palette;
    2. palette.setColor(QPalette::Window, QColor(255,0,0));
    3. QLineEdit* edit;
    4. edit->setBackgroundRole(QPalette::Window);
    5. edit->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 
    Why does a function of QLineEdit to change its backgroundRole (from standard QPalette::Base to some other) even exist when it does not work??


    Well, I think I did not understand the whole thing and I'm totally on the wrong way. So if there is anyone who understands that all and wants to explain that to me, I would be really glad

    Thank you in anticipation!

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

    Default Re: Understanding QWidget's Background/Foreground role

    Quote Originally Posted by Binary91 View Post
    If all I mentioned above is right and I'm on the right way, then I don't understand why I can't do the following to change a QLineEdits background color to grey (like window background):
    Some styles (e.g. modern Windows) delegate drawing of standard controls to the system so palette settings are ignored for them.
    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. #3
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Understanding QWidget's Background/Foreground role

    Hi and thank you for your reply.

    So do you mean that in case of QLineEdits, they ignore the command 'setBackgroundRole(QPalette::Window)' and always use QPalette::Base, so the only way to change background is using QPalette::setColor(QPalette::Base, QColor) ??

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

    Default Re: Understanding QWidget's Background/Foreground role

    Quote Originally Posted by Binary91 View Post
    So do you mean that in case of QLineEdits, they ignore the command 'setBackgroundRole(QPalette::Window)' and always use QPalette::Base, so the only way to change background is using QPalette::setColor(QPalette::Base, QColor) ??
    No, I mean that in case of a Windows XP or newer running the default Qt style for that platform for any widget the amount of aspects the palette can influence is very limited.
    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.


  5. #5
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Understanding QWidget's Background/Foreground role

    Ok, that sounds like there is no 'general' way to change background/foreground settings for QWidgets when QPalette's influence varies so much, right?
    That's too bad...

    But were my speculations about the way of using QPalette right (first post)? Did I understand that stuff correctly? In principle, would it work to set the QWidget's background color to the same background color as grey windows by setting backgroundRole to QPalette::Window (if OS would allow it) ? Or did I understand the whole stuff fundamentally wrong?

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

    Default Re: Understanding QWidget's Background/Foreground role

    Quote Originally Posted by Binary91 View Post
    Ok, that sounds like there is no 'general' way to change background/foreground settings for QWidgets when QPalette's influence varies so much, right?
    You can try using stylesheets however that will replace the original look of the widget with a more generic look.

    But were my speculations about the way of using QPalette right (first post)? Did I understand that stuff correctly? In principle, would it work to set the QWidget's background color to the same background color as grey windows by setting backgroundRole to QPalette::Window (if OS would allow it) ? Or did I understand the whole stuff fundamentally wrong?
    Launch the application with a different style (e.g. by passing -style plastique in Qt4 or -style fusion in Qt5) and see for yourself.
    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.


  7. #7
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Understanding QWidget's Background/Foreground role

    Ok, I'll try that. Thank's again for supporting :-)

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Understanding QWidget's Background/Foreground role

    Usually the way to change colors is to modify a widget's palette

    Qt Code:
    1. QPalette p = widget->palette();
    2. p.setColor(...);
    3. widget->setPaletted(p);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 10th August 2012, 16:29
  2. Replies: 3
    Last Post: 10th August 2012, 12:51
  3. Replies: 0
    Last Post: 3rd February 2012, 02:05
  4. Replies: 4
    Last Post: 11th January 2011, 07:56
  5. Replies: 9
    Last Post: 14th September 2010, 18:00

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
  •  
Qt is a trademark of The Qt Company.