简体   繁体   English

从当前的一个打开QWidget或QWindow

[英]Open QWidget or QWindow from the current one

I know this was asked here , but it doesn't work... 我知道这是在这里问的,但这不起作用...

Here's my problem: my program actually has 2 forms: LoginWindow and MainWindow , the programme starts on LoginWindow and then I called a class Login where are all methods and attributes for checking users Here's some code: 这是我的问题:我的程序实际上有2种形式: LoginWindowMainWindow ,该程序在LoginWindow上启动,然后我调用Login类,其中有所有用于检查用户的方法和属性,下面是一些代码:

//in LoginWindow class
Login log;
    log.checkID(ui->usrnmle->text().toStdString(), ui->passwdle->text().toStdString());

//in Login class
void Login::checkID(string usr, string passwd)
{
    if(usr == "Test" && passwd == "root")
    {
        cout << "Ok!" << endl;

        MainWindow mw = MainWindow();
        mw.show();

        LoginWindow lw = LoginWindow();
        lw.hide();

    }
    else
    {
        cout << "Connection failed " << endl;
    }
}

I type the good username and password, I got the 'Ok!' 我输入了好的用户名和密码,然后点击“确定!”。 in the output console but MainWindow never appears. 在输出控制台中,但MainWindow从不出现。

Someone can help? 有人可以帮忙吗?

Your MainWindow mw is a local variable that is destroyed as soon as you are out of checkID function (or even out of if but that does not really matters) 你的MainWindow mw是,只要你出的摧毁了一个局部变量checkID功能(甚至退出的if但这并不真正重要的)

You need to create it on heap 您需要在堆上创建它

MainWindow *mw =  new MainWindow();
mw->show();

Then to avoid memory leak you would need to destroy it when it is of no further use: 然后,为避免内存泄漏,您将在不再使用它时将其销毁:

delete mw;

I'm not a QT developer, but 我不是QT开发人员,但

{
        cout << "Ok!" << endl;

    MainWindow mw = MainWindow();
    mw.show();

    LoginWindow lw = LoginWindow();
    lw.hide();
}

You are creating two local windows here, which are destroyed on the spot when the scope ends. 您将在此处创建两个本地窗口,合并范围结束时将立即将其销毁。

you need to keep these instances alive throught their use. 您需要在使用过程中保持这些实例的生命。 for example, make them member variables of some bigger object like "App" class os so. 例如,使它们成为某个较大对象(例如“ App”类os)的成员变量。

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

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