PDA

View Full Version : Get and Set Methods



020394
16th July 2013, 05:04
Anyone can teach me how to implement the get and set method in qt ?



//HEADER FILE
public:
explicit PatientGui(QWidget *parent = 0);

~PatientGui();
QString ID;



QString getID() const;
void setID(const QString &value);

//CPP FILE
QString PatientGui::getID() const
{
return ID;
}

void PatientGui::setID(const QString &value)
{
ID = ui->nric->text();
}

//IN THE NEXT GUII WANT TO PASS DATA TO

ui->Patientidentifier->setText(getID());


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

Santosh Reddy
16th July 2013, 05:08
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.



PatientGui pGui;

//else where in other class
ui->Patientidentifier->setText(pGui.getID());

ChrisW67
16th July 2013, 05:52
void PatientGui::setID(const QString &value)
{
ID = ui->nric->text();
}

makes little sense. Try:


void PatientGui::setID(const QString &value)
{
ID = value;
}

if you want the obvious setter to match your getter.

020394
16th July 2013, 07:54
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.

Lykurg
16th July 2013, 09:43
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.

020394
16th July 2013, 12:36
void PatientGui::setID(const QString &value)
{
ID = ui->nric->text();
}

makes little sense. Try:


void PatientGui::setID(const QString &value)
{
ID = value;
}

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:


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.


#ifndef PATIENTGUI_H
#define PATIENTGUI_H

#include <QWidget>
#include <QLineEdit>
#include <QDesktopWidget>
#include <QtGui>
#include <QtCore>
#include <QCompleter>


#include "logingui.h"

namespace Ui {
class PatientGui;
}

class PatientGui : public QWidget
{
Q_OBJECT

public:
explicit PatientGui(QWidget *parent = 0);

~PatientGui();
QString ID;



QString getID() const;

void setID(const QString &value);
public slots:




private slots:

private:

};

#endif // PATIENTGUI_H


//PatientGui.cpp




PatientGui::PatientGui(QWidget *parent) :
QWidget(parent),
ui(new Ui::PatientGui)
{
ui->setupUi(this);
setWindowFlags(Qt::CustomizeWindowHint);
this->setStyleSheet("background-color:WHITE;")

QString PatientGui::getID() const
{
return ID;
}

void PatientGui::setID(const QString &value)
{
ID = value;
}


void PatientGui::on_Register_clicked()
{
setID(ui->nric->text());
}


I want to pass the data to a new GUI which is called Infusion.cpp


#ifndef INFUSION_H
#define INFUSION_H

#include <QWidget>
#include <QLineEdit>
#include <QDesktopWidget>
#include <QtGui>
#include <QtCore>
#include <QCompleter>
#include "patientgui.h"


namespace Ui {
class Infusion;
}

class Infusion : public QWidget
{
Q_OBJECT

public:
explicit Infusion(QWidget *parent = 0);
~Infusion();
};

#endif // INFUSION_H


//INFUSION.cpp file



#include "infusion.h"
#include "ui_infusion.h"
#include "patientgui.h"



#include <QTime>
infusionChangeRegimen*changeregimen=0;
LoginGui*infusionLogingui=0;
PatientGui*infusionPatientgui=0;

Infusion::Infusion(QWidget *parent) :
QWidget(parent),
ui(new Ui::Infusion)
{

ui->setupUi(this);
setWindowFlags(Qt::CustomizeWindowHint);
this->setStyleSheet("background-color:white");
PatientGui patient;
ui->Patientidentifier->setText(""+patient.getID());

020394
16th July 2013, 16:47
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 ?

ChrisW67
17th July 2013, 04:48
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.


ID = ui->nric->text();

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.

020394
17th July 2013, 11:33
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

//PatientGui.h file

signals:
void getNric(const QString&);



//PatientGui.cpp file

void PatientGui::on_Register_clicked()
{
emit getNric(ui->lineEdit->text());
}


//in Infusion.h file
private slots:
void setPatientIdentifier( const QString&nric );


//in Infusion.cpp file
#include "PatientGui.h"

Infusion::Infusion(QWidget *parent) :
QWidget(parent),
ui(new Ui::Infusion)
{

PatientGui infusionPatientgui;
connect( &infusionPatientgui, SIGNAL(getNric(const QString&)),this,SLOT(setPatientIdentifier(const QString& nric)));
}
void Infusion::setPatientIdentifier(const QString& nric)
{
ui->Patientidentifier->setText(nric);
}

Where have i done wrong ?

ChrisW67
17th July 2013, 23:08
Last listing first, see comments I have inserted:


//in Infusion.cpp file
#include "PatientGui.h"

Infusion::Infusion(QWidget *parent) :
QWidget(parent),
ui(new Ui::Infusion)
{
PatientGui infusionPatientgui;
//^^ A local PatientGui object that ceases to exist at the end of this constructor. This is in no way related
// to any PatientGui object that is displayed and the user is interacting with.
connect( &infusionPatientgui, SIGNAL(getNric(const QString&)),this,SLOT(setPatientIdentifier(const QString& nric)));
//^^ an attempt to connect the local PatientGui object to a slot in this object that fails because the SLOT()
// specification should not include the variable name. A warning that the connect() failed is printed to application output.
//vv Even if connect had succeeded it would be broken now as infusionPatientgui goes out of scope and is destroyed.
}
void Infusion::setPatientIdentifier(const QString& nric)
{
ui->Patientidentifier->setText(nric);
}


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

020394
18th July 2013, 02:57
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 ?