Results 1 to 7 of 7

Thread: How to actually connect variables to the GUI ?

  1. #1
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to actually connect variables to the GUI ?

    Hi,
    I've just started using Qt.

    What I haven't found out yet is how I can display a variable, e.g. a text, in a GUI element. How do I find the proper GUI element to display a certain kind of data and how do I make the connection ?
    Also, can I make a displayed text variable look like a fixed text instead of an editable string ?
    Will the GUI automatically update the displayed value if I change it elsewhere in the code or do I need to call a special update routine ?

    Specific answers are appreciated, but I'm happy if you could just direct me to a chapter to read or keyword to look for.

  2. #2
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to actually connect variables to the GUI ?

    Look here. Of particular interest to you, judging by the question, would be signals and slots but I encourage you to read all of it.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to actually connect variables to the GUI ?

    The official tutorials can help, signals and slots are very easy after you get use to the concept, so practice and when you are stuck just ask
    The not editable (by user) content you can use QLabel to display it.

    Also in a book the information is presented in an particular order that introduce new concepts only when you already learn what you need to understand them, so i really recommend reading a book, Foundation of Qt development written by Johan Thelin is a very good introduction to Qt and it has also some advanced topics like unit-testing, networking, threads, etc.

  4. #4
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to actually connect variables to the GUI ?

    Well, I've read the signals and slots chapter, but I'm still lost.
    I'm sure that reading the entire tutorial will slowly give me a better understanding of Qt. But shouldn't a simple thing be just as simple to learn ?

    For example, I have a simple test program with just a push button and I want to make it so that when the button is pressed it's label changes.

    Now I've tried to write a slot function in the corresponding .cpp file (just the standard file you get when creating a new C++ GUI application), like this:

    void MainWindow:: on_button_clicked()
    {
    }

    The function is properly registered as a slot and I checked that it is actually called.
    So, what do I have to put in there for the respective push button that sent the signal to change its label ?

    I looked up that the QPushButton class inherits a setText() method for the purpose of setting its label. But I can't seem to be able to get a reference to the button in order to call it's member function. The GUI is automatically designed by Qt Creator and, though I see the QPushButton object in the respective "ui_mainwindow.h" if I specifically open it, I have no idea how to get a reference to it at the point where I defined the slot and want to change its text.

    Then I found in the signal and slot chapter that there is the QObject sender() function.
    So I queried the current QObject (the main window) for the sender of the signal and tried to change its Text, like this:

    this->sender()->setText("Hi");

    But the problem seems to be that the returned reference is a generic QObject and not QPushButton, so there is no function "setText" to do that.

    I'm sure there must a simple way to do what I try to do. I just don't see it.
    Could someone please give me a simple hint on how to do that and why my approach didn't work ?



    Also, I have another question about signals and slots:

    In the chapter about signals and slots (http://doc.trolltech.com/latest/signalsandslots.html) it says:

    "Signals are automatically generated by the moc and must not be implemented in the .cpp file."

    But why does it seem then like that is done in the very example above (class Counter) where a signal function is defined ?

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to actually connect variables to the GUI ?

    Quote Originally Posted by Computer Hater View Post
    Well, I've read the signals and slots chapter, but I'm still lost.
    I'm sure that reading the entire tutorial will slowly give me a better understanding of Qt. But shouldn't a simple thing be just as simple to learn ?
    That's a sure way to not get help. You show that you're lazy.

    Try this in your slot:
    Qt Code:
    1. ui->yourPushButton->setText("Hi");
    To copy to clipboard, switch view to plain text mode 
    ui is the object pointing to the generated code by uic.
    yourPushButton is the button object you want to change the text of. I assume it is a member of the ui object (which means, you added the button via designer).

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to actually connect variables to the GUI ?

    The code depends on what method you used to integrate the .ui file in C++ code, there are many ways to achieve that (i personally prefer to use a private pointer to the ui class, that is because the code is better encapsulated that way, and disadvantage is that you most likely will need to code more signals and slots to keep that pointer private )
    So the code can be:
    Qt Code:
    1. void MainWindow:: on_button_clicked()
    2. {
    3. // this depends on how you integrate the .ui file
    4. // example if ui is a pointer
    5. ui->YourButtonName->setText("Hello World!");
    6. }
    To copy to clipboard, switch view to plain text mode 
    And one more thing, i really advise you ignore the feature called auto-connect (the one based on naming the slots like this on_ObjectName_SignalName() ) at least at the beginning learn how and when to do the connections and when you are comfortable with signal/slots "mechanism", decide if you will use auto-connect or not.

    LE: The thing with the signal is that you will not implement the signal, you just declare the signals something like this signals: void mySignal(int x); and you don't write something like void signal(int x) {//... code; }

    You just "call" the signal using: emit mySignal(10);
    Last edited by Zlatomir; 14th November 2010 at 21:23.

  7. #7
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to actually connect variables to the GUI ?

    Okay, thank you both for the fast responses. It works now.
    I still have to figure out exactly why though.

    Quote Originally Posted by tbscope View Post
    That's a sure way to not get help. You show that you're lazy.
    It showed that I was lost and didn't know where to look.
    But I will keep it in mind.

    Quote Originally Posted by Zlatomir View Post
    LE: The thing with the signal is that you will not implement the signal, you just declare the signals something like this signals: void mySignal(int x); and you don't write something like void signal(int x) {//... code; }

    You just "call" the signal using: emit mySignal(10);
    Ah ! Right ! That makes sense.
    Didn't think of that.

Similar Threads

  1. Casting variables
    By axisdj in forum Newbie
    Replies: 1
    Last Post: 6th September 2010, 17:00
  2. how to share variables
    By sksingh73 in forum Newbie
    Replies: 4
    Last Post: 2nd July 2010, 04:54
  3. Replies: 16
    Last Post: 16th February 2010, 13:17
  4. environmental variables
    By mohanakrishnan in forum Newbie
    Replies: 4
    Last Post: 26th October 2009, 09:43
  5. Replies: 4
    Last Post: 10th November 2006, 15:38

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.