gpt4 book ai didi

c++ - Qt5 在关闭时崩溃

转载 作者:行者123 更新时间:2023-11-30 04:03:43 28 4
gpt4 key购买 nike

我的项目有时会在关闭时崩溃,但并不可靠。当使用“取消”按钮关闭时,它崩溃的频率比使用转义键关闭时要少得多,这让我更加困惑。我有一个简单的查找对话框(较大项目的一小部分)作为测试用例的形式。

尝试使用 GDB 进行调试导致它永远不会崩溃。核心转储是垃圾(坏内存)。

下面是来源。我无法确定到底出了什么问题;我已经看到(稍作修改)双重释放、释放 NULL、释放一个从未存在过的指针等,我开始怀疑这是否部分是 Qt 的错。

标题:

#ifndef NIDE_FINDDIALOG_HPP
#define NIDE_FINDDIALOG_HPP

#include <QDialog>
#include <QString>
#include <QGridLayout>
#include <QCheckBox>
#include <QLineEdit>
#include <QPushButton>
#include <QWidget>

class FindDialog: public QDialog {
public:
bool regex, caseSensitive, inSelection, wholeWord, forward, wrap;
QString expr;

private:
struct {
QGridLayout *layout;
QCheckBox *regex, *caseSense, *select, *whole, *forward, *wrap;
QLineEdit *exprBox;
QPushButton *cancel, *find;
}ui;

void onCancel();
void onFind();

void onRegex();
void onCase();
void onSelect();
void onWhole();
void onForward();
void onWrap();

public:
FindDialog(QWidget *parent);
~FindDialog();
};

#endif/*NIDE_FINDDIALOG_HPP*/

来源:

#include <NIDE/FindDialog.hpp>

FindDialog::FindDialog(QWidget *parent): QDialog(parent),
regex(false), caseSensitive(true), inSelection(false), wholeWord(true),
forward(true), wrap(true), expr() {

ui.layout = new QGridLayout(this);
ui.regex = new QCheckBox("Regex", this);
ui.caseSense = new QCheckBox("Match case", this);
ui.select = new QCheckBox("In Selection", this);
ui.whole = new QCheckBox("Whole Word", this);
ui.forward = new QCheckBox("Forward", this);
ui.wrap = new QCheckBox("Wrap around", this);
ui.exprBox = new QLineEdit(this);
ui.cancel = new QPushButton("Cancel", this);
ui.find = new QPushButton("Find", this);

ui.regex->setChecked(regex);
connect(ui.regex, &QCheckBox::stateChanged, this, &FindDialog::onRegex);

ui.caseSense->setChecked(caseSensitive);
connect(ui.caseSense, &QCheckBox::stateChanged, this, &FindDialog::onCase);

ui.select->setChecked(inSelection);
connect(ui.select, &QCheckBox::stateChanged, this, &FindDialog::onSelect);

ui.whole->setChecked(wholeWord);
connect(ui.whole, &QCheckBox::stateChanged, this, &FindDialog::onWhole);

ui.forward->setChecked(forward);
connect(ui.forward, &QCheckBox::stateChanged, this, &FindDialog::onForward);

ui.wrap->setChecked(wrap);
connect(ui.wrap, &QCheckBox::stateChanged, this, &FindDialog::onWrap);

ui.exprBox->setPlaceholderText(tr("Find expr..."));
connect(ui.exprBox, &QLineEdit::returnPressed, this, &FindDialog::onFind);

connect(ui.cancel, &QPushButton::clicked, this, &FindDialog::onCancel);

ui.find->setDefault(true);
connect(ui.find, &QPushButton::clicked, this, &FindDialog::onFind);


ui.layout->addWidget(ui.regex, 0, 0);
ui.layout->addWidget(ui.caseSense, 0, 1);
ui.layout->addWidget(ui.select, 0, 2);
ui.layout->addWidget(ui.whole, 1, 0);
ui.layout->addWidget(ui.forward, 1, 1);
ui.layout->addWidget(ui.wrap, 1, 2);
ui.layout->addWidget(ui.exprBox, 2, 0, 1, 3);
ui.layout->addWidget(ui.cancel, 3, 1);
ui.layout->addWidget(ui.find, 3, 2);

setLayout(ui.layout);

setWindowTitle(tr("Find"));
}
FindDialog::~FindDialog() {
delete ui.layout;
delete ui.regex;
delete ui.caseSense;
delete ui.select;
delete ui.whole;
delete ui.forward;
delete ui.wrap;
delete ui.exprBox;
delete ui.cancel;
delete ui.find;
}

void FindDialog::onCancel() {
done(QDialog::Rejected);
}
void FindDialog::onFind() {
expr = ui.exprBox->text();
done(QDialog::Accepted);
}

void FindDialog::onRegex() { regex = ui.regex->isChecked(); }
void FindDialog::onCase() { caseSensitive = ui.caseSense->isChecked(); }
void FindDialog::onSelect() { inSelection = ui.select->isChecked(); }
void FindDialog::onWhole() { wholeWord = ui.whole->isChecked(); }
void FindDialog::onForward() { forward = ui.forward->isChecked(); }
void FindDialog::onWrap() { wrap = ui.wrap->isChecked(); }

