Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to reset stylesheet file over apllication while the app is running?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mit_cruzeM Offline
    mit_cruzeM Offline
    mit_cruze
    wrote on last edited by
    #1

    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";
      }
    
    
    raven-worxR 1 Reply Last reply
    0
    • mit_cruzeM mit_cruze

      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";
        }
      
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • mit_cruzeM Offline
        mit_cruzeM Offline
        mit_cruze
        wrote on last edited by
        #3

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

        raven-worxR 1 Reply Last reply
        0
        • mit_cruzeM mit_cruze

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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @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?!

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved