PDA

View Full Version : slots questions that's probably really silly



seerofsorrow
28th September 2017, 23:27
I currently have another silly question, I have a layout in gui folder with two buttons that are exclusive and have a bool checked slot on them Also for the whole window I have a make template button with a clicked slot on it as well and here from my class.cpp file is the code:




void Classy::on_Boy_clicked(bool checked)
{
QString Gender = "Boy";
}

void Classy::on_Girl_clicked(bool checked)
{
QString Gender = "Girl";
}

void Classy::on_MakeButton_clicked()
{


QString Gender;


QFile writeFile( filename );
if (!writeFile.open(QIODevice::Append | QIODevice::Text))
{
qDebug() << "Didn't open the file!";
}
else
{
QTextStream out (&writeFile);
out << "Case Number: "+caseNumberText << endl;
out << "Patient Name: "+ patientNameText << endl;
out << "Gender: " + Gender<< endl;

}

}



I'm trying to figure out how to make the Gender variable viewable to multiple slots at the same time. All of the code I realize won't run I'm just using it as example, as I have written the other code for it to create a directory and create the file correctly. I found this article source: https://doc.qt.io/archives/qq/qq10-signalmapper.html but slightly confused as not all of the buttons in this instance are in the same layout.

My humble thanks to any answers I get.

d_stranz
28th September 2017, 23:59
Umm, do you know C++? Have you heard of member variables in C++ classes? Like maybe adding a "QString gender;" member variable and using that instead of local stack variables in each slot?

seerofsorrow
29th September 2017, 02:30
I defined QString *Gender in public: under Classy. However when I do this the program crashes if I remove it as a local variable. Do I need to define it first inside of the Classy header file?


Yes I know C++ to some degree. I took two courses in college but for all the good it did me I may as well have slept through the classes :) I looked up what you were talking about in my text book and I get that it's like nested classes basically. But if everything is under the same class why would it matter?

Basically a local variable is one that's defined inside of a function, a member variable (Global variable) is defined inside of the class.

If you can't tell I actually made an A+ in all my coding classes but obviously learned zilch in practical application.

Added after 1 6 minutes:

I"m an idiot.
It's ok please ignore me I figured it out.

GeneCode
29th September 2017, 04:45
Ya that is because you keep on redeclaring gender variable in every functions.
You need to declare it in your h as global to your class instead.

d_stranz
29th September 2017, 21:20
I'm an idiot.

We were all idiots once. Some of us are still idiots, and the rest of us tend to elect them to public office for some reason.