Results 1 to 5 of 5

Thread: Background color for rows in a QTableWidget: How to do that?

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Background color for rows in a QTableWidget: How to do that?

    Hello!

    I have a QTableWidget in my program and i want to set a background rgb color for some rows, but i read QTableWidgetItem::setBackgroundColor is obsolete and i'm using Qt 5.4.1 with QtCreator 3.4.0. How can i do what i want? I really don't want to change to QTableView.

    Thanks!

  2. #2
    Join Date
    Mar 2015
    Posts
    16
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Background color for rows in a QTableWidget: How to do that?

    Hi, have the same problem

    Hope for some answers too

    In my case I use QSortFilterProxyModel to first filter the data. This data can be further selected for use using a combobox
    In my combobox the user can select options 1- 3 and that shall set background for a group of data in the tableview, meaning first to third group of 4 rows each.

    Thinking of adding a column and data to filter like "group1", "group2", "group3" - is that a better idea?

    any ideas?

    /anders
    Last edited by andsun; 20th May 2015 at 15:46.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Background color for rows in a QTableWidget: How to do that?

    These are two different questions. QTableView and QTableWidget use different methods for setting the background color for rows.

    For QTableWidget, when you add or modify the QTableWidgetItem(s) in the rows, you set the color using QTableWidgetItem::backgroundBrush().

    If you are using QTableView with a QAbstractItemModel (or proxy), you implement the Qt::BackgroundRole role in the QAbstractItemModel::data() method to set the QBrush used to fill the cells in the row with the appropriate color.

  4. #4
    Join Date
    Mar 2015
    Posts
    16
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Background color for rows in a QTableWidget: How to do that?

    Hi d_strantz,

    OK, good to know!
    Give it a google & try

  5. #5
    Join Date
    Mar 2015
    Posts
    16
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Background color for rows in a QTableWidget: How to do that?

    Thanks, that did the trick for me
    /a

Similar Threads

  1. QTableWidget alternating background color
    By Aslan in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2014, 07:32
  2. Replies: 4
    Last Post: 17th July 2010, 04:47
  3. Setting background colors to rows in QTableWidget
    By cnbp173 in forum Qt Programming
    Replies: 0
    Last Post: 24th April 2009, 17:38
  4. Replies: 1
    Last Post: 23rd March 2009, 21:36
  5. QTableWidget Background Color
    By mbrusati in forum Qt Programming
    Replies: 2
    Last Post: 23rd September 2008, 18: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.