PDA

View Full Version : Display and update variable's value in a label.



SalahuddinAsh
3rd September 2017, 22:40
Hi all,

I'm a newbie in QT. I've developed a project in vim. Now I want to display what I used to display in a terminal in a user-friendly way in a GUI.
Simply, I have a set of variables which I need to display in labels in the GUI. I also have couple of variables, whose values should control what should be displayed in the labels.
I knew that the best way to do this is using signals and slots, right?
Unfortunately, most of the signals and slots examples describe how to connect a slider with a progress bar, or a button with a label. But I need to know how to display a variable in my main project in a GUI?
Following is a sample code I can run successfully, and I want to display the variable distance in a label.


#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
int distance = 4;
distance++;

QApplication a(argc, argv);
MainWindow w;
w.setName("Guided");
w.show();

return a.exec();
}

Thanks

admkrk
3rd September 2017, 23:15
Hi,
Most of the time you are not going to be using main.cpp, you want to be doing it in MainWindow.cpp. You do not need signals / slots to set text, unless you are doing something like the examples you looked at. If you added the labels in Creator, you just need to do something like:

ui->myLabel->setText(distance);

SalahuddinAsh
4th September 2017, 10:58
Thanks for your reply.
In my program, the variables' values are updated in the main.cpp file, therefore, I think I should update their values in the GUI also in the main.cpp file, right?
If it should be as you said, how should I send the variables' values from main.cpp to mainwindow.cpp? Do you mean that I define the variables as global? This will not be a safe solution, right?

d_stranz
4th September 2017, 20:06
I think you are having problems because you are trying to reimplement a function-driven program into an event-driven GUI framework.

A function-driven program is usually one where main() calls functions and stores their results into variables defined in main(). An event-driven program doesn't work that way. The main() routine is almost always just a way to get the GUI's event loop started and display the main GUI window. In Qt, this is done through QApplication::exec() and QMainWindow::show(), respectively.

For you, the easiest way is to take all of the variables you define in main() and make them member variables of the MainWindow class. Likewise, take all of the functions that are called from main() that update the variables and make them member functions of the MainWindow class. Move the logic that calls these functions out of main() and into the MainWindow class. Each time you calculate a new value for a variable, you update the GUI labels with the text version of the value.

You will need a way to start the calculations that update the values. You can use a push button, menu action, or some other means to do this manually.

You might take a look at the Qt Calculator example (https://doc.qt.io/qt-5/qtwidgets-widgets-calculator-example.html). SImilar things happen there.

admkrk
4th September 2017, 21:55
If you look at main.cpp in that example, you will see that, except for the comments, it is the same as what you had before you added anything to yours. While these are kind of old, VoidRealms (https://www.youtube.com/watch?v=GxlB34Cn0zw&index=3&list=PL2D1942A4688E9D63) does a pretty good job of giving a basic overview of how it works, and how Creator fits in. Some people do not like videos, but I find these videos helpful for a quick overview when using a widget for the first time. Also, it is a quicker way for me to find out the right one to use, as opposed to reading through the docs (I read slow).