Results 1 to 3 of 3

Thread: QTableWidget change header background color - QHeaderView

  1. #1
    Join Date
    Sep 2009
    Location
    London
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QTableWidget change header background color - QHeaderView

    In Short,
    Anyone has any clue on how to set background color specific per header in a table???


    Guys, would really appreciate some help with this - looked everywhere and nothing working yet.

    What im trying to do is to have a tablewidget with 2 distinctive horizontal header styles, so some columns headers will have a colour background whilst another columns will have a different color header background.

    The thing is, the basic properties settings(right-click menu) in QT Designer for setting color background looks really buggy and simply does not work, so im trying to do this through stylesheet.

    What i've done is....

    QHeaderView::section
    {
    spacing: 10px;
    background-color:lightblue;
    color: white;
    border: 1px solid red;
    margin: 1px;
    text-align: right;
    font-family: arial;
    font-size:12px;
    }

    but this applies to all the headers.
    then, if i want multiple background colors, there is only one way to do it which is...

    using QHeaderView::section:last or QHeaderView::section:first ,
    which would be perfect IF i only had 3 headers to style, but i am working with many different tables containing 6+ columns!

    Anyone has any clue on how to set background color specific per header in a table?? Appreciate any help!!

    thanks,
    Erik

  2. #2
    Join Date
    Feb 2009
    Posts
    20

    Default Re: QTableWidget change header background color - QHeaderView

    Has anybody found a solution for that?

  3. #3
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: QTableWidget change header background color - QHeaderView

    Quote Originally Posted by Erik_Klein View Post

    QHeaderView::section
    {
    spacing: 10px;
    background-color:lightblue;
    color: white;
    border: 1px solid red;
    margin: 1px;
    text-align: right;
    font-family: arial;
    font-size:12px;
    }

    but this applies to all the headers.
    If you want that to applly to a specific header, you can do it manually by code.

    Qt Code:
    1. QString styleSheet = "::section {" // "QHeaderView::section {"
    2. "spacing: 10px;"
    3. "background-color: lightblue;"
    4. "color: white;"
    5. "border: 1px solid red;"
    6. "margin: 1px;"
    7. "text-align: right;"
    8. "font-family: arial;"
    9. "font-size: 12px; }";
    10.  
    11. tableWidget->horizontalHeader()->setStyleSheet(styleSheet); // tableWidget->verticalHeader()->setStyleSheet(styleSheet);
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by Erik_Klein View Post
    then, if i want multiple background colors, there is only one way to do it which is...
    Maybe, you can use QTableWidgetItem and QTableWidget::setHorizontalHeaderItem(int, QTableWidgetItem*).

    Qt Code:
    1. QTableWidgetItem *col1 = new QTableWidgetItem("Column 1");
    2. col1->setForeground(Qt::white);
    3. col1->setBackground(QColor("lightblue"));
    4. col1->setFont(QFont("arial", 12));
    5. tableWidget->setHorizontalHeaderItem(0, col1); // tableWidget->setVerticalHeaderItem(0, col1);
    To copy to clipboard, switch view to plain text mode 
    But, the background color doesn't work for me.
    Last edited by saa7_go; 17th July 2010 at 21:12. Reason: updated contents

  4. The following user says thank you to saa7_go for this useful post:

    pavanbarot (18th August 2010)

Similar Threads

  1. How to edit Horizontal Header Item in QTableWidget
    By ioannis in forum Qt Programming
    Replies: 6
    Last Post: 5th March 2013, 18:50
  2. Replies: 1
    Last Post: 11th September 2009, 17:03
  3. Setting two horizontal header labels for QTablewidget
    By arunvv in forum Qt Programming
    Replies: 2
    Last Post: 20th August 2009, 17:58
  4. QHeaderView: background of selected header item with CSS
    By antarctic in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2009, 07:37
  5. Change QTableWidget header name or text
    By ricardo in forum Qt Programming
    Replies: 3
    Last Post: 14th May 2009, 11:37

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.