PDA

View Full Version : How do I gain access to the data saved in a form when the form has been completed?



ajf
17th July 2013, 19:09
I apologize for being an incompetent newbie, but I am having a problem in extracting the data imbedded in a form, once the user has completed it.
The form is named SystemSettingsDialog, and has a relatively large number of fields for the user to complete. The user signals completion by pressing an OK button or a Cancel button. If the user selects OK, I need to process the data entered in the form. For the sake of simplicity, I will assume that the the form contains a QLineEdit named "name", and two buttons named "OKButton" and "CancelButton"
The form is launched using a signal from the MainWindow. The OKButton is connected to accept, and the CancelButton is connected to reject the dialog.
Here is the relevant code:
in MainWindow.cpp, I have the code to launch the dialog


systemsettingsdialog *SystemSettingsDialog = 0;

void MainWindow::on_actionSystem_Settings_triggered()
{
SystemSettingsDialog = new systemsettingsdialog(this);
reply = systemsettingsDialog->exec();
if (reply)
{
// How do I retrieve name->text from the form? See note 1
}
delete SystemSettingsDialog;

}

In systemsettingsdialog.cpp I have the following code

systemsettingsdialog::systemsettingsdialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::systemsettingsdialog)
{
// Setup dialog from SystemSettingsData See note 2
ui->setupUi(this);
}



Is there any way I can access the ui class at this point to get the data
It appears that I can populate the ui class with data from another source, but I have not tested this yet


Thank you in advance for any advice.

-a.

ChrisW67
17th July 2013, 21:58
You need to give the systemsettingsdialog a "getXXX() const" function for each private member variable you wish to expose through the public interface of the class. This is standard C++ and not specific to Qt.

ajf
18th July 2013, 01:50
I should have said that I used QT Designer to generate the form.
This declares all members that I need access to as public, and I can access them if I have a pointer to the class, which I have in the code snippet from systemsettingsdialog.cpp. (ui). However, this is declared as a local variable, and is out of scope when I try to access it from MainWindow.cpp.

If somebody could explain what "systemsettingsdialog::systemsettingsdialog(QWidget *parent) : QDialog(parent), ui(new Ui::systemsettingsdialog)" means, and how to make "ui" public, then I think I can sort out the problem.

-a.

ChrisW67
18th July 2013, 04:12
The code generated by uic from your Designer ui file is included in your systemsettingsdialog class (by the template code generated by Qt Creator I assume) as a private member variable, nothing public about it.
This:


systemsettingsdialog::systemsettingsdialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::systemsettingsdialog)
{
...
}

is the constructor for the class, and lines 2 and 3 are the initialization list (http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/) for the parent class and member variables.

You can grant controlled access to private parts of the systemsettingsdialog by creating public getter functions in the systemsettingsdialog. Allowing uncontrolled access by, for example, moving the declaration of ui into the public part of the class is generally a bad idea even if it is valid C++.