Results 1 to 5 of 5

Thread: Display and update variable's value in a label.

  1. #1

    Default Display and update variable's value in a label.

    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.

    Qt Code:
    1. #include <QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. int distance = 4;
    7. distance++;
    8.  
    9. QApplication a(argc, argv);
    10. MainWindow w;
    11. w.setName("Guided");
    12. w.show();
    13.  
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    Thanks

  2. #2
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Display and update variable's value in a label.

    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:
    Qt Code:
    1. ui->myLabel->setText(distance);
    To copy to clipboard, switch view to plain text mode 

  3. #3

    Default Re: Display and update variable's value in a label.

    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?

  4. #4
    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: Display and update variable's value in a label.

    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. SImilar things happen there.
    <=== 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.

  5. #5
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Display and update variable's value in a label.

    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 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).

Similar Threads

  1. QT label update. Different threads.
    By hagor in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2017, 23:09
  2. Replies: 1
    Last Post: 1st February 2017, 09:52
  3. unable to update label from another thread
    By dhanya.v in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2012, 13:05
  4. Qwt display coordinates label
    By Jean-Luc in forum Qwt
    Replies: 2
    Last Post: 13th February 2012, 11:08
  5. Replies: 2
    Last Post: 8th April 2010, 17:16

Tags for this Thread

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.