Results 1 to 4 of 4

Thread: QTableWidget alternating background color

  1. #1
    Join Date
    May 2010
    Location
    Somewhere in Southern Germany
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTableWidget alternating background color

    I stripped my problem down to a simple testcase. I want to display the rows of a QTableWidget with alternating colors using a stylesheet.
    When running the program with the -stylesheet widgettest.qss option, I only see the default colors for the alternating background, not the colors defined in the stylesheet.
    I expected either the first or second entry to use yellow as background but that is not the case as you can see in the screenshot. Any Idea what I'm doing wrong?

    Here's the code
    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QTableWidget>
    6.  
    7. class MainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MainWindow(QWidget *parent = 0);
    13. QTableWidget *myTable;
    14. ~MainWindow();
    15. };
    16.  
    17. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. myTable=new QTableWidget();
    7. myTable->setColumnCount(1);
    8. myTable->setRowCount(5);
    9. myTable->show();
    10. QTableWidgetItem *myTableWidget=new QTableWidgetItem("First Entry ...");
    11. myTable->insertRow(0);
    12. myTable->setItem(0,0,myTableWidget);
    13. QTableWidgetItem *myTableWidget2=new QTableWidgetItem("Second Entry...");
    14. myTable->setItem(1,0,myTableWidget2);
    15. myTable->resizeColumnsToContents();
    16. myTable->setAlternatingRowColors(true);
    17. }
    18.  
    19. MainWindow::~MainWindow()
    20. {
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Stylesheet widgettest.qss
    Qt Code:
    1. alternate-background-color: yellow;
    2. background-color: red;
    3. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Last edited by Aslan; 6th June 2011 at 12:32. Reason: typo

  2. #2
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QTableWidget alternating background color

    did you set Alternating Row Colors property to "true"

    try ths
    Qt Code:
    1. myTable->setAlternatingRowColors(true);
    2. myTable->setStyleSheet("alternate-background-color: yellow;background-color: red;");
    To copy to clipboard, switch view to plain text mode 

    this works fine...
    Last edited by deepal_de; 7th June 2011 at 08:31.

  3. #3
    Join Date
    May 2010
    Location
    Somewhere in Southern Germany
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget alternating background color

    OK, looks like I found the problem. I tried with another computer and got it to work under windows. The I thought it was a "Windows" vs. "Linux" kind of problem.. but.. it seems like the QT version I originally used on the linux box is too old (4.7.0). It works under windows in 4.7.3 and on another Linux machine with 4.7.3 as well. So, I will upgrade my machine to 4.7.3 immediately.
    Thanks for trying this out.

  4. #4
    Join Date
    Sep 2013
    Location
    Yogyakarta, Indonesia
    Posts
    3
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTableWidget alternating background color

    Quote Originally Posted by deepal_de View Post
    did you set Alternating Row Colors property to "true"

    try ths
    Qt Code:
    1. myTable->setAlternatingRowColors(true);
    2. myTable->setStyleSheet("alternate-background-color: yellow;background-color: red;");
    To copy to clipboard, switch view to plain text mode 

    this works fine...

    I just want to confirm that this is working great for me.
    Qt 4.8.5 on Windows 8 64bit (like that mean something! )

Similar Threads

  1. Replies: 2
    Last Post: 17th July 2010, 22:07
  2. Replies: 4
    Last Post: 17th July 2010, 05:47
  3. Replies: 1
    Last Post: 11th September 2009, 18:03
  4. Replies: 1
    Last Post: 23rd March 2009, 22:36
  5. QTableWidget Background Color
    By mbrusati in forum Qt Programming
    Replies: 2
    Last Post: 23rd September 2008, 19:38

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.