在我的项目中,我有一个显示有两个按钮的对话框。如果有人按下“是”,那么我希望程序关闭。但我似乎无法让它工作。无法关闭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