如何在QT C ++中从另一个.cpp访问小部件的UI [英] How to access ui of widget from another .cpp in QT C++

查看:797
本文介绍了如何在QT C ++中从另一个.cpp访问小部件的UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我必须从名为ApplicationWindow.cpp的另一个.cpp文件调用widget函数(注册)。

< br $>
Widget.cpp

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 < br $>







ApplicationWindow.cpp


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







在ApplicationWindow.h中我已经包含了Widget.h。





这里我可以从ApplicationWindow调用注册函数但无法访问ui意味着无法访问阅读lineedit文本。当我打印regid时它打印为空(没有)。



那么如何阅读这一行编辑文本,请你为此提供解决方案。



我尝试过:



Widget.cpp




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








ApplicationWindow.cpp


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

}

推荐答案

你必须公开一个公共接口您感兴趣的对象。通常这是通过单个对象的静态方法完成的。例如,通常只有一个主应用程序窗口,它可以有一个静态的Get()方法,它返回一个指向自身的指针。然后,该指针可用于访问对象的其他成员。您可以执行类似的操作 - 为包含控件的对象提供一个静态Get()成员,该成员返回指向自身的指针,然后您可以访问控件(如果它是公共成员,或者有一个方法返回指向它的指针)。



-edit-



这是一个例子:
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:
class MyWindow
{
public:
    static MyWindow * Get() { return m_pThis; }

protected:
    static MyWindow * m_pThis;
};

现在,您可以从包含MyWindow头文件的任何位置访问指向MyWindow的指针。如果您的控件是公共成员,则可以直接访问。如果没有,那么你需要提供一个访问者方法,比如GetTheControl()或者什么。

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.


这篇关于如何在QT C ++中从另一个.cpp访问小部件的UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