QDialog Signals & Slots - How do I pass values back to the Main Form mid-function?
-
I'm designing a QDialog as an enhanced QMessageBox. However, I am not sure how to handle the signals and slots correctly to pass values back to my Main Form mid-function. Slots only seem to pass values to a function during execution. Therefore, it seems that the only fix is to create global variables within the Main Form class. This would become unnecessarily complex and messy. I cannot use QSignalMapper (my variables are not restricted to integers and strings).
Is there a way for a signal to pass values to a slot from one dialog to another in the middle of a function?
-
Hi
What you mean mid-function ?
I mean , what function ?do you mean like ( pseudo code)
mainwindow::DoDialog() {
MyDialog d;
d.exec();
}and in MyDialog
void Mydialog::DoTheStuff() {
x = 100;
emit MyNewValueToMain(x); // send signal to main
more calc etc.
}So Main gets some signal while the dialog is "up" ?
-
I'm trying to preserve values between slots or functions so that the values can be accessed from any function at any point in the Main Form class. I want the state of checkboxes and line edits in the dialog to be accessible to private variables, not global variables, in the Main Form class.
Due to the design of the slots and signals, the Main Form class seems to be limited to:
Creating invisible check boxes and line edits to temporarily store the returned values. This can unnecessarily complicate the forms.
Creating global variables in the Main Form to carry the controls' states across every function.Is there a way to make this simpler, not more complex?
-
@QtBit44 I guess that instead of considering signals and slots to pass data from/to your dialog from main form, you may need to provide your dialog with getter/setters that allow you to set the values to display when your dialog is shown, and then once the user closes (accept) the dialog you ask it about the maybe new values. I have implemented something like this with a small dialog for server configuration, please imagine labels, line edits and checkbox here:
address: ......... port: ..... https: X
so there are setAddress(address), setPort(port), setHttps(true/false) and getters getAddress(), getPort() and isHttpsChecked() So some pseudo-code for using the dialog is as follows:
mainwindow::DoDialog() { MyDialog d; d.setAddress("192.168.1.1"); d.setPort(8080); d.setHttps(true); d.exec(); // need to check if the user changed values if (d.accepted()) { if (d.getAddress != currentAddress) { currentAddress = d.getAddress(); } // check remaining values for changes } }
Obviously, if your dialog got lots of values to set/get this approach may not be the best way to go. In that case I imagine breaking the dialog into smaller ones or considering a getter/setter for an object containing the values to display/change