在我的項目中,我有一個顯示有兩個按鈕的對話框。如果有人按下「是」,那麼我希望程序關閉。但我似乎無法讓它工作。無法關閉GUI程序
我試過所有這些。
qApp->exit();
qApp->quit();
QApplication::exit();
QApplication::quit();
QCoreApplication::exit();
QCoreApplication::quit();
而這些都沒有關閉程序。我試着將它們移動到我的main.cpp中,我嘗試了第二個函數只是爲了關閉,沒有任何效果。
它會與我的事件循環更早檢查更新有什麼關係嗎?如果是這樣,我會發布它。
編輯:
這是我的main.cpp和,我想我的程序關閉功能:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(icons);
QApplication a(argc, argv);
MainWindow w;
w.show();
w.checkVersion();
return a.exec();
}
功能:
void MainWindow::checkVersion()
{
if((version != "1.0.0") && (version != ""))//version is a string that is filled when the mainwindow first opens.
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Update", "Version " + version + " is now available. Would you like to update now?\n\nOr visit http://www.youtube.com/oPryzeLP to download manually.", QMessageBox::Yes | QMessageBox::No);
if(reply == QMessageBox::Yes)
{
}
QApplication::exit();//moved out of reply just to test closing
}
}
這是包含的功能事件循環:
void MainWindow::downloadFile(const QString &url, const QString &aPathInClient)
{
QNetworkAccessManager* m_NetworkMngr = new QNetworkAccessManager(this);
QNetworkReply *reply = m_NetworkMngr->get(QNetworkRequest(QUrl(url)));
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QUrl aUrl(url);
QFileInfo fileInfo=aUrl.path();
QFile file(aPathInClient+"\\"+fileInfo.fileName());
file.open(QIODevice::WriteOnly);
file.write(reply->readAll());
delete reply;
loop.quit();
}
這是調用downloadFile()函數:你列出退出了Qt事件循環
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
downloadFile("link", "stuff");
QFile info("stuff\\info.txt");
if(info.open(QIODevice::ReadOnly))
{
QTextStream in(&info);
while(!in.atEnd())
{
version = in.readLine();
versionLink = in.readLine();
vidLink = in.readLine();
}
}
info.close();
setCentralWidget(ui->tabWidget);
ui->creativeFlag->setEnabled(false);
ui->structures->setEnabled(false);
ui->raining->setEnabled(false);
ui->thundering->setEnabled(false);
ui->hardcore->setEnabled(false);
AddSlotsToGroup();
AddBlocksToGroup();
QPalette palette = ui->blockFrame->palette();
palette.setColor(backgroundRole(), QColor(139, 139, 139));
ui->blockFrame->setPalette(palette);
ui->blockFrame->setAutoFillBackground(true);
QPixmap map_bg(":/images/mapbg.png");
ui->mapBgLabel->setPixmap(map_bg.scaled(224, 224, Qt::IgnoreAspectRatio, Qt::FastTransformation));
QShortcut *returnShortcut = new QShortcut(QKeySequence("Return"), ui->tab_4);
QObject::connect(returnShortcut, SIGNAL(activated()), ui->blockFind, SLOT(click()));
}
發佈重現問題的最小測試用例。將所有代碼放入一個文件(main.cpp,用於類聲明和實現)。如果你有任何'Q_OBJECT'宏,你可以'#include「main.moc」'。 –
在dialog.exec()之前調用app.exec(),還是直接在main()中打開對話框? –
我將用我的main.cpp和我要關閉的函數更新這篇文章。 – mrg95