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. Could not change stylesheet of push button in "for" function
Forum Update on Monday, May 27th 2025

Could not change stylesheet of push button in "for" function

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 554 Views
  • 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.
  • V Offline
    V Offline
    victor wang
    wrote on 14 Feb 2019, 07:56 last edited by victor wang
    #1

    Hi All,
    Below is my code.

    for(i=0;i<DO_num;i++)
        {
            qDebug("\ndido routine!");
            QThread::sleep(1);
            qDebug()<<"do"+QString::number(i)+" set high";
            Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DO_set_value_high.at(i));
            temp=i;
            if(temp!=0)
            {
                for(a=0;a<temp;a++)
                {
                    qDebug()<<"do"+QString::number(a)+" set low";
                    Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DO_set_value_low.at(a));
                }
            }
            for(a=temp+1;a<DO_num;a++)
            {
                qDebug()<<"Second do"+QString::number(a)+" set low";
                Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DO_set_value_low.at(a));
                Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(0),&get);
                qDebug()<<"\nZero di0="+get;
                Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(1),&get);
                qDebug()<<"First di1="+get;
                Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(2),&get);
                qDebug()<<"Second di2="+get;
                Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(3),&get);
                qDebug()<<"Third di3="+get;
            }
            for(int h=0;h<DI_num;h++)
            {
                QThread::sleep(2);
                Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(h),&get);
                qDebug()<<"di"+QString::number(h)+"="+get;
                if(h==i)
                {
                    if(get == "1\n")
                    {
                        qDebug("h=i is 1!");
                        switch(h) {
                        case 0:
                            qDebug("0 set green");
                            ui->pushButtonPT_LED1->setStyleSheet("QPushButton{background-color:green;}");
                            break;
                        case 1:
                            qDebug("1 set green");
                            ui->pushButtonPT_LED2->setStyleSheet("QPushButton{background-color:green;}");
                            break;
                        case 2:
                            qDebug("2 set green");
                            ui->pushButtonPT_LED3->setStyleSheet("QPushButton{background-color:green;}");
                            break;
                        case 3:
                            qDebug("3 set green");
                            ui->pushButtonPT_LED4->setStyleSheet("QPushButton{background-color:green;}");
                            break;
                        }
                        result[h]=true;
                    }
                    else
                    {
                        qDebug("h=i is 0!");
                        result[h]=false;
                        err_flag = true;
                    }
                }
                else
                {
                    if(get == "0\n")
                    {
                        qDebug("h!=i is 0!");
                        /*switch(h) {
                        case 0:
                            ui->pushButtonPT_LED1->setStyleSheet("QPushButton{background-color:red;}");
                            break;
                        case 1:
                            ui->pushButtonPT_LED2->setStyleSheet("QPushButton{background-color:red;}");
                            break;
                        case 2:
                            ui->pushButtonPT_LED3->setStyleSheet("QPushButton{background-color:red;}");
                            break;
                        case 3:
                            ui->pushButtonPT_LED4->setStyleSheet("QPushButton{background-color:red;}");
                            break;
                        }*/
                        result[h]=true;
                    }
                    else
                    {
                        qDebug("h!=i is 1!");
                        result[h]=false;
                        err_flag = true;
                    }
                }
            }
        }
    

    I try to change my stylesheet color to green.
    But it did not change while in the "for" loop.
    It will only show up when out of for(i=0;i<DO_num;i++).

    I want to change the color every time when I set it.

    How can I solve this issue?

    J M 2 Replies Last reply 14 Feb 2019, 08:27
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 14 Feb 2019, 08:21 last edited by
      #2

      @victor-wang said in Could not change stylesheet of push button in "for" function:

      How can I solve this issue?

      A simple but bad solution is to use qApp->processEvents(); just after you switch the style sheets.

      A better solution would be to run that awfully synchronous code you have in a separate thread, so it won't block GUI's event loop. You can then easily use signals and slots to tell the GUI to update.

      (Z(:^

      1 Reply Last reply
      4
      • V victor wang
        14 Feb 2019, 07:56

        Hi All,
        Below is my code.

        for(i=0;i<DO_num;i++)
            {
                qDebug("\ndido routine!");
                QThread::sleep(1);
                qDebug()<<"do"+QString::number(i)+" set high";
                Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DO_set_value_high.at(i));
                temp=i;
                if(temp!=0)
                {
                    for(a=0;a<temp;a++)
                    {
                        qDebug()<<"do"+QString::number(a)+" set low";
                        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DO_set_value_low.at(a));
                    }
                }
                for(a=temp+1;a<DO_num;a++)
                {
                    qDebug()<<"Second do"+QString::number(a)+" set low";
                    Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DO_set_value_low.at(a));
                    Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(0),&get);
                    qDebug()<<"\nZero di0="+get;
                    Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(1),&get);
                    qDebug()<<"First di1="+get;
                    Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(2),&get);
                    qDebug()<<"Second di2="+get;
                    Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(3),&get);
                    qDebug()<<"Third di3="+get;
                }
                for(int h=0;h<DI_num;h++)
                {
                    QThread::sleep(2);
                    Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(h),&get);
                    qDebug()<<"di"+QString::number(h)+"="+get;
                    if(h==i)
                    {
                        if(get == "1\n")
                        {
                            qDebug("h=i is 1!");
                            switch(h) {
                            case 0:
                                qDebug("0 set green");
                                ui->pushButtonPT_LED1->setStyleSheet("QPushButton{background-color:green;}");
                                break;
                            case 1:
                                qDebug("1 set green");
                                ui->pushButtonPT_LED2->setStyleSheet("QPushButton{background-color:green;}");
                                break;
                            case 2:
                                qDebug("2 set green");
                                ui->pushButtonPT_LED3->setStyleSheet("QPushButton{background-color:green;}");
                                break;
                            case 3:
                                qDebug("3 set green");
                                ui->pushButtonPT_LED4->setStyleSheet("QPushButton{background-color:green;}");
                                break;
                            }
                            result[h]=true;
                        }
                        else
                        {
                            qDebug("h=i is 0!");
                            result[h]=false;
                            err_flag = true;
                        }
                    }
                    else
                    {
                        if(get == "0\n")
                        {
                            qDebug("h!=i is 0!");
                            /*switch(h) {
                            case 0:
                                ui->pushButtonPT_LED1->setStyleSheet("QPushButton{background-color:red;}");
                                break;
                            case 1:
                                ui->pushButtonPT_LED2->setStyleSheet("QPushButton{background-color:red;}");
                                break;
                            case 2:
                                ui->pushButtonPT_LED3->setStyleSheet("QPushButton{background-color:red;}");
                                break;
                            case 3:
                                ui->pushButtonPT_LED4->setStyleSheet("QPushButton{background-color:red;}");
                                break;
                            }*/
                            result[h]=true;
                        }
                        else
                        {
                            qDebug("h!=i is 1!");
                            result[h]=false;
                            err_flag = true;
                        }
                    }
                }
            }
        

        I try to change my stylesheet color to green.
        But it did not change while in the "for" loop.
        It will only show up when out of for(i=0;i<DO_num;i++).

        I want to change the color every time when I set it.

        How can I solve this issue?

        J Offline
        J Offline
        JonB
        wrote on 14 Feb 2019, 08:27 last edited by JonB
        #3

        @victor-wang
        I imagine that changes do not become visible to the user while you are inside your own executing code. They would become visible the next time you allow the main event loop to execute. See https://doc.qt.io/qt-5/qwidget.html#update. Try https://doc.qt.io/qt-5/qwidget.html#repaint to force an immediate if you do not want to wait.

        As @sierdzio has just posted, you may well be better off anyway moving your synchronous code to a thread, or at least using the non-blocking calls in QProcess to allow the UI to run. And btw I don't know why you are calling QThread::sleep(1);, but if you're expecting the UI to run/update during that sleep it won't.

        1 Reply Last reply
        1
        • V victor wang
          14 Feb 2019, 07:56

          Hi All,
          Below is my code.

          for(i=0;i<DO_num;i++)
              {
                  qDebug("\ndido routine!");
                  QThread::sleep(1);
                  qDebug()<<"do"+QString::number(i)+" set high";
                  Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DO_set_value_high.at(i));
                  temp=i;
                  if(temp!=0)
                  {
                      for(a=0;a<temp;a++)
                      {
                          qDebug()<<"do"+QString::number(a)+" set low";
                          Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DO_set_value_low.at(a));
                      }
                  }
                  for(a=temp+1;a<DO_num;a++)
                  {
                      qDebug()<<"Second do"+QString::number(a)+" set low";
                      Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DO_set_value_low.at(a));
                      Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(0),&get);
                      qDebug()<<"\nZero di0="+get;
                      Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(1),&get);
                      qDebug()<<"First di1="+get;
                      Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(2),&get);
                      qDebug()<<"Second di2="+get;
                      Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(3),&get);
                      qDebug()<<"Third di3="+get;
                  }
                  for(int h=0;h<DI_num;h++)
                  {
                      QThread::sleep(2);
                      Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<DI_get_value.at(h),&get);
                      qDebug()<<"di"+QString::number(h)+"="+get;
                      if(h==i)
                      {
                          if(get == "1\n")
                          {
                              qDebug("h=i is 1!");
                              switch(h) {
                              case 0:
                                  qDebug("0 set green");
                                  ui->pushButtonPT_LED1->setStyleSheet("QPushButton{background-color:green;}");
                                  break;
                              case 1:
                                  qDebug("1 set green");
                                  ui->pushButtonPT_LED2->setStyleSheet("QPushButton{background-color:green;}");
                                  break;
                              case 2:
                                  qDebug("2 set green");
                                  ui->pushButtonPT_LED3->setStyleSheet("QPushButton{background-color:green;}");
                                  break;
                              case 3:
                                  qDebug("3 set green");
                                  ui->pushButtonPT_LED4->setStyleSheet("QPushButton{background-color:green;}");
                                  break;
                              }
                              result[h]=true;
                          }
                          else
                          {
                              qDebug("h=i is 0!");
                              result[h]=false;
                              err_flag = true;
                          }
                      }
                      else
                      {
                          if(get == "0\n")
                          {
                              qDebug("h!=i is 0!");
                              /*switch(h) {
                              case 0:
                                  ui->pushButtonPT_LED1->setStyleSheet("QPushButton{background-color:red;}");
                                  break;
                              case 1:
                                  ui->pushButtonPT_LED2->setStyleSheet("QPushButton{background-color:red;}");
                                  break;
                              case 2:
                                  ui->pushButtonPT_LED3->setStyleSheet("QPushButton{background-color:red;}");
                                  break;
                              case 3:
                                  ui->pushButtonPT_LED4->setStyleSheet("QPushButton{background-color:red;}");
                                  break;
                              }*/
                              result[h]=true;
                          }
                          else
                          {
                              qDebug("h!=i is 1!");
                              result[h]=false;
                              err_flag = true;
                          }
                      }
                  }
              }
          

          I try to change my stylesheet color to green.
          But it did not change while in the "for" loop.
          It will only show up when out of for(i=0;i<DO_num;i++).

          I want to change the color every time when I set it.

          How can I solve this issue?

          M Offline
          M Offline
          mchinand
          wrote on 14 Feb 2019, 17:14 last edited by
          #4

          Try unpolishing/polishing the widget after updating the stylesheet. I also call update() on the widget, but reading the docs now, I think this step is unnecessary.

          Wiki: Dynamic Stylesheet

          J 1 Reply Last reply 14 Feb 2019, 18:32
          0
          • M mchinand
            14 Feb 2019, 17:14

            Try unpolishing/polishing the widget after updating the stylesheet. I also call update() on the widget, but reading the docs now, I think this step is unnecessary.

            Wiki: Dynamic Stylesheet

            J Offline
            J Offline
            JonB
            wrote on 14 Feb 2019, 18:32 last edited by JonB
            #5

            @mchinand
            My understanding/experience:

            Polishing is only required when dynamic properties are referenced in the stylesheet. In my own code this is the case. That is not the case here.

            update() just marks for/queues a refresh for the next time the event loop runs to paint. This user is delayed getting there so it won't solve his problem. The docs link was for explanation. It's repaint() which needs to be called for force redraw now.

            1 Reply Last reply
            0

            1/5

            14 Feb 2019, 07:56

            • Login

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