Unsolved QTableWidget: how to highlight headers section programmatically + changing cell custom widget style
-
following this thread
I''ve created a custom table by inheriting from QTableWidget, and I had to use custom headers too. .
My requirements were these:- No cluster of cells selectable
- row selection without highlighting the column header
- column selection without highlighting the row header
- no multiple row selection
- no multiple column selection
- cells must contain a custom widget with a label inside.
I could achieve most of my objectives but i have the following issues:
I can't find a way to highlight programmatically a section of a header (column and rows)
cell selected have, as their widgets have no background color, they get the table selected background colors accordingly to stylesheet but I need to change widget labels foreground color as well.
I set normal foreground color with"QTableWidget QLabel { color: #FFFFFF ; }
I'd like to find a way to set in stylesheet a color to assign to labels when item is selected without doing it manually by assigning my widgets style sheet when the cell where they are is selected or unselected.
this is the whole minimal code
main.cpp
#include "mainwindow.h" #include <QApplication> #include <QTableWidget> #include <QHeaderView> int main(int argc, char *argv[]) { QApplication a(argc, argv); a.setStyleSheet( "QTableWidget { background-color: #000000; alternate-background-color: #222222; gridline-color: #AAAAAA; color: #FF00FF; selection-background-color: #FFC000; selection-color: #0000FF}" "QTableWidget QHeaderView { background-color: #00000000;}" "QTableWidget QHeaderView::section { background-color: #444444; color: #FFFFFF; border: none; border-style: none;}" "QTableWidget QHeaderView::section:checked { background-color: #FFC000; color: #000000;}" "QHeaderView::section:horizontal { border-right: 1px solid #AAAAAA; }" "QHeaderView::section:vertical { border-bottom: 1px solid #AAAAAA; }" "QTableWidget QTableCornerButton::section { background: #00000000; border: 0px }" "QTableWidget QLabel { color: #FFFFFF; }" ); MainWindow w; w.show(); return a.exec(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMdiArea> #include <QTableWidget> #include <QHeaderView> class CustomWidget : public QWidget { public: CustomWidget(); }; class ColumnHeader : public QHeaderView { public: ColumnHeader(); protected: void mousePressEvent(QMouseEvent *e) override; void mouseMoveEvent(QMouseEvent *e) override; private: }; class RowHeader : public QHeaderView { public: RowHeader(); protected: void mousePressEvent(QMouseEvent *e) override; void mouseMoveEvent(QMouseEvent *e) override; private: }; class CustomTable : public QTableWidget { Q_OBJECT public: CustomTable(); protected: void mousePressEvent(QMouseEvent * event) override; protected slots: void onColumnSelected(int); void onRowSelected(int); void onCellSelected(int, int); }; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); protected slots: private: QMdiArea * mArea; CustomTable * table ; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include <QTableWidget> #include <QHeaderView> #include <QMdiSubWindow> #include <QBoxLayout> #include <QSizePolicy> #include <QCheckBox> #include <QLabel> #include <QDebug> #include <QMouseEvent> #include "mainwindow.h" CustomWidget::CustomWidget() { QHBoxLayout * layout = new QHBoxLayout(); QCheckBox * checkbox = new QCheckBox(); QLabel * label = new QLabel("Pluto"); //label->setStyleSheet("background-color: #700700;"); layout->addWidget(checkbox); layout->addWidget(label); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); setLayout(layout); } ColumnHeader::ColumnHeader():QHeaderView(Qt::Horizontal) { } void ColumnHeader::mousePressEvent(QMouseEvent *event) { int index = logicalIndexAt(event->pos()); qDebug() << __FUNCTION__ << "index " << index ; emit(sectionPressed(index)); } void ColumnHeader::mouseMoveEvent(QMouseEvent *event) { } RowHeader::RowHeader():QHeaderView(Qt::Vertical) { } void RowHeader::mousePressEvent(QMouseEvent *event) { int index = logicalIndexAt(event->pos()); qDebug() << __FUNCTION__ << "index " << index ; emit(sectionPressed(index)); } void RowHeader::mouseMoveEvent(QMouseEvent *event) { } CustomTable::CustomTable() { setAlternatingRowColors(true); ColumnHeader * ch = new ColumnHeader(); setHorizontalHeader(ch); RowHeader * rh = new RowHeader(); setVerticalHeader(rh); QStringList verHeaderList({"One", "Two", "Three"}); setRowCount(verHeaderList.size()); setColumnCount(5); setVerticalHeaderLabels(verHeaderList); verticalHeader()->setVisible(true); setCellWidget(1, 1, new CustomWidget()); disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)), this, SLOT(selectColumn(int))); disconnect(verticalHeader(), SIGNAL(sectionPressed(int)), this, SLOT(selectRow(int))); setEditTriggers(QAbstractItemView::NoEditTriggers); setSelectionMode(QAbstractItemView::ExtendedSelection); connect(horizontalHeader(), SIGNAL(sectionPressed(int)), this, SLOT(onColumnSelected(int))); connect(verticalHeader(), SIGNAL(sectionPressed(int)), this, SLOT(onRowSelected(int))); connect(this, SIGNAL(cellClicked(int, int)), this, SLOT(onCellSelected(int, int))); } void CustomTable::mousePressEvent(QMouseEvent *event) { if (event->buttons() == Qt::RightButton) { return; } clearSelection(); QModelIndex idx = indexAt(event->pos()); if (!idx.isValid()) { // ... } else { selectionModel()->select(idx, QItemSelectionModel::Select); emit(cellClicked(idx.row(), idx.column())); } } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { mArea = new QMdiArea(); setCentralWidget(mArea); QMdiSubWindow * sw = new QMdiSubWindow(); sw->resize(900, 600); mArea->addSubWindow(sw); sw->show(); QWidget * widget = new QWidget(); QVBoxLayout * vlayout = new QVBoxLayout(); widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); widget->setLayout(vlayout); sw->setWidget(widget); table = new CustomTable(); //connect(table, SIGNAL(cellClicked(int, int)), this, SLOT(onCellSelected(int, int))); //table->setItemSelected( table->verticalHeaderItem(1), true); table->show(); //table->setSelectionMode(QAbstractItemView::SingleSelection); //table->selectRow(0); vlayout->addWidget(table); } MainWindow::~MainWindow() { } void CustomTable::onColumnSelected(int index) { qDebug() << __FUNCTION__ << "index " << index; clearSelection(); horizontalHeader()->clearSelection(); QModelIndex idx1 = model()->index(0,index); QModelIndex idx2 = model()->index(rowCount() - 1, index); QItemSelection selection(idx1, idx2); selectionModel()->select(selection, QItemSelectionModel::Select); } void CustomTable::onRowSelected(int index) { qDebug() << __FUNCTION__ << "index " << index; clearSelection(); QModelIndex idx1 = model()->index(index, 0); QModelIndex idx2 = model()->index(index, columnCount() - 1); QItemSelection selection(idx1, idx2); selectionModel()->select(selection, QItemSelectionModel::Select); } void CustomTable::onCellSelected(int r, int c) { qDebug() << __FUNCTION__ << "row index " << r << " column index " << c; }
Img 1: ok but I need to highlight horizontal section header as well (column header)
Img 2: ok but I need to highlight vertial section header as well(row header)
Img 3: ok but I need to style label text color once for all when item containing that widget is selectedthank you
-
no suggestion at all?