[英]Application crashes when opening a window qt
我想創建一個程序,向用戶顯示一個帶有一些答案的問題。 我的應用程序使用3種形式:“主菜單”,“登錄菜單”和“游戲表單”,所有這些都繼承自名為Form的抽象類。 我這樣做是因為它允許使用factory方法,當實際形式發出信號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);
}
謝謝大家的幫助,盧卡
我建議您重新考慮您的解決方案,似乎您將它與工廠方法過於復雜了。 只需對表單使用3個變量,對每個變量執行一次“ new”操作,然后根據信號使用show()/ hide()方法。
為了回答崩潰問題,我看到的一個原因是因為您在插槽中執行了“刪除”操作。 從Qt doc:
警告:在等待傳遞待處理事件時刪除QObject可能導致崩潰。 如果QObject與當前正在執行的線程不在同一線程中,則不能直接刪除它。 使用deleteLater()代替,這將導致事件循環在所有未決事件傳遞到對象后刪除該對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.