Results 1 to 4 of 4

Thread: Passing value that is obtained via pass by value from one function to another

  1. #1
    Join Date
    Jul 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Unhappy Passing value that is obtained via pass by value from one function to another

    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 ......
    }

  2. #2
    Join Date
    Jul 2008
    Posts
    447
    Thanks
    9
    Thanked 58 Times in 56 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing value that is obtained via pass by value from one function to another

    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

  3. #3
    Join Date
    Jul 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Passing value that is obtained via pass by value from one function to another

    Hi,
    Thank You So much for your reply....

    "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()
    Last edited by QtNewBeee; 7th July 2020 at 17:20.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,564
    Thanks
    255
    Thanked 728 Times in 718 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Passing value that is obtained via pass by value from one function to another

    Qt Code:
    1. Personal::Personal(QWidget *parent) : QWidget(parent)
    2. {
    3.  
    4. // All other stuffs
    5.  
    6. Clinical *clinical = new Clinical();
    7. connect(this, SIGNAL(pass_SlideID(QString)),clinical, SLOT(getSlideID(QString)));
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by d_stranz; 8th July 2020 at 00:46.
    <=== 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. How to pass the arguments from the GUI to a function?
    By Flavio Mesquita in forum Newbie
    Replies: 0
    Last Post: 7th June 2018, 16:25
  2. How to pass a QML ListModel to C++ function
    By cmessineo in forum Newbie
    Replies: 3
    Last Post: 29th May 2015, 11:04
  3. pass member function as argument int template function
    By bibhukalyana in forum General Programming
    Replies: 1
    Last Post: 12th March 2013, 07:05
  4. Replies: 14
    Last Post: 1st December 2009, 20:45
  5. How to pass a QComboBox to a function?
    By Ricardo_arg in forum General Programming
    Replies: 4
    Last Post: 9th March 2008, 22:16

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.