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

QTimer Slot - time delta varies depending on Slot-Method content



  • Hello there,

    I´m making a quick and dirty Neo-Pixel sim where I have a QHLayout on the Widget, placew some widgets in in the Layout as representing the pixels.
    Then there is a method, which translates the lower 24bit of a uint32_t to RGB values for creating a stylesheet which is passed with indivisual color values to the widgets in the layout.

    Currently I just use a rainbow effect based on the approach of deviding a 256 step color circle in 3 segments (like the Arduin-Project does it, which is the later destination hardware for created effects):
    This is ony a "getting started" effect. So a "rainbow gradient is not what I´m loking for.
    For the fading effect I use a QTimer timeout slot "void Widget::SltTimerEl()"```
    code_text

    What I observe is, depending how many HLayouts with thier corresponding widgets I treat in the Slot the time delta´s kind of scale with the amount of HLayouts.
    I measured that with retrieving by QTime.msec and calculating the differences.

    Does the repainting of the widgets take so long, that it blocks the slot?

    What would be a more clever way to do this?

    #include "widget.h"
    #include "ui_widget.h"
    #include <QDebug>
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
       pixel* p = new pixel;
        p ->SetColor(4198520);
        //ui->horizontalLayout->addWidget(p);
        for(uint32_t i=0; i<10; i++)
        {
            QWidget* w = new QWidget;
            ui->horizontalLayout->addWidget(w);
            pixelList.append(w);
        }
    
    
        for(uint32_t i=0; i<50; i++)
        {
            QWidget* w = new QWidget;
            ui->horizontalLayout_2->addWidget(w);
            pixelList2.append(w);
        }
    
    
        for(uint32_t i=0; i<100; i++)
        {
            QWidget* w = new QWidget;
            ui->horizontalLayout_3->addWidget(w);
            pixelList3.append(w);
        }
    
        //connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(SltShow()));
        connect(&timer, SIGNAL(timeout()), this, SLOT(SltTimerEl()));
    
        timer.setInterval(20);
        timer.start();
        time.start();
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::SltShow()
    {
        SltTimerEl();
    }
    
    void Widget::SetColor(QWidget* w, uint32_t col)
    {
        QString stSheet;
        unsigned int red, green, blue;
        red = (col & 16711680) >> 16;
        green = (col & 65280) >> 8;
        blue = col & 255;
        //qDebug() << "  red: " << red<< "  green: " << green<< "  blue: " << blue;
        stSheet.append("background-color: rgb(");
        stSheet.append(QString::number(red));
        stSheet.append(',');
        stSheet.append(QString::number(green));
        stSheet.append(',');
        stSheet.append(QString::number(blue));
        stSheet.append(");");
        w->setStyleSheet(stSheet);
    
        //qDebug() << stSheet;
    }
    
    void Widget::Rainbow()
    {
    
    }
    
    void Widget::SltTimerEl()
    {
        static uint32_t j = 0;
        static uint32_t k = 0;
        static uint32_t l = 0;
    
        static int sameCntr = 0, lastRes = 0, curRes;
        static uint32_t oldTime, newTime, timeDiff;
    
        newTime = time.msec();
        time.start();
        if(newTime >= oldTime)
        {
            timeDiff = newTime-oldTime;
        }
        else
        {
            timeDiff = 999-oldTime + newTime;
        }
        qDebug() << " newTime " << newTime << " timeDiff " << timeDiff;
        oldTime = newTime;
    
    if(ui->pushButton->isDown())
    {
        for(uint32_t i=0; i< pixelList.length(); i++)
          {
              SetColor(pixelList.at(i), Wheel(((i * 256 / (pixelList.length()))//+ j) & 255));
                                               + (j * 256 / (100)) )& 255));
              curRes = (j * 256 / (100));
              if(curRes != lastRes)
              {
                  newTime = time.msec();
                  time.start();
                  if(newTime >= oldTime)
                  {
                      timeDiff = newTime-oldTime;
                  }
                  else
                  {
                      timeDiff = 999-oldTime + newTime;
                  }
                  //qDebug() << sameCntr << "  " << lastRes << " newTime " << newTime << " timeDiff " << timeDiff;
                  sameCntr = 0;
              }
              else
              {
                 sameCntr++;
              }
              lastRes = curRes;
    
                                              //+(j * 256 / (pixelList.length())) & 255));
              pixelList.at(i)->repaint();
           }
    /**/
        for(uint32_t i=0; i< pixelList2.length(); i++)
          {
              SetColor(pixelList2.at(i), Wheel(((i * 256 / (pixelList2.length()))//+ j) & 255));
                                               + (k * 256 / (100)) )& 255));
                                              //+(j * 256 / (pixelList.length())) & 255));
              pixelList2.at(i)->repaint();
           }
    
    
        for(uint32_t i=0; i< pixelList3.length(); i++)
          {
              SetColor(pixelList3.at(i), Wheel(((i * 256 / (pixelList3.length()))//+ j) & 255));
                                               + (l * 256 / (100)) )& 255));
                                              //+(j * 256 / (pixelList.length())) & 255));
              pixelList3.at(i)->repaint();
           }
    
        j++;
        if(j >= 100) j = 0;
    
        k++;
        if(k >= 100) k = 0;
    
        l++;
        if(l >= 100) l = 0;
    }
    }
    
    uint32_t Widget::Wheel(uint32_t WheelPos)
    {
        uint32_t r, g, b, ret;
      if(WheelPos < 85)
      {
          r = WheelPos * 3;
          g = 255 - WheelPos * 3;
          b = 0;
    
         return ret = (r<<16) + (g<<8) +b;
      }
      else if(WheelPos < 170)
      {
          WheelPos -= 85;
          b = WheelPos * 3;
          r = 255 - WheelPos * 3;
          g = 0;
    
            return ret = (r<<16) + (g<<8) +b;
        }
        else
        {
          WheelPos -= 170;
          g = WheelPos * 3;
          b = 255 - WheelPos * 3;
          r = 0;
    
            return ret = (r<<16) + (g<<8) +b;
        }
    }
    
    

  • Lifetime Qt Champion

    Can you please format your post so that it's readable?
    20 ms is a very short time, it's the minimum time for normal windows timers so yes, the timeout can vary. Doing such stuff with stylesheets is also not the best way - override QWidget::paintEvent() and paint the background there.



  • yes... Saw the unformatted code - tryed to edit, but there it is displayed formated.
    Perhaps I have to try a different browser or control-signs



  • @Flaming-Moe
    Put a line with just ``` above your code and the same line again below your code.

    Outside of this, where your

    What I observe is,
    

    paragraphs are, do not allow lines to start with 4 or more spaces, which I believe is what you have now.


  • Lifetime Qt Champion

    @Flaming-Moe I fixed your first post.


Log in to reply