Results 1 to 6 of 6

Thread: [QTable] Change row's color

  1. #1
    Join Date
    Jul 2006
    Location
    Cagliari - Sardinia - Italy
    Posts
    26
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default [QTable] Change row's color

    Hi all,
    I want to change the color of a row in a table, when a checkbutton is checked. Checkbox is a QChecktableItem in a column. I've captured valueChanged(int, int) emitted by the QTable, but i don't know how to change the background color of the row
    Can somebody help me?

    Thnaks

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

    Default Re: [QTable] Change row's color

    You have to subclass QCheckTableItem and reimplement paint(). All you have to do inside it is to change background color in QColorGroup that was passed to paint() and invoke original paint() implementation.

    Here's something similar: http://www.qtcentre.org/forum/f-qt-p...item-2713.html (look for SRadioButtonTableItem::paint).

  3. #3
    Join Date
    Jul 2006
    Location
    Cagliari - Sardinia - Italy
    Posts
    26
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: [QTable] Change row's color

    Thank you!
    I've tried to do it but it doesn't work. Row color doesn't change What's wrong?!?

    This is the declaration

    Qt Code:
    1. class EnableTableItem : public QCheckTableItem {
    2. public:
    3. EnableTableItem(QTable * table, const QString & txt);
    4. ~EnableTableItem();
    5.  
    6. protected:
    7. virtual void paint (QPainter *p, const QColorGroup &cg,
    8. const QRect &cr, bool selected);
    9. };
    To copy to clipboard, switch view to plain text mode 

    And this is my paint reimplementation

    Qt Code:
    1. void EnableTableItem::paint( QPainter * p, const QColorGroup & cg,
    2. const QRect & cr, bool selected )
    3. {
    4. QColorGroup color(cg);
    5. if (isChecked())
    6. color.setColor(QColorGroup::Background, Qt::green);
    7. else
    8. color.setColor(QColorGroup::Background, Qt::red);
    9.  
    10. QCheckTableItem::paint(p, color, cr, selected);
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: [QTable] Change row's color

    Try QColorGroup::Base instead of QColorGroup::Background.

  5. #5
    Join Date
    Jul 2006
    Location
    Cagliari - Sardinia - Italy
    Posts
    26
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Talking Re: [QTable] Change row's color

    Oh yes That's right!
    But now, if I want to change the color of the entire row, I think's it's better to subclass QTable and reimplement paint method... it's the right way?

    Thanks a lot!

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

    Default Re: [QTable] Change row's color

    Quote Originally Posted by villy View Post
    if I want to change the color of the entire row, I think's it's better to subclass QTable and reimplement paint method... it's the right way?
    You can reimplement QTable::paintCell() exactly the same way and you'll get the whole row painted in green or red, but you can also stay with custom table items.

    It just depends whether you want to have a custom table or custom items.

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

    villy (16th November 2006)

Similar Threads

  1. Change background color of QPushButton
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2013, 10:23
  2. How to change color of Tab in QTabWidget?
    By rajesh in forum Qt Programming
    Replies: 10
    Last Post: 21st October 2013, 07:52
  3. how to change QGroupBox frame color? (Qt4)
    By high_flyer in forum Qt Programming
    Replies: 7
    Last Post: 9th May 2006, 13:48
  4. QTableView change color of current Cell
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 11:22
  5. Change the text color tab page
    By gtthang in forum Qt Programming
    Replies: 4
    Last Post: 18th February 2006, 17:38

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.