#include #include #include #include #include #include class MyDialog : public QDialog { public: MyDialog() { QVBoxLayout * layout = new QVBoxLayout; QPushButton * btnChangeBackground = new QPushButton( "change background" ); layout->addWidget( btnChangeBackground ); connect( btnChangeBackground, &QPushButton::clicked, this, &MyDialog::changeBackgroundSlot ); layout->addWidget( &m_tableView ); m_tableView.setModel( new QDirModel() ); setLayout( layout ); // comment this to make changeBackgroundSlot working correctly m_tableView.setStyleSheet( "QTableView { gridline-color: red }" ); // } void changeBackgroundSlot() { QPalette currentPalette; currentPalette = m_tableView.palette(); // trying to invert all ColorRoles, but without expected effect for ( int i = 0; i < QPalette::NColorRoles; ++i ) { currentPalette.setColor( ( QPalette::ColorRole )i , invertColor( currentPalette.color( ( QPalette::ColorRole )i ) ) ); } // m_tableView.setPalette( currentPalette ); } QColor invertColor( QColor const & _color ) const { return QColor( 255 - _color.red(), 255 - _color.green(), 255 - _color.blue() ); } private: QTableView m_tableView; }; int main( int _argc, char ** _argv ) { QApplication app( _argc, _argv ); MyDialog dlg; dlg.show(); return app.exec(); }