Results 1 to 5 of 5

Thread: slots questions that's probably really silly

  1. #1
    Join Date
    Sep 2017
    Posts
    12
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default slots questions that's probably really silly

    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:

    Qt Code:
    1. void Classy::on_Boy_clicked(bool checked)
    2. {
    3. QString Gender = "Boy";
    4. }
    5.  
    6. void Classy::on_Girl_clicked(bool checked)
    7. {
    8. QString Gender = "Girl";
    9. }
    10.  
    11. void Classy::on_MakeButton_clicked()
    12. {
    13.  
    14.  
    15. QString Gender;
    16.  
    17.  
    18. QFile writeFile( filename );
    19. if (!writeFile.open(QIODevice::Append | QIODevice::Text))
    20. {
    21. qDebug() << "Didn't open the file!";
    22. }
    23. else
    24. {
    25. QTextStream out (&writeFile);
    26. out << "Case Number: "+caseNumberText << endl;
    27. out << "Patient Name: "+ patientNameText << endl;
    28. out << "Gender: " + Gender<< endl;
    29.  
    30. }
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: slots questions that's probably really silly

    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?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    seerofsorrow (29th September 2017)

  4. #3
    Join Date
    Sep 2017
    Posts
    12
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: slots questions that's probably really silly

    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.
    Last edited by seerofsorrow; 29th September 2017 at 02:30.

  5. #4
    Join Date
    Sep 2017
    Posts
    29
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: slots questions that's probably really silly

    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.
    I am noob. Are you noob? Lets learn Qt together! https://qtnoobies.blogspot.com/

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: slots questions that's probably really silly

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Silly Question/A very silly question
    By seerofsorrow in forum Newbie
    Replies: 1
    Last Post: 21st September 2017, 23:24
  2. Questions about signals/slots and QObject lifetimes
    By themolecule in forum Qt Programming
    Replies: 11
    Last Post: 24th May 2014, 18:45
  3. Replies: 2
    Last Post: 4th July 2010, 21:44
  4. Signals and Slots questions
    By Janek in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2009, 21:47
  5. silly array element counter..
    By ct in forum General Programming
    Replies: 4
    Last Post: 3rd March 2006, 15:50

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
  •  
Qt is a trademark of The Qt Company.