Results 1 to 3 of 3

Thread: Alternating colors in QTableWidget

  1. #1
    Join Date
    Dec 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question Alternating colors in QTableWidget

    Hello,

    We are trying to use some custom items in a QTableWidget via setCellWidget() call. We are adding some QPushButton and QComboBox objects.

    That works fine, but we also want to use the alternating colors feature of QTableView. In our stylesheet we specify alternating colors for QPushButton, QComboBox, etc like this:

    QLineEdit,
    QPushButton,
    QComboBox,
    QMenu {
    selection-color: black;
    selection-background-color: lightgrey;
    background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 black, stop: 1 rgb(128, 128, 128));
    alternate-background-color: rgb(255, 0, 127);
    min-width: 14px;
    min-height: 14px;
    border: 1px solid gray;
    padding: 2px;
    border-radius: 3px;
    }

    However, when push buttons and combo boxes are added to the table they all appear the same color with a little bit of the background color being drawn at the very corners.

    Any ideas on how to fix this?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Alternating colors in QTableWidget

    Widget's are not tied to the model in any way, they have no concept of being "alternate" and the view doesn't handle them in any way other than moving them when it scrolls its viewport.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  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: Alternating colors in QTableWidget

    And since the view has no idea about the buttons, you need to set the backgroundRole for the button's palette to the alternate color manually when you insert the buttons into the cell. Alternatively, set the button's autoFillBackground to false and the underlying cell color will be visible. You will need to remove the background color specification from the style sheet, because the style sheet overrides this setting.

    You could also try playing around with the button opacity, but this will probably affect the foreground text as well as the background fill.

Similar Threads

  1. QTableWidget Highlighting and Selection Colors
    By Slewman in forum Qt Programming
    Replies: 1
    Last Post: 11th February 2010, 23:14
  2. Setting background colors to rows in QTableWidget
    By cnbp173 in forum Qt Programming
    Replies: 0
    Last Post: 24th April 2009, 18:38
  3. Do it yourself alternating row colors.
    By barnabyr in forum Qt Programming
    Replies: 3
    Last Post: 1st November 2008, 18:30
  4. QTreeView -- Alternating Row Colors Oddity
    By chuckshaw in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 20:41
  5. Draw a rectangle alternating two colors with qPainter
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 25th January 2006, 00:12

Tags for this Thread

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.