主要内容:

#include <NIDE/FindDialog.hpp>

#include <QApplication>

int main(int argc, char **argv) {
QApplication *app = new QApplication(argc, argv);
FindDialog fd(NULL);

app->setApplicationName("NIDE");

fd.exec();
}

最佳答案

在避免使用 Q_OBJECT 宏时,您依赖于 Qt 的实现细节。连接到在没有Q_OBJECT 的类中声明 的方法是未定义的行为。它在特定版本的 Qt 中工作是一个令人愉快的巧合。

唉,你的代码做了很多不必要的事情。

  1. 无需在堆上保留 Ui 元素或 QApplication 实例。

  2. 没有理由使用所有的 onXxxx 方法,因为当它通过 exec() 显示时,您不会读取对话框的状态.您只关心对话被接受(或可能被拒绝)后的状态。

  3. 您可以利用 QDialog::acceptQDialog::r​​eject 插槽。

  4. 要使对话框在多个平台上看起来正确,您应该使用 QDialogBu​​ttonBox 而不是离散按钮。

  5. 最后,使用 QDialog::exec() 重新进入事件循环,让您认为您的代码是同步的,但实际上并非如此。这是很难找到错误的来源。您应该简单地show() 对话框并对单击 Find 按钮时它发出的 accepted 信号使用react。

下面的代码试图成为一个合理正确的解决方案。它适用于 Qt 4 和 Qt 5。

// Interface
#include <QDialog>
#include <QGridLayout>
#include <QCheckBox>
#include <QLineEdit>
#include <QDialogButtonBox>

#if QT_VERSION<QT_VERSION_CHECK(5,0,0)
#define Q_DECL_OVERRIDE
#endif

class FindDialog: public QDialog {
struct Ui {
QGridLayout layout;
QCheckBox regex, caseSense, select, whole, forward, wrap;
QLineEdit exprBox;
QDialogButtonBox buttonBox;
Ui(QWidget * widget);
} m_ui;
void get();
public:
bool regex, caseSensitive, inSelection, wholeWord, forward, wrap;
QString expr;

FindDialog(QWidget *parent = 0);
~FindDialog();
void set();
void done(int r) Q_DECL_OVERRIDE;
};

// Implementation
FindDialog::Ui::Ui(QWidget * widget) :
layout(widget),
regex(tr("Regex")),
caseSense(tr("Match case")),
select(tr("In Selection")),
whole(tr("Whole Word")),
forward(tr("Forward")),
wrap(tr("Wrap around")),
buttonBox(QDialogButtonBox::Cancel)
{
layout.addWidget(&regex, 0, 0);
layout.addWidget(&caseSense, 0, 1);
layout.addWidget(&select, 0, 2);
layout.addWidget(&whole, 1, 0);
layout.addWidget(&forward, 1, 1);
layout.addWidget(&wrap, 1, 2);
layout.addWidget(&exprBox, 2, 0, 1, 3);
layout.addWidget(&buttonBox, 3, 0, 1, 3);
exprBox.setPlaceholderText(tr("Find expr..."));
buttonBox.addButton(tr("Find"), QDialogButtonBox::AcceptRole);
}

FindDialog::FindDialog(QWidget *parent): QDialog(parent), m_ui(this),
regex(false), caseSensitive(true), inSelection(false), wholeWord(true),
forward(true), wrap(true)
{
set();
connect(&m_ui.buttonBox, SIGNAL(rejected()), SLOT(reject()));
connect(&m_ui.buttonBox, SIGNAL(accepted()), SLOT(accept()));
setWindowTitle("Find");
}

FindDialog::~FindDialog() {}

void FindDialog::done(int result)
{
get();
QDialog::done(result);
}

void FindDialog::get()
{
regex = m_ui.regex.isChecked();
caseSensitive = m_ui.caseSense.isChecked();
inSelection = m_ui.select.isChecked();
wholeWord = m_ui.whole.isChecked();
forward = m_ui.forward.isChecked();
wrap = m_ui.wrap.isChecked();
expr = m_ui.exprBox.text();
}

void FindDialog::set()
{
m_ui.regex.setChecked(regex);
m_ui.caseSense.setChecked(caseSensitive);
m_ui.select.setChecked(inSelection);
m_ui.whole.setChecked(wholeWord);
m_ui.forward.setChecked(forward);
m_ui.wrap.setChecked(wrap);
}

// Main
#include <QApplication>
#include <QMessageBox>

int main(int argc, char **argv) {
QApplication app(argc, argv);
app.setApplicationName("NIDE");
FindDialog fd;
fd.show();
#if QT_VERSION>=QT_VERSION_CHECK(5,0,0)
// This can't be done in Qt4 without using moc
QObject::connect(&fd, &QDialog::accepted, [&fd]{
QMessageBox::information(NULL, "Find",
QString("The user wants to find \"%1\"").arg(fd.expr));
});
#endif
return app.exec();
}

关于c++ - Qt5 在关闭时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24249775/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com