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. QTimer Slot - time delta varies depending on Slot-Method content

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 225 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.
  • Flaming MoeF Offline
    Flaming MoeF Offline
    Flaming Moe
    wrote on last edited by Christian Ehrlicher
    #1

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

    A lovely day for a ̶g̶̶u̶̶i̶̶n̶̶n̶̶e̶̶s̶ DUFF^^

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • Flaming MoeF Offline
        Flaming MoeF Offline
        Flaming Moe
        wrote on last edited by
        #3

        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

        A lovely day for a ̶g̶̶u̶̶i̶̶n̶̶n̶̶e̶̶s̶ DUFF^^

        JonBJ Christian EhrlicherC 2 Replies Last reply
        0
        • Flaming MoeF Flaming Moe

          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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • Flaming MoeF Flaming Moe

            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

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Flaming-Moe I fixed your first post.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1

            • Login

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