Is possible in Qt change a value or property of a parent widget?
-
Hi All!!
A beginner with a problem :-(
Is possible from a Dialog, alter a value of a widget in its MainWindow? and... know a value of widget in mainwindow? or only is possible with parameters and return value between MainWindow and Dialog?
If is possible please can you tell me how I do it in Qt?
Thanks.
-
As I know is using the signal and slot
-
Hello.
It is possible to excess other widgets from a dialog. There are multiple ways to do so:
You can connect a signal from the dialog with a slot in an other widget. This has the advantage that the dialog itself does not need to know about the any other widgets, it just notifies the world around it that something happened, and everybody who wants to react to this needs to be connected to the signal. This way you cannot read a value from an other widget though (unless the widget sends a signal that is connected to a slot in the dialog, but that requires the widget to know that somebody else wants some information right now, which might or might not be what you want).
If you can modify the behavior of the dialog directly (i.e. if you inherited from QDialog somehow) then you can call whatever function on other widgets you like. For that the dialog needs to know the other widget of course. So you either have to pass the widget to the dialog (e.g. to your custom constructor), or you could use the QDialog's parent() method to navigate up in your widget hierarchy and find the widget you want.
-
For example, in the constructor of the dialog you can connect the signal valueChanged to the slot setValue of mainWindow. It can be an integer or whatever value type you need.
@
connect(dialog, SIGNAL(valueChanged(int)), mainWindow, SLOT(setValue(int)));
@Then, when you need the dialog to emit the signal, just type:
@emit valueChanged(value);@
Remember that you have to create those signals and slots if the classes you use don't have them. It's easy and very powerful.
More information: http://qt-project.org/doc/qt-4.8/signalsandslots.html
I hope it helps!
-
[quote author="David_Gil" date="1339400872"]For example, in the constructor of the dialog you can connect the signal valueChanged to the slot setValue of mainWindow. It can be an integer or whatever value type you need.
@
connect(dialog, SIGNAL(valueChanged(int)), mainWindow, SLOT(setValue(int)));
@Then, when you need the dialog to emit the signal, just type:
@emit valueChanged(value);@
Remember that you have to create those signals and slots if the classes you use don't have them. It's easy and very powerful.
More information: http://qt-project.org/doc/qt-4.8/signalsandslots.html
I hope it helps!
[/quote]I think its just I need Thank's a lot!
-
Note that if you are asking about properties, most experienced Qt developers will think if QObject properties, maybe even the dynamic kind (I certainly did). Those are a different kind of beast. In your case, the signal-slot mechanism is what you need.
-
[quote author="miroslav" date="1339487318"]Note that if you are asking about properties, most experienced Qt developers will think if QObject properties, maybe even the dynamic kind (I certainly did). Those are a different kind of beast. In your case, the signal-slot mechanism is what you need.[/quote]
Yes, in this moment this is all I need; but in the future....... ;-)