[英]Program crashes on exit qt5
我添加了QApplication::quit();
到两个 if 语句,以便当用户单击“取消”或“x”时程序结束; 但是,一旦取消或单击 x,程序就会崩溃,并表示程序有问题。 我不确定为什么它会一直崩溃。 我在 main.cpp 文件中添加了这段代码。
Header 档案:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QString>
class SentenceProcessor {
public:
int getWordNumber(QString sentence);
int getVowelNumber(QString sentence);
bool isReversible(QString sentence);
QString wordsReversed(QString sentence);
QString formatSentence(QString sentence);
};
#endif // MAINWINDOW_H
实施文件:
#include "mainwindow.h"
#include <QString>
#include <QStringList>
int SentenceProcessor::getWordNumber(QString sentence) {
QStringList list = sentence.split(" ");
int len = 0;
foreach(QString str, list) {
len++;
}
return len;
}
int SentenceProcessor::getVowelNumber(QString sentence) {
int count = 0;
int len = sentence.size();
for (int i = 0; i < len; i++) {
if (sentence.data()[i] == 'a' || sentence.data()[i] == 'A' || sentence.data()[i] == 'e' || sentence.data()[i] == 'E' || sentence.data()[i] == 'i' ||
sentence.data()[i] == 'I' || sentence.data()[i] == 'o' || sentence.data()[i] == 'O' || sentence.data()[i] == 'u' || sentence.data()[i] == 'U') {
count++;
}
}
return count;
}
bool SentenceProcessor::isReversible(QString sentence) {
QStringList list1 = sentence.split(" ");
QStringList list2;
int count = 0;
bool reversible;
foreach(QString str, list1) {
list2.prepend(str);
}
for (int i = 0; i < list1.size(); i++) {
if (QString::compare(list1[i], list2[i], Qt::CaseInsensitive) == 0) {
count++;
}
}
if (count == list1.size()) {
reversible = true;
} else {
reversible = false;
}
return reversible;
}
QString SentenceProcessor::wordsReversed(QString sentence) {
QStringList list1 = sentence.split(" ");
QStringList list2;
foreach(QString str, list1) {
list2.prepend(str);
}
QString reversed = list2.join(" ");
return reversed;
}
QString SentenceProcessor::formatSentence(QString sentence) {
sentence.data()[0] = sentence.data()[0].toUpper();
int len = sentence.length();
if (sentence.data()[len - 1] != '.') {
sentence.append('.');
}
return sentence;
}
用于测试的 Main.cpp 文件:在这个文件中我添加了QApplication::quit();
#include "mainwindow.h"
#include <QApplication>
#include <QString>
#include <QStringList>
#include <QTextStream>
#include <QInputDialog>
#include <QMessageBox>
int main(int argc, char * argv[]) {
QApplication app(argc, argv);
QMessageBox::StandardButton response;
response = QMessageBox::question(0, "Ready for input", "Are you ready to enter a sentence?", QMessageBox::Yes | QMessageBox::No);
while (response == QMessageBox::Yes) {
SentenceProcessor s1;
bool ok;
QString text = QInputDialog::getText(0, "Sentence Processor", "Enter sentence", QLineEdit::Normal, 0, &ok);
QStringList list = text.split(" ");
if (ok) {
while (list.size() < 2) {
QMessageBox::information(0, "Error", "You need to enter at least two words");
text = QInputDialog::getText(0, "Sentence Processor", "Enter sentence", QLineEdit::Normal, 0, &ok);
if (!ok) {
QApplication::quit();
}else{
list = text.split(" ");
}
}
QString numWords = QString::number(s1.getWordNumber(text));
QString numVowels = QString::number(s1.getVowelNumber(text));
bool reversible = s1.isReversible(text);
QString wordsReverse = s1.wordsReversed(text);
QString senFormat = s1.formatSentence(text);
QString reversibleText;
if (reversible == false) {
reversibleText = "Your sentence is not reversible";
} else {
reversibleText = "Your sentence is reversible";
}
QMessageBox::information(0, "Sentence Processing", "Your sentence: " + text + "\n" +
"The number of words in your sentence is: " + numWords + "\n The number of vowels in your sentence: " +
numVowels + "\n" + reversibleText + "\n" + "Your sentence reversed is: " + wordsReverse + "\n" +
"Your sentence formatted is: " + senFormat);
response = QMessageBox::question(0, "Another Input", "Would you like to enter another sentence?", QMessageBox::Yes | QMessageBox::No);
}else{
QApplication::quit();
}
}
return app.exec();
}
崩溃的原因可能是您使用了QApplication::quit();
在代码中间,甚至在调用应用程序exec()
之前。 看起来你假设quit()
是你的 function 的退出点。它不是。 只有return
或 scope 的结尾是任何 function 的退出点,包括main()
。 您应该使用return
(并返回 0 或一些错误代码,如 1 或 -1,这取决于您)而不是您对QApplication::quit()
的所有调用,然后它可能会按您预期的那样工作。
由于您实际上并没有使用应用程序事件循环,因此您应该删除app.exec();
. 只需使用return 0;
反而。 使用app.exec()
您的应用程序将永远不会完成。
顺便说一句,这种设计 Qt 代码的方式是错误的。 理想情况下,您应该在应用程序的事件循环运行时执行您的代码。 当您调用exec()
时开始。 但是考虑到消息框有自己的事件循环这一事实,您的代码最终可能会真正起作用。 但是Qt apps绝对是错误的做法。 总的来说,你应该研究 Qt 应用程序中的事件循环是什么以及它是如何工作的。 您的代码看起来您一无所知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.