Results 1 to 3 of 3

Thread: How to set background color one row in TableWidget?

  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default How to set background color one row in TableWidget?

    Hi,

    I've a TableWidget:

    Qt Code:
    1. TableResult = new QTableWidget;
    2. TableResult->setRowCount(10);
    3. TableResult->setColumnCount(5);
    4. for(int c=1;c<5;c++){
    5. for(int r=1;r<10;r++){
    6. QTableWidgetItem *tmpItem;
    7. tmpItem= new QTableWidgetItem(tr("%1 x %2").arg(c).arg(r));
    8. TableResult->setItem(r, c, tmpItem);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    How to set background color rows number 1, 3, 5, 7, 9 to (example) blue, and rows number 2,4,6,8 to red?

    If I had a model-view-delegate pattern, I could do it in delegate, but I've any view, any model, any delegate :/

  2. #2
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set background color one row in TableWidget?

    Qt Code:
    1. TableResult = new QTableWidget;
    2. TableResult->setRowCount(10);
    3. TableResult->setColumnCount(5);
    4.  
    5. QTableWidgetItem *tmpItem; //why create a new item on the heap in every loop pass?
    6. QBrush blue(Qt::blue);
    7. QBrush red(Qt::red);
    8.  
    9. for(int c=0;c<4;c++) {
    10. for(int r=0;r<9;r++){
    11. tmpItem= new QTableWidgetItem(tr("%1 x %2").arg(c + 1).arg(r + 1));
    12.  
    13. if (r % 2 == 1)
    14. tmpItem->setForeground(blue)
    15. else
    16. tmpItem->setForeground(red);
    17. TableResult->setItem(r, c, tmpItem);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    There you have it, pretty simple.
    But if you really just want to alternate the row colors, this would be much better.


    Edit:
    I almost forgot: you're starting with 1 in both your for-loops. Remember that the TableWidgets indexes start at 0 and end at rowCount - 1. I have corrected that, s.a.

    Edit 2:
    Of course you can use a delegate with a QTableWidget, it's also part of the model-view architecture.
    See here
    Last edited by ChiliPalmer; 29th September 2009 at 00:34.

  3. The following user says thank you to ChiliPalmer for this useful post:

    TomASS (29th September 2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set background color one row in TableWidget?

    Thank you,

    There you have it, pretty simple.
    Yes, that's simple

    But if you really just want to alternate the row colors, this would be much better.
    Yest, that's what I meant.

    I almost forgot: you're starting with 1 in both your for-loops. Remember that the TableWidgets indexes start at 0 and end at rowCount - 1. I have corrected that, s.a.
    Yest, I know, thank you for your attention.

    Of course you can use a delegate with a QTableWidget, it's also part of the model-view architecture.
    I've wrote, I fourtunately havent't use model-view-delegate architecture.

Similar Threads

  1. Replies: 2
    Last Post: 17th July 2010, 21:07
  2. QTabWidget transparent background problem
    By destroyar0 in forum Qt Programming
    Replies: 10
    Last Post: 25th June 2009, 12:19
  3. QLCDNumber with transparent background?
    By PolyVox in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2008, 05:34
  4. background colour
    By kw in forum Qt Programming
    Replies: 6
    Last Post: 11th April 2006, 00:44
  5. Replies: 1
    Last Post: 5th April 2006, 16:44

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.