简体   繁体   English

带有QT的多个窗口

[英]Multiple windows with QT

I'm a student and for my thesis I want to create a question-based game; 我是一名学生,所以我想创建一个基于问题的游戏; I would like to use QT to elaborate the graphical interface, but I've never used it and I don't know how to create an application with multiple windows. 我想使用QT来完善图形界面,但我从未使用过它,也不知道如何创建具有多个窗口的应用程序。 There is a base-class (abstract) called "Form" and three others classes that inherits from Form: MainMenuForm, LoginForm and GameForm. 有一个称为“ Form”的基类(抽象),还有另外三个从Form继承的类:MainMenuForm,LoginForm和GameForm。

In some of the examples I found, the layout of the window is setted in the constructor and in the main is called the method show(), but I would like to do something like this in the main: 在我发现的一些示例中,窗口的布局是在构造函数中设置的,而在主窗口中则称为show()方法,但是我想在主窗口中执行以下操作:

// Creates a FormFactory object and ensures singleton instance
FormFactory * factory;

factory = factorySingleton.GetFactory();

//select a Form to display

int choice = 1;
Form * actualForm;

while (choice != 0)
{
    factory->Init(choice);
    actualForm = factory->ReturnActualForm();
    choice = actualForm->Display();
}

The Display method, common to every derived-Form, must return an int that indicates which window display (for example, the Display method of MainMenuForm returns 2 if the user want to proceed to the Login window or 0 if the user want to close the application); 每个派生式窗体共有的Display方法必须返回一个int,该int指示哪个窗口显示(例如,如果用户要继续登录窗口,MainMenuForm的Display方法将返回2;如果要关闭登录窗口,则返回0。应用); obviously, the Display method must also show the form. 显然,Display方法还必须显示表单。

Finally, I noticed that in all examples they write 最后,我注意到在所有示例中

return app.exec();

in the end of the main. 在主要的结尾。

For my project, where am I supposed to write app.exec()? 对于我的项目,我应该在哪里编写app.exec()? Before, after or inside the while statement? 在while语句之前,之后还是内部?

Your design is quite bad. 您的设计相当糟糕。 The code you quoted should be in a separate class which is instantiated before calling app.exec(). 您引用的代码应该在一个单独的类中,该类在调用app.exec()之前已实例化。 The current solution only allows to select the form once, it will be displayed and when you close it, the program ends. 当前的解决方案只允许选择一次表单,它将显示出来,当您关闭它时,程序结束。

Read Qt docs about signals and slot, and how to write Qt GUI apps, you need a different approach. 阅读有关信号和插槽的Qt文档,以及如何编写Qt GUI应用程序,您需要使用其他方法。

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

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