PDA

View Full Version : QTableWidget alternating background color



Aslan
6th June 2011, 11:31
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


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QTableWidget>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
QTableWidget *myTable;
~MainWindow();
};

#endif // MAINWINDOW_H


mainwindow.cpp


#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
myTable=new QTableWidget();
myTable->setColumnCount(1);
myTable->setRowCount(5);
myTable->show();
QTableWidgetItem *myTableWidget=new QTableWidgetItem("First Entry ...");
myTable->insertRow(0);
myTable->setItem(0,0,myTableWidget);
QTableWidgetItem *myTableWidget2=new QTableWidgetItem("Second Entry...");
myTable->setItem(1,0,myTableWidget2);
myTable->resizeColumnsToContents();
myTable->setAlternatingRowColors(true);
}

MainWindow::~MainWindow()
{

}


main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


Stylesheet widgettest.qss

QTableWidget {
alternate-background-color: yellow;
background-color: red;
}

deepal_de
7th June 2011, 07:19
did you set Alternating Row Colors property to "true"

try ths


myTable->setAlternatingRowColors(true);
myTable->setStyleSheet("alternate-background-color: yellow;background-color: red;");


this works fine...

Aslan
7th June 2011, 08:41
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.

swdev
22nd May 2014, 07:32
did you set Alternating Row Colors property to "true"

try ths


myTable->setAlternatingRowColors(true);
myTable->setStyleSheet("alternate-background-color: yellow;background-color: red;");


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! :D )