Results 1 to 4 of 4

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

  1. #1
    Join Date
    Nov 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How do I gain access to the data saved in a form when the form has been completed?

    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);
    }
    1. Is there any way I can access the ui class at this point to get the data
    2. 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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How do I gain access to the data saved in a form when the form has been completed

    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.

  3. #3
    Join Date
    Nov 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I gain access to the data saved in a form when the form has been completed

    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(QWidge t *parent) : QDialog(parent), ui(new Ui::systemsettingsdialog)" means, and how to make "ui" public, then I think I can sort out the problem.

    -a.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How do I gain access to the data saved in a form when the form has been completed

    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:
    Qt Code:
    1. systemsettingsdialog::systemsettingsdialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::systemsettingsdialog)
    4. {
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    is the constructor for the class, and lines 2 and 3 are the initialization list 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++.

Similar Threads

  1. Replies: 7
    Last Post: 23rd May 2012, 13:00
  2. How to hide from taskbar a form showed form mainwindow
    By tonnot in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2011, 15:36
  3. how can i access other form widget
    By crissti_cta in forum Newbie
    Replies: 2
    Last Post: 14th December 2010, 21:51
  4. Access form's widget's in other class
    By p3c0 in forum Newbie
    Replies: 3
    Last Post: 12th June 2010, 15:20
  5. access the members of the Form from a loop.
    By cbarmpar in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2008, 02:35

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.