Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have to call the widget function(registration) from another .cpp file called ApplicationWindow.cpp.

Widget.cpp
Void Widget::registration()
{
    qDebug()<<"In registration function";
    QString regid = ui->lineedit_regid->text;
    qDebug()<<"regid="<<regid;
    //  after calling some API
}



ApplicationWindow.cpp


Void ApplicationWindow::closeApp()
{
    Widget *obj = new Widget();
    obj->registration(); //from here I have to call registration function
}



In ApplicationWindow.h I have included Widget.h.


Here I am able to call the registration function from ApplicationWindow but unable to access ui means unable to read lineedit text. When I am printing regid it is printing empty(nothing).

So how to read this line edit text, can you please provide the solution for this.

What I have tried:

Widget.cpp
Void Widget::registration()
{
    qDebug()<<"In registration function";
    QString regid = ui->lineedit_regid->text;
    qDebug()<<"regid="<<regid;
    ///  after calling some API  /////
}



ApplicationWindow.cpp


void ApplicationWindow::closeApp()
{
    Widget *obj = new Widget(); 
    obj->registration();  // from here I have to call registration function

}
Posted
Updated 29-Apr-19 21:21pm

1 solution

You have to expose a public interface to the object you are interested in. Usually this is done through static methods of a single object. For example, there is usually only one main application window and it can have a static Get() method that returns a pointer to itself. That pointer can then be used to access other members of the object. You can do something similar - give the object that contains the control a static Get() member that returns a pointer to itself and then you can access the control if it is a public member or there is a method that returns a pointer to it.

-edit-

Here's an example:
C++
class MyWindow
{
public:
    static MyWindow * Get() { return m_pThis; }

protected:
    static MyWindow * m_pThis;
};
Now you can access a pointer to MyWindow from anywhere that includes MyWindow's header file. If your control is a public member then it is directly accessible. If not then you will need to provide an accesser method, like GetTheControl() or what ever.
 
Share this answer
 
v2
Comments
Member 13740197 2-May-19 6:46am    
Can you please explain with an example.
Rick York 2-May-19 10:31am    
an example was added to the solution.
Member 13740197 3-May-19 2:21am    
My code
public:
static Widget * Get(){return w_pThis;}

protected:
static Widget * w_pThis;

After using this I am getting below error

error: undefined reference to `Widget::m_pThis'

Can you please tell me how to solve this.
Rick York 3-May-19 11:09am    
I hope the code you posted is in a Window class. Otherwise it is of little use.

One of the rules of using static variables is there has to be what amounts to an external declaration or, in essence, an instantiation. In the implementation file you need to have this : Widget * Widget::m_pThis = nullptr; and then remember to set the value of m_pThis in the constructor of the class. Note that since this is a static member there can be only one instance of it which means only one instance of the Widget object also. That's why my sample code had the static pointer in the window. The window typically contains several widgets and all of them would be accessible through the static pointer to the window object : Window::Get()->m_Widget_1.do_something();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900