QDialog exec()(段错误)

发布于 2024-10-20 03:30:29 字数 1853 浏览 1 评论 0原文

我的 QDialog 在关闭后导致段错误。我在使用它之前对结构进行了 malloc,设置的类型为 PSETTINGS,并且是私有变量。

MainWindow 类:(段错误发生在 settingsDiag->exec() 中)

Settings *settingsDiag = new Settings(this);

settingsDiag->exec();

设置类:

在头文件中:

typedef struct ConnSettings {
    ConnSettings():ipAddr(""), alias("Local"), port(8000), isClient(false){}
    QString ipAddr;
    QString alias;
    int port;
    bool isClient;
} SETTINGS, *PSETTINGS;

在 CPP 文件中:

Settings::Settings(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Settings)
{
    ui->setupUi(this);

    QButtonGroup serviceGroup(ui->serviceBox);
    QValidator *validPort = new QRegExpValidator(QRegExp("^\\d*$"), this);
    QValidator *validIp = new QRegExpValidator(QRegExp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"), this);

    serviceGroup.addButton(ui->clientButton);
    serviceGroup.addButton(ui->serverButton);

    connect(ui->okCancel, SIGNAL(accepted()), this, SLOT(storeSettings()));
    connect(ui->clientButton, SIGNAL(toggled(bool)), this, SLOT(enableDisableClient(bool)));

    ui->portText->setValidator(validPort);
    ui->ipText->setValidator(validIp);
}

Settings::~Settings() {
    delete ui;
}

void Settings::storeSettings() {
    settings = (PSETTINGS)malloc(sizeof(SETTINGS));
    settings->port = ui->portText->text().toInt();

    if((settings->isClient = ui->clientButton->isChecked())) {
        settings->ipAddr = ui->ipText->text();
        settings->alias = ui->aliasText->text();
    }
}

PSETTINGS Settings::getSettings() {
    return settings;
}

void Settings::enableDisableClient(bool client) {
    ui->clientBox->setEnabled(client);
}

谢谢: )

My QDialog is causing a seg fault after it closes. I malloc my struct before using it, settings is of type PSETTINGS and is a private variable.

MainWindow Class:(Seg Fault happens in settingsDiag->exec())

Settings *settingsDiag = new Settings(this);

settingsDiag->exec();

Settings Class:

In header file:

typedef struct ConnSettings {
    ConnSettings():ipAddr(""), alias("Local"), port(8000), isClient(false){}
    QString ipAddr;
    QString alias;
    int port;
    bool isClient;
} SETTINGS, *PSETTINGS;

In CPP file:

Settings::Settings(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Settings)
{
    ui->setupUi(this);

    QButtonGroup serviceGroup(ui->serviceBox);
    QValidator *validPort = new QRegExpValidator(QRegExp("^\\d*$"), this);
    QValidator *validIp = new QRegExpValidator(QRegExp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"), this);

    serviceGroup.addButton(ui->clientButton);
    serviceGroup.addButton(ui->serverButton);

    connect(ui->okCancel, SIGNAL(accepted()), this, SLOT(storeSettings()));
    connect(ui->clientButton, SIGNAL(toggled(bool)), this, SLOT(enableDisableClient(bool)));

    ui->portText->setValidator(validPort);
    ui->ipText->setValidator(validIp);
}

Settings::~Settings() {
    delete ui;
}

void Settings::storeSettings() {
    settings = (PSETTINGS)malloc(sizeof(SETTINGS));
    settings->port = ui->portText->text().toInt();

    if((settings->isClient = ui->clientButton->isChecked())) {
        settings->ipAddr = ui->ipText->text();
        settings->alias = ui->aliasText->text();
    }
}

PSETTINGS Settings::getSettings() {
    return settings;
}

void Settings::enableDisableClient(bool client) {
    ui->clientBox->setEnabled(client);
}

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2024-10-27 03:30:29

malloc 也会引起问题。该结构中的字符串无需构造即可使用。假设您的堆在那之后损坏了。

而不是 malloc 尝试...

settings = new SETTINGS();

...当然,当你完成它时删除。

The malloc is causing problems too. The strings in that struct get used without being constructed. Assume your heap corrupted after that point.

Instead of malloc try...

settings = new SETTINGS();

... and of course delete when you are done with it.

死开点丶别碍眼 2024-10-27 03:30:29

您正在堆栈上分配 QButtonGroup 服务组。可能不是你想做的事。

You are allocating the QButtonGroup serviceGroup on the stack. Probably not what you want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文