PDA

View Full Version : Best way to handle repetitive code that updates GUI elements



KairiTech
8th December 2016, 03:04
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"));
^~



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDateTime"

void AddCurrentDateTime() {
ui->label->setText(ui->label->text() + " " + QDateTime::currentDateTime().toString("yyyy MMM/dd h:mm:ss a t"));
}

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

ui->label->setText("Waiting on you Kiddo...so click something.");

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
ui->label->setText("Push Button");
AddCurrentDateTime();
}

void MainWindow::on_radioButton_clicked()
{
ui->label->setText("Radio Button");
AddCurrentDateTime();
}

void MainWindow::on_checkBox_stateChanged(int arg1)
{
ui->label->setText("Checkbox");
AddCurrentDateTime();
}

jefftee
8th December 2016, 05:43
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.

Lesiok
8th December 2016, 06:08
AddCurrentDateTime should be a method of MainWindow not function.

KairiTech
8th December 2016, 11:35
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.

d_stranz
8th December 2016, 16:20
Lesiok gave you the answer.

KairiTech
8th December 2016, 17:21
AddCurrentDateTime should be a method of MainWindow not function.

Thanks guys. I'm half way there. How do I code this as a method?

d_stranz
8th December 2016, 20:12
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.

KairiTech
8th December 2016, 21:22
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.

d_stranz
8th December 2016, 23:03
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.