Results 1 to 11 of 11

Thread: Get and Set Methods

  1. #1
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Get and Set Methods

    Anyone can teach me how to implement the get and set method in qt ?

    Qt Code:
    1. //HEADER FILE
    2. public:
    3. explicit PatientGui(QWidget *parent = 0);
    4.  
    5. ~PatientGui();
    6. QString ID;
    7.  
    8.  
    9.  
    10. QString getID() const;
    11. void setID(const QString &value);
    12.  
    13. //CPP FILE
    14. QString PatientGui::getID() const
    15. {
    16. return ID;
    17. }
    18.  
    19. void PatientGui::setID(const QString &value)
    20. {
    21. ID = ui->nric->text();
    22. }
    23.  
    24. //IN THE NEXT GUII WANT TO PASS DATA TO
    25.  
    26. ui->Patientidentifier->setText(getID());
    To copy to clipboard, switch view to plain text mode 

    I gotten this error"error: undefined reference to `Infusion::getID()'"

    Can anyone tell me what I did wrong ? I think the way i do iot is wrong , please teach me how to implement it correctly

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Get and Set Methods

    Anyone can teach me how to implement the get and set method in qt ?
    Qt? This is a C++ question.

    To use get/set methods or in general any non-static member method of a class an object (INSTANCE of class) is required.

    Qt Code:
    1. PatientGui pGui;
    2.  
    3. //else where in other class
    4. ui->Patientidentifier->setText(pGui.getID());
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    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: Get and Set Methods

    Qt Code:
    1. void PatientGui::setID(const QString &value)
    2. {
    3. ID = ui->nric->text();
    4. }
    To copy to clipboard, switch view to plain text mode 
    makes little sense. Try:
    Qt Code:
    1. void PatientGui::setID(const QString &value)
    2. {
    3. ID = value;
    4. }
    To copy to clipboard, switch view to plain text mode 
    if you want the obvious setter to match your getter.

  4. #4
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get and Set Methods

    Yes i am not too good in C++ thats why I am not sure of it . I tried your method above . builded it no errors but i cant show the data at all.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Get and Set Methods

    How does your code now looks like. Please try to post a minimal example showing your problem. Without seeing your code it is hard to help.

  6. #6
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get and Set Methods

    Quote Originally Posted by ChrisW67 View Post
    Qt Code:
    1. void PatientGui::setID(const QString &value)
    2. {
    3. ID = ui->nric->text();
    4. }
    To copy to clipboard, switch view to plain text mode 
    makes little sense. Try:
    Qt Code:
    1. void PatientGui::setID(const QString &value)
    2. {
    3. ID = value;
    4. }
    To copy to clipboard, switch view to plain text mode 
    if you want the obvious setter to match your getter.
    I changed the 'value' to ui->nric->text(); because I want to pass the text in nric , which is a line edit to the ID.
    Did I do it wrongly ?


    Added after 5 minutes:


    In short , i want to change the QString 'value' to a ui->nric->text();


    Added after 51 minutes:


    Quote Originally Posted by Lykurg View Post
    How does your code now looks like. Please try to post a minimal example showing your problem. Without seeing your code it is hard to help.
    I will try to make it simpler.
    Qt Code:
    1. #ifndef PATIENTGUI_H
    2. #define PATIENTGUI_H
    3.  
    4. #include <QWidget>
    5. #include <QLineEdit>
    6. #include <QDesktopWidget>
    7. #include <QtGui>
    8. #include <QtCore>
    9. #include <QCompleter>
    10.  
    11.  
    12. #include "logingui.h"
    13.  
    14. namespace Ui {
    15. class PatientGui;
    16. }
    17.  
    18. class PatientGui : public QWidget
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. explicit PatientGui(QWidget *parent = 0);
    24.  
    25. ~PatientGui();
    26. QString ID;
    27.  
    28.  
    29.  
    30. QString getID() const;
    31.  
    32. void setID(const QString &value);
    33. public slots:
    34.  
    35.  
    36.  
    37.  
    38. private slots:
    39.  
    40. private:
    41.  
    42. };
    43.  
    44. #endif // PATIENTGUI_H
    To copy to clipboard, switch view to plain text mode 

    //PatientGui.cpp

    Qt Code:
    1. PatientGui::PatientGui(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::PatientGui)
    4. {
    5. ui->setupUi(this);
    6. setWindowFlags(Qt::CustomizeWindowHint);
    7. this->setStyleSheet("background-color:WHITE;")
    8.  
    9. QString PatientGui::getID() const
    10. {
    11. return ID;
    12. }
    13.  
    14. void PatientGui::setID(const QString &value)
    15. {
    16. ID = value;
    17. }
    18.  
    19.  
    20. void PatientGui::on_Register_clicked()
    21. {
    22. setID(ui->nric->text());
    23. }
    To copy to clipboard, switch view to plain text mode 

    I want to pass the data to a new GUI which is called Infusion.cpp
    Qt Code:
    1. #ifndef INFUSION_H
    2. #define INFUSION_H
    3.  
    4. #include <QWidget>
    5. #include <QLineEdit>
    6. #include <QDesktopWidget>
    7. #include <QtGui>
    8. #include <QtCore>
    9. #include <QCompleter>
    10. #include "patientgui.h"
    11.  
    12.  
    13. namespace Ui {
    14. class Infusion;
    15. }
    16.  
    17. class Infusion : public QWidget
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. explicit Infusion(QWidget *parent = 0);
    23. ~Infusion();
    24. };
    25.  
    26. #endif // INFUSION_H
    To copy to clipboard, switch view to plain text mode 

    //INFUSION.cpp file
    Qt Code:
    1. #include "infusion.h"
    2. #include "ui_infusion.h"
    3. #include "patientgui.h"
    4.  
    5.  
    6.  
    7. #include <QTime>
    8. infusionChangeRegimen*changeregimen=0;
    9. LoginGui*infusionLogingui=0;
    10. PatientGui*infusionPatientgui=0;
    11.  
    12. Infusion::Infusion(QWidget *parent) :
    13. QWidget(parent),
    14. ui(new Ui::Infusion)
    15. {
    16.  
    17. ui->setupUi(this);
    18. setWindowFlags(Qt::CustomizeWindowHint);
    19. this->setStyleSheet("background-color:white");
    20. PatientGui patient;
    21. ui->Patientidentifier->setText(""+patient.getID());
    To copy to clipboard, switch view to plain text mode 
    Last edited by 020394; 16th July 2013 at 12:36.

  7. #7
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get and Set Methods

    Sorry for the spam .
    Just wondering whether is there any other method other than get/set method . to get a data from a lineEdit/label and pass it to a new form/cpp/gui file ?

  8. #8
    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: Get and Set Methods

    In the PatientGui class, if you want the ID member variable to contain the current text for a QLineEdit in the GUI then you can just assign it directly.
    Qt Code:
    1. ID = ui->nric->text();
    To copy to clipboard, switch view to plain text mode 
    This has nothing to do with getters or setters or passing the information outside the class or to another form.

    Get/set methods provide a public interface that exposes (typically) private data from one class to the outside world. If you have a pointer to or reference to an object of class PatientGui in an object of another class then it can access the data through the get/set methods.

    You are free to use any C++ mechanism to pass data between objects. Qt also provides a signals/slots mechanism suitable for some circumstances.

    Your Infusion class constructor creates a local PatientGui object, extracts the current value of ID from that object, and then discards the PatientGui object. Since that PatientGui object has never been displayed or otherwise manipulated the value of the return ID will be an empty QString. You need to explain the relationship between an object of class PatientGui and one of class Infusion before you can know how best to give Infusion access to a specific instance of PatientGui that the user is seeing and interacting with.
    Last edited by ChrisW67; 17th July 2013 at 04:53.

  9. #9
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get and Set Methods

    Hello , I tried using the signal/slot method , But to no avail , i am unable to pass the data. All i get is a Empty QString .
    Here is my codes
    Qt Code:
    1. //PatientGui.h file
    2.  
    3. signals:
    4. void getNric(const QString&);
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //PatientGui.cpp file
    2.  
    3. void PatientGui::on_Register_clicked()
    4. {
    5. emit getNric(ui->lineEdit->text());
    6. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //in Infusion.h file
    2. private slots:
    3. void setPatientIdentifier( const QString&nric );
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //in Infusion.cpp file
    2. #include "PatientGui.h"
    3.  
    4. Infusion::Infusion(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Infusion)
    7. {
    8.  
    9. PatientGui infusionPatientgui;
    10. connect( &infusionPatientgui, SIGNAL(getNric(const QString&)),this,SLOT(setPatientIdentifier(const QString& nric)));
    11. }
    12. void Infusion::setPatientIdentifier(const QString& nric)
    13. {
    14. ui->Patientidentifier->setText(nric);
    15. }
    To copy to clipboard, switch view to plain text mode 

    Where have i done wrong ?

  10. #10
    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: Get and Set Methods

    Last listing first, see comments I have inserted:
    Qt Code:
    1. //in Infusion.cpp file
    2. #include "PatientGui.h"
    3.  
    4. Infusion::Infusion(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Infusion)
    7. {
    8. PatientGui infusionPatientgui;
    9. //^^ A local PatientGui object that ceases to exist at the end of this constructor. This is in no way related
    10. // to any PatientGui object that is displayed and the user is interacting with.
    11. connect( &infusionPatientgui, SIGNAL(getNric(const QString&)),this,SLOT(setPatientIdentifier(const QString& nric)));
    12. //^^ an attempt to connect the local PatientGui object to a slot in this object that fails because the SLOT()
    13. // specification should not include the variable name. A warning that the connect() failed is printed to application output.
    14. //vv Even if connect had succeeded it would be broken now as infusionPatientgui goes out of scope and is destroyed.
    15. }
    16. void Infusion::setPatientIdentifier(const QString& nric)
    17. {
    18. ui->Patientidentifier->setText(nric);
    19. }
    To copy to clipboard, switch view to plain text mode 

    C++ object lifetimes are important to understand.
    C++ objects of the same class are (generally) independent of each other.

  11. #11
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get and Set Methods

    So how can I connect the PatientGui Object to the Infusion method ? So i can successfully show the Variable i called in PatientGui Object to Infusion ?

Similar Threads

  1. Problem with pointers and methods
    By LastElemtnal in forum Qt Programming
    Replies: 3
    Last Post: 8th December 2011, 03:21
  2. What is the general rule for onX methods in Qt?
    By piotr.dobrogost in forum Qt Programming
    Replies: 8
    Last Post: 17th August 2009, 23:10
  3. Accessing QMainWindow methods
    By vieraci in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2009, 08:38
  4. wrapper of methods
    By mickey in forum General Programming
    Replies: 8
    Last Post: 15th August 2008, 15:33
  5. The Methods of quick search...
    By Xagen in forum General Discussion
    Replies: 1
    Last Post: 27th February 2006, 19:09

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.