PDA

View Full Version : Passing value that is obtained via pass by value from one function to another



QtNewBeee
7th July 2020, 13:21
Hi,
I'm new to QT and C++. Here is my situation.
For any help, Thanks in Advance.

void Clinical::getSlideID(QString slide_ID) // This is the function which i got value from another class using pass by value
{
//slide_idValue is a global variable declared under public.

slide_idValue = slide_ID;
qDebug()<<"Expected Value is ::"<<slide_idValue; // Here i got the value of slide_Id from my different class.

}

I want the same value of above " slide_idValue" inside the below function, in the same class itself

void Clinical::Clinical_savetodb()
{
slide_idValue;
qDebug()<<"Expected Value is ::"<<slide_idValue; // Here I am getting null value ......
}

Ginsengelf
7th July 2020, 14:32
Hi,

//slide_idValue is a global variable declared under public.
a global variable is not the same as a public member variable. Please be careful with the naming.


{
slide_idValue;
What should that line do?

In general if you store the slide_ID in a member of the class it should still be there when you access it in Clinical_savetodb(). At least if you use the same instance of the class. Please show the code where you call getSlideID() and Clinical_savetodb().
By the way, your naming scheme is a bit confusing. Normally what you called getSlideID would be called setSlideID, because you set the value inside the class. A get function would return that value.

Ginsengelf

QtNewBeee
7th July 2020, 17:20
Hi,
Thank You So much for your reply....:D

"In general if you store the slide_ID in a member of the class it should still be there when you access it in Clinical_savetodb(). At least if you use the same instance of the class. Please show the code where you call getSlideID() and Clinical_savetodb().
By the way, your naming scheme is a bit confusing. Normally what you called getSlideID would be called setSlideID, because you set the value inside the class. A get function would return that value."

Actually what I'm doing is : I have 4 different classes.

I will show my code

Added after 24 minutes:

personal.h

class Personal : public QWidget
{
Q_OBJECT

public: explicit Personal(QWidget *parent = nullptr);


private:
void create_personal_details_groupbox();


public slots:
void savetodb();
void gettext();

signals:
void pass_SlideID(QString slideid); // My signal containing value to be passed from personal to clinical
}




personal.cpp


Personal::Personal(QWidget *parent) : QWidget(parent)
{

// All other stuffs

Clinical *clinical = new Clinical();
connect(this, SIGNAL(pass_SlideID(QString)),clinical, SLOT(getSlideID(QString)));

}

void Personal::savetodb()
{
QString cytono_val = this->cytologyvalue->text();

// All other stuf......

if (query.exec())
{
QMessageBox::critical(this, tr("Save"),tr("Saved Successfully..."));
emit pass_SlideID(cytono_val); //------------ here i emits the value of my slideid-----//
}
else
{
QMessageBox::critical(this,tr("Database error"),tr("Could not setup database"));
}
}

From the above class when push button "Save " clicks "savetodb()" would emit the value of Slide ID to the slot of Clinical class having slot "getSlideID(QString)"

Clinical.h

class Clinical : public QWidget
{
Q_OBJECT
public:
explicit Clinical(QWidget *parent = nullptr);

QString slide_idValue;

private:

public slots:
void getSlideID(QString slide_ID); // function where slide id getting
void Clinical_savetodb(); // function where i again want the slide id for further databse operations

signals:


}

Clinical.cpp


Clinical::Clinical(QWidget *parent) : QWidget(parent)
{

// All Other Stuff---

void Clinical::getSlideID(QString slide_ID){

slide_idValue = slide_ID;
qDebug()<<"Expected Value is ::"<<slide_idValue;

}

void Clinical::Clinical_savetodb()
{

qDebug()<<"Expected Value is ::"<<slide_idValue;

//----uses the slide if for Other DB Operations----

}


}



I need to get the value of slide id inside both the functions getSlideID(QString slide_ID) and Clinical_savetodb()

d_stranz
7th July 2020, 23:49
Personal::Personal(QWidget *parent) : QWidget(parent)
{

// All other stuffs

Clinical *clinical = new Clinical();
connect(this, SIGNAL(pass_SlideID(QString)),clinical, SLOT(getSlideID(QString)));

}

You are creating an instance of "Clinical" here, but you aren't saving the pointer anywhere, so it basically becomes a phantom pointer as soon as this constructor exits, not accessible from anywhere.

If somewhere else in your code you are creating another "Clinical" instance, it is not the same one as this one, and is not connected to any signal sent by this "Personal" class instance, which means the slot for the second instance of Clinical will never be called.

Please use CODE tags when positing source code. See my signature, below.