PDA

View Full Version : How to actually connect variables to the GUI ?



Computer Hater
14th November 2010, 02:52
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.

Timoteo
14th November 2010, 03:42
Look here. (http://doc.trolltech.com/latest/qt-basic-concepts.html) Of particular interest to you, judging by the question, would be signals and slots (http://doc.trolltech.com/latest/signalsandslots.html) but I encourage you to read all of it.

Zlatomir
14th November 2010, 05:55
The official tutorials (http://doc.qt.nokia.com/4.7/tutorials.html) 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 (http://doc.qt.nokia.com/4.7/qlabel.html) 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.

Computer Hater
14th November 2010, 20:52
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 ?

tbscope
14th November 2010, 21:00
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:

ui->yourPushButton->setText("Hi");
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).

Zlatomir
14th November 2010, 21:14
The code depends on what method you used to integrate the .ui file in C++ code, there are many ways to achieve that (http://doc.qt.nokia.com/4.7/designer-using-a-ui-file.html) (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:

void MainWindow:: on_button_clicked()
{
// this depends on how you integrate the .ui file
// example if ui is a pointer
ui->YourButtonName->setText("Hello World!");
}

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

Computer Hater
14th November 2010, 23:49
Okay, thank you both for the fast responses. It works now. :)
I still have to figure out exactly why though. :D


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.



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.