How to reset stylesheet file over apllication while the app is running?



  • main.cpp

    int main(int argc, char *argv[]){
    QApplication app(argc, argv);
    
    QFile data("://stylesheet.qss");  // this is a stylesheet file in images.qrc file
        QString style;
        if (data.open(QFile::ReadOnly))
        {
            QTextStream styleIn(&data);
            style = styleIn.readAll();
            data.close();
            app.setStyleSheet(style);  // I want this line to restart with different style-sheet file 
        }
    app.setWindowIcon(QIcon("://images/BEL.jpg"));
    
    MainWindow w;
    w.setFixedSize(500,350);
    w.showMaximized();
    return app.exec();}
    

    theme.cpp

    theme::theme(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::theme) {    
    ui->setupUi(this);
    ui->apply_button->setDefault(1);
    QButtonGroup *radio=new QButtonGroup;
    radio->setExclusive(true);
    radio->addButton(ui->radio_blue,2);
    radio->addButton(ui->radio_grey,3);
    connect(radio,SIGNAL(buttonClicked(int)),this,SLOT(function(int)));}
    theme::~theme()
    {
    delete ui;
     }
     void theme::on_pushButton_2_clicked()
     {
     this->close();
     }
     void theme::function(int value)
      {  
        if(value==2)
      {
        qDebug()<<"blue";
      }
        else if(value==3)
      {
        qDebug()<<"green";
       }
     else
        qDebug()<< "error";
      }
    
    

  • Moderators

    @mit_cruze
    Setting the stylesheet to the mainwindow will make thing a lot easier, since the stylesheet will then be propagated to all children whenever it changes, and thus they all get polished.
    If you set it to the QApplication you will have to listen to QEvent::StyleChange events on the QApplication and do the polishing of the widgets yourself or at least forward it to your top level widgets to get the same effect.

    qApp->installEventFilter(this);
    ...
    bool eventFilter( QObject* watched, QEvent* event )
    {
          if( watched == qApp && event->type() == QEvent::StyleChange )
          {
                 foreach( QWidget* topLevelWidget, qApp->topLevelWidgets() )
                      topLevelWidget->setStyle( qApp->style() );
          }
          return BaseClass::eventFilter( watched, event );
    }
    


  • Where do I put this event definition?
    How would event definition come to know that style-sheet changed?
    I am confused.


  • Moderators

    @mit_cruze said in How to reset stylesheet file over apllication while the app is running?:

    Where do I put this event definition?

    What event definition?!?

    How would event definition come to know that style-sheet changed?

    i just gave you an example using an event filter?!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.