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