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;
}
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;
}