Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to get gray background headers of qtableview



  • This post is deleted!


  • #include "mainwindow.h"
    #include <QHeaderView>
    #include <QTableWidgetItem>
    #include <QLabel>
    #include <QPushButton>

    myWidget::myWidget(QWidget *parent) : QWidget(NULL) {
    hbox = new QHBoxLayout;

    QLabel* label = new QLabel;
    label->setText("Options");
    label->setMaximumWidth(100);
    label->setMinimumHeight(20);
    hbox->addWidget(label);

    QPushButton* push = new QPushButton("+");
    push->setMaximumWidth(30);
    push->setMinimumHeight(20);
    hbox->addWidget(push);

    setMinimumWidth(100);
    setLayout(hbox);

    }

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {

    QTableWidget * t = new QTableWidget;
    t->setColumnCount(5);
    t->setRowCount(3);
    QStringList headerLabels;

    QString col1 = "Label1";
    QString col2 = "Label2";
    QString col3 = "Label3";
    QString col4 = " Label4";
    QString col5 = " Label5";
    t->setStyleSheet("QHeaderView::section { background-color:'light grey' }");

    QColor color(QColor("light grey"));
    QTableWidgetItem* item = new QTableWidgetItem("23");
    item->setData(Qt::BackgroundRole,color);

    QColor color1(QColor("light grey"));
    QTableWidgetItem* item1 = new QTableWidgetItem("9");
    item1->setData(Qt::BackgroundRole,color1);

    QTableWidgetItem* item2 = new QTableWidgetItem("Set1");
    item2->setData(Qt::BackgroundRole,color);

    QTableWidgetItem* item3 = new QTableWidgetItem("Set2");
    item3->setData(Qt::BackgroundRole,color);

    t->setItem(0,0,item);
    t->setItem(1,0,item1);
    t->setItem(0,1,item2);
    t->setItem(1,1,item3);

    QComboBox* combox = new QComboBox;
    t->setCellWidget(0,2,combox);
    combox->addItem(QString(("T1")));
    combox->addItem(QString(("T2")));

    QComboBox* combox_1 = new QComboBox;
    t->setCellWidget(1,2,combox_1);
    combox_1->addItem(QString(("T1")));
    combox_1->addItem(QString(("T2")));

    QLineEdit * edit = new QLineEdit;
    edit->setText(" aaa: aaa ");
    t->setCellWidget(0,3,edit);

    QLineEdit * edit1 = new QLineEdit;
    edit1->setText(" bbb: bbb ");
    t->setCellWidget(1,3,edit1);

    myWidget* m = new myWidget;
    t->setCellWidget(0,4,m);

    myWidget* n = new myWidget;
    t->setCellWidget(1,4,n);

    headerLabels << col1 << col2 << col3 << col4 << col5;
    t->setHorizontalHeaderLabels(headerLabels);
    t->resizeColumnsToContents();
    t->verticalHeader()->hide();
    t->show();

    }

    MainWindow::~MainWindow()
    {

    }


Log in to reply