【发布时间】:2014-10-26 09:57:39
【问题描述】:
我想创建一个程序,向用户显示一个带有一些答案的问题。我的应用程序使用 3 种表单:主菜单、登录菜单和游戏表单,它们都继承自一个名为 Form 的抽象类;我这样做是因为它允许使用工厂方法,当实际表单发出信号 GoFw 时,它会创建一个新窗口。
显示窗口的“循环”如下:MainMenu -> LoginMenu -> GameForm -> MainMenu... 问题是当游戏结束时(例如剩余问题的计数为零),GameForm 发出信号 GoFw 但应用程序在 show() 方法之后崩溃(我可以看到一个白色窗口,在崩溃之前没有按钮)。 调试器显示一个带有此错误的消息框:
The inferior stopped because it triggered an exception.
Stopped in thread 0 by: Exception at 0x723f7b93, code: 0xc0000005: read access
violation at: 0x0, flags=0x0 (first chance).
然后QtCreator打开一个名为:Disassembler(QHash::findNode)的文件
这是工厂方法的代码:
void FormFactory::Init(int formType)
{
///if formType == 1 MainMenu is showed
if(formType == MAIN_MENU_TYPE)
{
//inizializza il puntatore
actualForm = new MainMenu();
}
///else if is == 2 show ProfileMenu
else if(formType == PROFILE_MENU_TYPE)
{
actualForm = new LoginMenu();
}
///else if == 3 show GameForm
else if(formType == GAME_FORM_TYPE)
{
actualForm = new GameForm();
}
///else if there is no match launch an exception
else
throw UnexpectedIdEx();
connect(actualForm, SIGNAL(GoFw(int)), this, SLOT(DisplayForm(int)));
}
void FormFactory::DisplayForm(int i)
{
Reset();
Init(i);
///show the form pointed by actualform
actualForm->show();
}
void FormFactory::Reset()
{
disconnect(actualForm, SIGNAL(GoFw(int)), this, SLOT(DisplayForm(int)));
///if actualform is actually pointing to a form, delete it and set actualForm to zero
if(actualForm!=0)
delete actualForm;
actualForm = 0;
}
而MainMenu.cpp的代码是
MainMenu::MainMenu()
{
setUpGui();
}
void MainMenu::setUpGui()
{
playButton = new QPushButton(tr("Play"));
infoButton = new QPushButton(tr("Info"));
quitButton = new QPushButton(tr("Exit"));
///connect the clicked signal to the related slot
connect(infoButton, SIGNAL(clicked()), this, SLOT(info()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(quit()));
connect(playButton, SIGNAL(clicked()), this, SLOT(emitSig()));
///create the vertical layout
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(playButton);
layout->addWidget(infoButton);
layout->addWidget(quitButton);
setLayout(layout);
setWindowTitle(tr("Menu Principale"));
}
void MainMenu::emitSig()
{
emit GoFw(2);
}
感谢大家的帮助, 卢卡
【问题讨论】:
标签: c++ forms qt layout window