QDialog exec()(段错误)
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
malloc 也会引起问题。该结构中的字符串无需构造即可使用。假设您的堆在那之后损坏了。
而不是 malloc 尝试...
...当然,当你完成它时删除。
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...
... and of course delete when you are done with it.
您正在堆栈上分配 QButtonGroup 服务组。可能不是你想做的事。
You are allocating the QButtonGroup serviceGroup on the stack. Probably not what you want to do.