简体   繁体   English

从父级到子级QT传递QString

[英]Passing QString from parent to child QT

I need in my program to get one QString from parent window to child window. 我需要在程序中从父窗口到子窗口获取一个QString。

 menu=new user(this);  menu->show();

This is how I am creating child window. 这就是我创建子窗口的方式。 I have QString Username; 我有QString Username; declared in my parent called "login". 在我父母那里宣布为“登录”。

This is how I am trying to pass QString to my new window: 这就是我试图将QString传递到新窗口的方式:

 QString loginnn=((login*)this->parent())->Username 

But I'm getting error: 但是我遇到了错误:

"login was not declared in this scope" “未在此范围内声明登录”

Although I've included "login.h" file to my "user.h" file, and also "login" is marked in pink colour, and QT know what it is. 尽管我已将“ login.h”文件包含到“ user.h”文件中,并且“ login”也用粉红色标记,但QT知道它是什么。

Thanks for any help! 谢谢你的帮助!

The simplest solution would be to send a reference to the required string during your object construction 最简单的解决方案是在对象构建过程中发送对所需字符串的引用

You should simply do this : 您应该简单地这样做:

menu = new user(this, username);
menu->show();

Menu.cpp Menu.cpp

class User
{
private:
     QString &_login;
public:
     User(..., QString &login):
       _login(login)
}

Now you should be able to use this string, which anyway would make sense to give during construction 现在您应该可以使用此字符串了,无论如何在构建过程中都应该给出该字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM