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. QListWidget update issue

QListWidget update issue

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 710 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.
  • T Offline
    T Offline
    tsdk
    wrote on last edited by
    #1

    Hi,
    I'm trying to make simple layout for visualizing the sorting of vector at every step. For that I'm using a pushbutton which when clicked should sort the given input vector. To visualize this I added a delay in the for loop and also highlighting the item colour in the Widget whose position is changed.

        for (int i = 0; i < (tempVisVector.size() - 1); i++ )
             {
                ui->sortedArray->clear();
                double min = tempVisVector[i];
                unsigned int locat = i ;
    
                for (int j = i+1; j < (tempVisVector.size()); j++)
                {
                    if (min > tempVisVector[j])
                    {
                        min = tempVisVector[j];
                        locat = j;
                    }
                }
    
    
                //Swap the values
                double temp = tempVisVector[i];
                tempVisVector[i] = min;
                tempVisVector[locat] = temp;
    
                for (int p = 0; p < tempVisVector.size(); p++)
                {
                    qDebug()<<"Add Item";
                    ui->sortedArray->addItem(QString::number(tempVisVector[p]));
                }
                ui->sortedArray->item(i)->setBackground(Qt::red);
                ui->sortedArray->item(locat)->setBackground(Qt::green);
                qDebug()<<"Sleeping Time";
                Sleep(2000);
    
              }
    

    The added delay is working but my ListWidget does not get updated. It gets updated only at the end once the entire sorting is done. I'm not sure if the sleep () is obstructing this or not. ANy suggestions?

    JonBJ 1 Reply Last reply
    0
    • T Offline
      T Offline
      tsdk
      wrote on last edited by
      #6

      It is Working now without the use of QTimer. I removed the outer loop of sorting and calling it from on_click pushbutton slot. Thanks Guys!!

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

        @tsdk said in QListWidget update issue:

        ANy suggestions?

        No, it's correct. Qt can only redraw when it's back in the eventloop. Therefore don't block the eventloop with sleep() or similar.

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

        T 1 Reply Last reply
        1
        • T tsdk

          Hi,
          I'm trying to make simple layout for visualizing the sorting of vector at every step. For that I'm using a pushbutton which when clicked should sort the given input vector. To visualize this I added a delay in the for loop and also highlighting the item colour in the Widget whose position is changed.

              for (int i = 0; i < (tempVisVector.size() - 1); i++ )
                   {
                      ui->sortedArray->clear();
                      double min = tempVisVector[i];
                      unsigned int locat = i ;
          
                      for (int j = i+1; j < (tempVisVector.size()); j++)
                      {
                          if (min > tempVisVector[j])
                          {
                              min = tempVisVector[j];
                              locat = j;
                          }
                      }
          
          
                      //Swap the values
                      double temp = tempVisVector[i];
                      tempVisVector[i] = min;
                      tempVisVector[locat] = temp;
          
                      for (int p = 0; p < tempVisVector.size(); p++)
                      {
                          qDebug()<<"Add Item";
                          ui->sortedArray->addItem(QString::number(tempVisVector[p]));
                      }
                      ui->sortedArray->item(i)->setBackground(Qt::red);
                      ui->sortedArray->item(locat)->setBackground(Qt::green);
                      qDebug()<<"Sleeping Time";
                      Sleep(2000);
          
                    }
          

          The added delay is working but my ListWidget does not get updated. It gets updated only at the end once the entire sorting is done. I'm not sure if the sleep () is obstructing this or not. ANy suggestions?

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

          @tsdk
          You absolutely will not want to use sleep(). You will want to use QTimer.

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @tsdk said in QListWidget update issue:

            ANy suggestions?

            No, it's correct. Qt can only redraw when it's back in the eventloop. Therefore don't block the eventloop with sleep() or similar.

            T Offline
            T Offline
            tsdk
            wrote on last edited by
            #4

            @Christian-Ehrlicher So any alternatives to display it otherwise I will have to use TableWidget.
            @JonB Will Try with QTimer.
            Thnx :)

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

              @tsdk said in QListWidget update issue:

              So any alternatives to display it

              Do one step, go back to the eventloop, do the next step. Maybe even with a QTimer so the user sees the changes otherwise it might go too fast.

              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
              3
              • T Offline
                T Offline
                tsdk
                wrote on last edited by
                #6

                It is Working now without the use of QTimer. I removed the outer loop of sorting and calling it from on_click pushbutton slot. Thanks Guys!!

                JonBJ 1 Reply Last reply
                0
                • T tsdk

                  It is Working now without the use of QTimer. I removed the outer loop of sorting and calling it from on_click pushbutton slot. Thanks Guys!!

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

                  @tsdk
                  I don't know how you managed to achieve "visualization" [for the end user] without a QTimer. Sorting steps will complete so quickly that I can't believe the user will see it "animated" in progress, which I thought is what you wanted. But you seem to be happy :)

                  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