Results 1 to 9 of 9

Thread: Best way to handle repetitive code that updates GUI elements

  1. #1
    Join Date
    Dec 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Best way to handle repetitive code that updates GUI elements

    I'd like to have the code that appends the dat/time to the lable executed when each of the GUI elements is clicked.

    How do I implement the "AddCurrentDateTime" function so that I can call that line of code whenever necessary?

    Obviously the code below generated the error:

    /home/wfdwfd/function/mainwindow.cpp:6: error: ‘ui’ was not declared in this scope
    ui->label->setText(ui->label->text() + " " + QDateTime::currentDateTime().toString("yyyy MMM/dd h:mm:ss a t"));
    ^~

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "QDateTime"
    4.  
    5. void AddCurrentDateTime() {
    6. ui->label->setText(ui->label->text() + " " + QDateTime::currentDateTime().toString("yyyy MMM/dd h:mm:ss a t"));
    7. }
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14.  
    15. ui->label->setText("Waiting on you Kiddo...so click something.");
    16.  
    17. }
    18.  
    19. MainWindow::~MainWindow()
    20. {
    21. delete ui;
    22. }
    23.  
    24. void MainWindow::on_pushButton_clicked()
    25. {
    26. ui->label->setText("Push Button");
    27. AddCurrentDateTime();
    28. }
    29.  
    30. void MainWindow::on_radioButton_clicked()
    31. {
    32. ui->label->setText("Radio Button");
    33. AddCurrentDateTime();
    34. }
    35.  
    36. void MainWindow::on_checkBox_stateChanged(int arg1)
    37. {
    38. ui->label->setText("Checkbox");
    39. AddCurrentDateTime();
    40. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Best way to handle repetitive code that updates GUI elements

    Use QTimer to create a timer that expires every second. Connect the QTimer::timeout signal to a slot in your code that gets the current date/time, etc.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Best way to handle repetitive code that updates GUI elements

    AddCurrentDateTime should be a method of MainWindow not function.

  4. #4
    Join Date
    Dec 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Best way to handle repetitive code that updates GUI elements

    Quote Originally Posted by jefftee View Post
    Use QTimer to create a timer that expires every second. Connect the QTimer::timeout signal to a slot in your code that gets the current date/time, etc.
    I only want the GUI updated when a GUI button is clicked. I don't want it refreshed automatically.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Best way to handle repetitive code that updates GUI elements

    Lesiok gave you the answer.
    <=== 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.

  6. #6
    Join Date
    Dec 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Best way to handle repetitive code that updates GUI elements

    Quote Originally Posted by Lesiok View Post
    AddCurrentDateTime should be a method of MainWindow not function.
    Thanks guys. I'm half way there. How do I code this as a method?

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Best way to handle repetitive code that updates GUI elements

    How do I code this as a method?
    You really can't be serious, are you? Asking how you add a member function to a C++ class? There is no magic here. Just because MainWindow is derived from QMainWindow it is still a C++ class. You can add anything you want to it as either a member variable or method.
    <=== 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.

  8. #8
    Join Date
    Dec 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Best way to handle repetitive code that updates GUI elements

    Quote Originally Posted by d_stranz View Post
    You really can't be serious, are you? Asking how you add a member function to a C++ class? There is no magic here. Just because MainWindow is derived from QMainWindow it is still a C++ class. You can add anything you want to it as either a member variable or method.
    Sorry! I really thought that this was the Newbie section. I won't be visiting this forum any longer.

    Bye.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Best way to handle repetitive code that updates GUI elements

    It is the newbie section, but for newbies in Qt development. If you are going to do anything at all with Qt, you have to at least understand how to write code in C++. The forum is here to give help in solving problems with your Qt code, not to teach the basics of C++ programming.

    If you need someone to give you an example of how to add a member function to a C++ class, then you really need to go back and hit the C++ textbooks.
    <=== 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.

Similar Threads

  1. QML ChartView updates
    By scgrant327 in forum Qt Programming
    Replies: 13
    Last Post: 23rd May 2017, 00:04
  2. installing updates
    By uj in forum Installation and Deployment
    Replies: 3
    Last Post: 13th July 2012, 08:15
  3. QMenuBar Updates
    By johnmauer in forum Qt Programming
    Replies: 5
    Last Post: 20th January 2010, 13:35
  4. How to put custom handle image in QSlider using code?
    By montylee in forum Qt Programming
    Replies: 6
    Last Post: 29th January 2009, 19:38
  5. Painting updates
    By QbelcorT in forum Qt for Embedded and Mobile
    Replies: 6
    Last Post: 29th September 2008, 00:48

Bookmarks

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.