Results 1 to 6 of 6

Thread: QTableWidget set Row Color

  1. #1
    Join Date
    Jul 2010
    Posts
    33
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QTableWidget set Row Color

    Hi,

    How to set the color of QTableWidget row. Want to set the color of specified row.

    Thanks & Regards

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget set Row Color

    Iterate through the items of a given row and use QTableWidgetItem::setBackground
    Or alternatively you can use delegates...

  3. #3
    Join Date
    Jul 2010
    Posts
    33
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableWidget set Row Color

    Iterating take too much time to set row color. if I want to change the color of 1000000 nth item then iterating this take time and my program get slow. I need a function to change the row by passing only row Index.

    I am using this code:
    model->setData(model->index(Row,Column,QModelIndex()),QColor(Qt::red),Q t::BackgroundRole);

    But in this I required to go through all column for changing the same row color.

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget set Row Color

    Then get rid of QTableWidget, use QTableView and implement your own model, where you can store colors for each row, and return this color in data() for each column in row. You can add method to your model (it can be even slot) setRowColor(int row, const QColor &color) where you can set the color for row and emit dataChanged() signal for whole row at once.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Jul 2010
    Posts
    33
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableWidget set Row Color

    I am using QModelIndex to set the value. In QModelIndex we need to pass row and column number . I am searching a function that will take only row number in QModelIndex

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget set Row Color

    QModelIndex represents only one index (item), which is in particular row and column. If you want to treat row as a one object then make your own model and add functionality to it allowing you to operate on whole rows.

    As I said in the previous post: Subclass QAbstractItemModel (or any other model class if you want their functionality) and keep there lets say a list (or maybe a map if you find that more suitable for your needs) of colors where color at position r is a color of row number r. Then in data() method return appropriate color for given index, where you can check only row of index. Then you can add some method e.g. setRowColor(int r, const QColor &c) where you can set color in a list, and update whole row at once by emitting QAbstractItemModel::dataChanged() covering indexes for whole row.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. change color/focus in qtablewidget
    By mmm286 in forum Newbie
    Replies: 7
    Last Post: 5th June 2013, 03:58
  2. Replies: 4
    Last Post: 17th July 2010, 04:47
  3. QTableWidget, grid color bug?
    By greenvirag in forum Qt Programming
    Replies: 6
    Last Post: 18th June 2009, 10: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
  •  
Qt is a trademark of The Qt Company.