【问题标题】:Application crashes when opening a window qt打开窗口时应用程序崩溃qt
【发布时间】: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


    【解决方案1】:

    我建议重新考虑您的解决方案,看来您使用工厂方法过于复杂了。 只需为表单使用 3 个变量,为每个变量执行一次“新”操作,然后根据您的信号使用 show() / hide() 方法。

    要回答崩溃问题,我看到的一个原因是因为您在插槽中“删除”了。 来自 Qt 文档:

    警告:在挂起的事件等待传递时删除 QObject 可能会导致崩溃。如果 QObject 存在于与当前执行的线程不同的线程中,则不得直接删除它。使用 deleteLater() 代替,这将导致事件循环在所有未决事件都传递给它之后删除对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多