在我的项目中,我有一个显示有两个按钮的对话框。如果有人按“是”,那么我希望程序关闭。但我似乎无法让它工作。
我已经尝试了所有这些。
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() 的函数:
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()));
}