Results 1 to 2 of 2

Thread: Copy of QPalette modifies original

  1. #1
    Join Date
    May 2013
    Posts
    6
    Thanks
    2

    Question Copy of QPalette modifies original

    Question of comprehension

    I would like to temporarily change the text color in the StatusBar.

    Question is, why does the original palette change after the modified palette has been assigned to the StatusBar?

    I already know how to solve this by simply resetting the color.

    Qt Code:
    1. const QPalette& palOrg = statusBar()->palette();
    2.  
    3. QPalette palCopy = palOrg;
    4.  
    5. palCopy.setColor(QPalette::WindowText, Qt::red);
    6.  
    7. qDebug() << palCopy.color(QPalette::WindowText) << palOrg.color(QPalette::WindowText);
    8. //logs QColor(ARGB 1, 1, 0, 0) QColor(ARGB 1, 0, 0, 0)
    9.  
    10. statusBar()->setPalette(palCopy);
    11.  
    12. qDebug() << palCopy.color(QPalette::WindowText) << palOrg.color(QPalette::WindowText);
    13. //logs QColor(ARGB 1, 1, 0, 0) QColor(ARGB 1, 1, 0, 0)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Copy of QPalette modifies original

    Hi, I think it's because statusBar()->palette() returns a QPalette&. So "palOrg" references the internal QPalette object of the status bar, and since it's a reference it reflects all changes made to the object it "points" to.

    Ginsengelf

Similar Threads

  1. Replies: 8
    Last Post: 9th April 2015, 14:45
  2. Replace Original Interface with a new one
    By stbb24 in forum Qt Programming
    Replies: 3
    Last Post: 9th June 2012, 08:06
  3. How to get the index of original SQL table?
    By Kode.Cooper in forum Qt Programming
    Replies: 0
    Last Post: 27th January 2010, 08:43
  4. turning widgets to original state
    By helvin in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2009, 08:35
  5. Replies: 3
    Last Post: 20th September 2009, 23:03

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.