简体   繁体   English

使用PushButton C ++ QT连接表单

[英]Connecting forms with pushButton c++ QT

I am creating a program that requires a form to be opened, and the previous to be closed on a push button click. 我正在创建一个程序,该程序要求打开一个窗体,并在单击按钮时将其关闭。 My current problem is that when I click the button the new form is shown for a millisecond then disappears. 我当前的问题是,当我单击按钮时,新表单将显示一毫秒,然后消失。

void mainMenu::on_mainLoginB_clicked()
{
     logIn objlogIn;
     objlogIn.show();
}

void mainMenu::on_mainExitB_clicked()
 {
     exit(1);
 }

here is my header file 这是我的头文件

private slots:
    void on_mainLoginB_clicked();

    void on_mainExitB_clicked();

private:
    Ui::mainMenu *ui;


};

objLogIn is declared in the scope of the SLOT, and for this reason, deleted when the function return. objLogIn在SLOT的范围内声明,因此,在函数返回时将其删除。

Remember that QT, like most user interfaces works with an event (message) loop, so functions like show() do not block: they immediately return and it is the event loop managing it further. 记住,像大多数用户界面一样,QT与事件(消息)循环一起使用,因此show()类的函数不会阻塞:它们会立即返回,这是事件循环进一步对其进行管理。

For solving this issue: 为解决此问题:

  • Create the object in a higher scope and show it/hide it when required. 在更高的范围内创建对象,并在需要时显示/隐藏它。
  • Create the object dynamically in the heap. 在堆中动态创建对象。

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

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