Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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?


  • Moderators

    @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.



  • @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.



  • 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



  • @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.


Log in to reply