Results 1 to 7 of 7

Thread: QPalette::brush

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 4 Times in 4 Posts

    Default QPalette::brush

    Currently, I am using a QTableWidget that has alternating colors, however I wanted to alternate the color every other row instead of every row. I made a function for this and it works properly. The problem is, that I still want to use QPalette::AlternateBase and QPalette::Base for the colors.

    Currently I can color the brush as follows:
    Qt Code:
    1. QBrush tableNoHighlight;
    2. tableNoHighlight.setStyle (Qt::SolidPattern);
    3. tableNoHighlight.setColor(Qt::white);
    4.  
    5. QBrush tableYesHighlight;
    6. tableYesHighlight.setStyle (Qt::SolidPattern);
    7. tableYesHighlight.setColor(Qt::lightGray);
    To copy to clipboard, switch view to plain text mode 
    However, since the colors are hard set, the highlighting does not match the particular look and feel of the OS that the program might be run in. Thus I wanted to use QPalette::AlternateBase and QPalette::Base.

    I tried the following:
    Qt Code:
    1. tableNoHighlight = QPalette::brush(QPalette::AlternateBase);
    2. tableNoHighlight = QPalette::brush(QPalette::Base);
    To copy to clipboard, switch view to plain text mode 
    and I got this error:
    alculatorform.cpp:81: error: cannot call member function `const QBrush& QPalette::brush(QPalette::ColorRole) const' without object
    alculatorform.cpp:87: error: cannot call member function `const QBrush& QPalette::brush(QPalette::ColorRole) const' without object
    so I tried:
    Qt Code:
    1. tableNoHighlight = this->brush(QPalette::AlternateBase);
    2. tableNoHighlight = this->brush(QPalette::Base);
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. tableNoHighlight = ui->brush(QPalette::AlternateBase);
    2. tableNoHighlight = ui->brush(QPalette::Base);
    To copy to clipboard, switch view to plain text mode 
    and both yielded errors about having no member named brush.

    How can I get the brush of QPalette::AlternateBase and QPalette::Base (or at least the color value) to use elsewhere?

  2. #2
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    7
    Thanked 25 Times in 24 Posts

    Default Re: QPalette::brush

    use the following:
    Qt Code:
    1. setBackgroundRole( QPalette::AlternateBase);
    2. setBackgroundRole( QPalette::Base);
    3.  
    4. OR
    5.  
    6. QPalette palette;
    7. palette.setColor(m_tabel->backgroundRole(), Qt::transparent); // any Qt::color
    8. m_tabel->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 4 Times in 4 Posts

    Default Re: QPalette::brush

    perhaps i didn't explain well enough what I am doing. I am coloring the rows in a QTableWidget in alternating colors, but 2 rows 1 color, then 2 rows the other. The only way I have found so far was to color the cells as I generate the rows:

    Qt Code:
    1. ui.tablewidget->item(0, 3)->setBackground(highlightBrush);
    2. ui.tablewidget->item(0, 4)->setBackground(highlightBrush);
    3. //etc...
    To copy to clipboard, switch view to plain text mode 
    This works, however it does not completely match the base and alternate base color (because I don't know how to construct brushes that take from those colors)

    The background role method:
    Qt Code:
    1. //this produces errors as a qtablewidgetitem does not have setBackgroundRole()
    2. ui.tableWidget->item(0, 1)->setBackgroundRole(QPalette::AlternateBase);
    3. ui.tableWidget->item(0, 2)->setBackgroundRole(QPalette::Base);
    To copy to clipboard, switch view to plain text mode 
    Will not work because QTableWidgetItem does not accept setBackgroundRole, it only accepts a brush in setBackground()

    so all I need to know really, is how to create a brush from the color used in QPalette::AlternateBase and QPalette::Base or use the existing brush for those colors (but how to reference it I have no clue.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QPalette::brush

    Quote Originally Posted by tpf80 View Post
    so all I need to know really, is how to create a brush from the color used in QPalette::AlternateBase and QPalette::Base
    Ask QPalette to give you one.

  5. #5
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 4 Times in 4 Posts

    Default Re: QPalette::brush

    I actually tried this (see above) but I got errors. (this was the way I really wanted to do it)
    Qt Code:
    1. tableNoHighlight = QPalette::brush(QPalette::AlternateBase);
    2. tableNoHighlight = QPalette::brush(QPalette::Base);
    To copy to clipboard, switch view to plain text mode 
    it asks for an object, but I am not quite sure what object its asking for.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QPalette::brush

    Quote Originally Posted by tpf80 View Post
    it asks for an object, but I am not quite sure what object its asking for.
    QPalette::brush() isn't a static method, so you have to create a QPalette object first:
    Qt Code:
    1. tableNoHighlight = p.brush(QPalette::Base);
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to jacek for this useful post:

    tpf80 (18th July 2007)

  8. #7
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 4 Times in 4 Posts

    Smile Re: QPalette::brush

    ah I see.. I knew it was something simple that I was missing...

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.