Results 1 to 13 of 13

Thread: Memory usage problem when setting style sheet

  1. #1
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Memory usage problem when setting style sheet

    I have a GUI application in which I have several QPushButtons and when I click one I give it a red border by calling myPushButton->setStyle("border:3px solid #ff0000; background-color:black;"); and I set the style sheet on my other buttons to a blank string, i.e. myPushButton->setStyle( "" );

    That's all I'm doing, but as I do this, my memory usage continually grows every time I click a button (i.e. when calling setStyleSheet). FYI, I'm currently developing on windows and looking at my task manager under the "Mem Usage" column. Any idea why this would be happening?

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

    Default Re: Memory usage problem when setting style sheet

    Did you try to see the mem usage when the stylesheet code is commented out?
    It is more likely you have a memory leak somewhere else in your code.
    ==========================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
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Memory usage problem when setting style sheet

    Yes, I tried commenting out the setStyleSheet part, but when I do the mem usage doesn't grow like it does with it there...

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

    Default Re: Memory usage problem when setting style sheet

    but when I do the mem usage doesn't grow like it does with it there...
    Doesn't grow *like*?
    This distinction is important!
    If it does grow, even if not by much, then the problem is somewhere in your code.
    The amount of memory leaking may change as you use more resources, which you do when using the style.
    But the leak it self is not in the style code, since you still leak if it commented out.
    ==========================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
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Memory usage problem when setting style sheet

    I eliminated the style sheets altogether and now I'm just changing the appearance of the buttons using setPalette(), but the problem still remains. The memory grows when changing the palette of the clicked one. If I keep clicking button1, then button2, then button1, etc. etc. back and forth the memory grows each time. Pretty sure it's not a leak somewhere else in the code because when I comment out the setPalette() call, the memory does not grow. Here's some code that may help clarify:

    Qt Code:
    1. void MenuManager::changeButtonAppearance( QPushButton * button, const QColor & bgColor, const QColor & textColor, int bgAlpha )
    2. {
    3. if( button )
    4. {
    5. QPalette p( button->palette() );
    6. QColor theBgColor( bgColor );
    7. theBgColor.setAlpha( bgAlpha );
    8. p.setColor( QPalette::Button, theBgColor );
    9. p.setColor( QPalette::ButtonText, textColor );
    10. button->setPalette( p );
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MenuManager::resetButtonStyles()
    2. {
    3. for( int i = 0; i < buttons.count(); i++ )
    4. {
    5. changeButtonAppearance(
    6. buttons.at( i ),
    7. QColor( Qt::black ),
    8. QColor( Qt::yellow ),
    9. 128 );
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MenuManager::setButtonAsSelected( int index )
    2. {
    3. if( index >= 0 && index < buttons.count() )
    4. {
    5. resetButtonStyles();
    6. changeButtonAppearance(
    7. buttons.at( index ),
    8. QColor( Qt::red ),
    9. QColor( Qt::yellow ),
    10. 128 );
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    When I click a particular button, I call setButtonAsSelected() with an index depending on which button was clicked... intent is to set the background color of the button I clicked to red and set the other ones to black...

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

    Default Re: Memory usage problem when setting style sheet

    Can you show the code where you allocate the buttons?
    ==========================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
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Memory usage problem when setting style sheet

    Are you perhaps using custom QStyle for your application?

  8. #8
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Memory usage problem when setting style sheet

    No, not using any custom styles.

    Allocate the buttons? The are allocated according to the .ui file in which I made them and then I simply added them to a QList<QPushButton*> at runtime (buttons.append( _ui->button1 ), etc );

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

    Default Re: Memory usage problem when setting style sheet

    Can you then show the "working slot" for the clicked buttons?
    Somewhere you have a either a leak, or it not a leak, but a valid allocation of resources, and it from what you are saying it is when you click the buttons, so it has to do with what every your code is doing at a respond to a button click.
    ==========================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.

  10. #10
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Memory usage problem when setting style sheet

    Qt Code:
    1. void MenuManager::initButtons()
    2. {
    3. buttons.append( button1 );
    4. buttons.append( button2 );
    5. ...
    6. ...
    7.  
    8. _buttonSignalMapper = new QSignalMapper( this );
    9.  
    10. foreach( QPushButton * button, buttons )
    11. {
    12. connect( button, SIGNAL( clicked() ), _buttonSignalMapper, SLOT( map() ) );
    13.  
    14. changeButtonAppearance( button,
    15. QColor( Qt::black ),
    16. QColor( Qt::yellow ),
    17. 128 );
    18.  
    19. buttons.at( i )->setWhatsThis( "command" + QString::number( i ) );
    20. _buttonSignalMapper->setMapping( buttons.at( i ), buttons.at( i )->whatsThis() );
    21. }
    22.  
    23. connect( _buttonSignalMapper, SIGNAL( mapped( const QString & ) ),
    24. this, SLOT( performCommand( const QString & ) ) );
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MenuManager::performCommand( const QString & command )
    2. {
    3. if( command == "command0" )
    4. {
    5. setButtonAsSelected( 0 );
    6. }
    7. else if( command == "command1" )
    8. {
    9. setButtonAsSelected( 1 );
    10. }
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Oct 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Memory usage problem when setting style sheet

    Hi,

    You probably could change your code a bit to remember the last clicked push button and change the appearance of just that + the new button that has been clicked. You seem to be looping over the whole button set.

    Just use a variable prevIndex and set it to curIndex when you paint it red.

    This might reduce the memory footprint.

    - Raja.

  12. #12
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Memory usage problem when setting style sheet

    I realize I am iterating over the whole button set and I did try what you said using a prevIndex, etc. but it does not seem to make a difference. I just don't understand what is going on here... why would the memory be growing every time I change the style of a button with a black background to have a style with a red background, etc... if I change one line in my changeButtonAppearance function and pass Qt::black instead of making a "color change" to Qt::red, the memory does not grow:

    Qt Code:
    1. void MenuManager::setButtonAsSelected( int index )
    2. {
    3. if( index >= 0 && index < buttons.count() )
    4. {
    5. resetButtonStyles();
    6. changeButtonAppearance(
    7. buttons.at( index ),
    8. QColor( Qt::black ), // if this is Qt::red or another color, memory grows :(
    9. QColor( Qt::yellow ),
    10. 128 );
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by DiamonDogX; 17th November 2009 at 18:57.

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

    Default Re: Memory usage problem when setting style sheet

    maybe you should run a profiler on it, see valgrind for example.
    ==========================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.

Similar Threads

  1. Style Sheet Problem
    By zgulser in forum Qt Tools
    Replies: 8
    Last Post: 17th February 2009, 08:27
  2. Replies: 0
    Last Post: 26th September 2008, 21:33
  3. QToolBox qss style sheet problem
    By eskil in forum Qt Programming
    Replies: 1
    Last Post: 17th August 2007, 17:45
  4. Qt 4.3 Style sheet, new features
    By Angelo Moriconi in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2007, 15:22
  5. Memory Problem with SIGNALS and SLOTS
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2007, 20:39

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.