Results 1 to 3 of 3

Thread: Display arrray values in QLCDNumber

  1. #1
    Join Date
    Mar 2019
    Qt products
    Qt5 Qt/Embedded
    Unix/X11 Windows

    Default Display arrray values in QLCDNumber

    I am working on a math trainer project. So, I must display int values from the array to QLCDNumber with delay. I used QTimer::singleshot but it delays only before the first number. Here is my code. What is the problem?
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QLCDNumber>
    4. #include <QTimer>
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. }
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    18. void MainWindow::on_pushButton_clicked()
    19. {
    20. int arr[] = {100,2,21,12};
    21. for (int i = 0;i<4;i++) {
    22. QTimer::singleShot(2000,this,[=]{
    23. ui->lcdNumber->display(arr[i]);
    24. });
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    I can see only 12.photo_2019-03-25_22-41-10.jpg

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 808 Times in 797 Posts
    Qt products
    Windows Android

    Default Re: Display arrray values in QLCDNumber

    Your on_pushbutton_clicked() slot fires four single shot timers in very quick succession, so essentially all of them execute their timeout events at the same time (maybe a fraction of a millisecond apart). Once the for() loop completes and the clicked slot exits, these four timeouts execute too fast for you to see, so the only number visible is the last one. You need to refactor the application so that only one timer is running, and when it times out, it starts a new timer to display the next number.

    Your mistake is in thinking QTimer::singleShot() acts as a delay. It doesn't - all it does is schedule something to happen later, then exits.

    One way to refactor might be to make the QTimer a member of the MainWindow class (instead of a single-shot). Also make the number array a member of MainWindow, and add a "current index" variable. I would turn the lambda into an actual slot in the MainWindow class.

    The clicked slot needs to: 1) set the current index to 0, 2) disable the button, and 3) start the QTimer
    The timeout slot needs to: 1) display the number at the current index, 2) increment the index, and 3) if the index == array size, enable the button else start the QTimer again.

    With this refactoring, all the clicked slot does it start the first timer and exit, so the UI stays alive. To prevent the user from clicking the button while the display is stepping through the array, the button is disabled until the end of the array is reached, at which point it is enabled and can be clicked again to start over.
    Last edited by d_stranz; 25th March 2019 at 21:17.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2006
    Graz, Austria
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Unix/X11 Windows

    Default Re: Display arrray values in QLCDNumber

    Another recommendation: don't use the "connect by name" feature to connect signals of widgets in the designer form to slots.

    Use actual, explicit connect() calls.

    Gives you much more robust and maintainable code


Similar Threads

  1. Display time in 12hr format using QLcdNumber
    By sattu in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2013, 08:16
  2. Replies: 14
    Last Post: 7th December 2011, 03:57
  3. How to use QLCDnumber to display negative numbers?
    By grissiom in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2010, 06:23
  4. QLCDNumber display
    By jhearon in forum Qt Programming
    Replies: 0
    Last Post: 26th October 2008, 23:53
  5. Need to make QLCDNumber display count up/down
    By JimDaniel in forum Newbie
    Replies: 2
    Last Post: 29th September 2007, 01:59

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.