【发布时间】:2012-01-11 02:45:37
【问题描述】:
作为一个更大项目的一部分,我正在创建一个非常简单的类。这个类很简单:
class Login : public QDialog
{
Q_OBJECT
private:
QLineEdit* txtUser;
QLineEdit* txtPass;
void setupUI();
public:
Login(QWidget* parent = 0);
};
当尝试创建setupUI() 函数时,txtUser = new QLineEdit; 行会崩溃。我尝试改变很多东西,但没有任何帮助。我接受了这个项目,复制了它,并删除了除此类和main.cpp 之外的所有内容,并且没有错误。我回到了我原来的项目(重新加载 Qt Creator),它继续失败。我开始从主文件中注释掉随机的自写头文件,以防出现某种冲突。在注释掉每一个之后,我会重新编译并运行。每次它都会继续失败。在最后一个之后,它起作用了。然后,我重新启用了一切,它再次运行良好。
那么,我的问题是,使用如此简单的代码,什么会导致这种类型的段错误?此外,如果没有更改代码,我的更改可能会做什么来修复它?基本上,如果我再次遇到这个问题并且我确定没有错误,我应该采取什么步骤?
请记住,我在 Windows 中使用 Qt 和 Qt Creator。
最后,为了完整起见,这里是setupUI()内的代码:
void Login::setupUI()
{
QVBoxLayout* main = new QVBoxLayout;
QHBoxLayout* userBox = new QHBoxLayout;
QHBoxLayout* passBox = new QHBoxLayout;
txtUser = new QLineEdit;
txtPass = new QLineEdit;
userBox->addWidget(new QLabel("User Name:"));
userBox->addWidget(txtUser);
passBox->addWidget(new QLabel("Password:"));
txtPass->setEchoMode(QLineEdit::Password);
passBox->addWidget(txtPass);
main->addLayout(userBox);
main->addLayout(passBox);
setLayout(main);
}
【问题讨论】:
标签: qt segmentation-fault qt-creator