I was creating a very simple class as part of a much larger project. The class was as simple as:
class Login : public QDialog
{
Q_OBJECT
private:
QLineEdit* txtUser;
QLineEdit* txtPass;
void setupUI();
public:
Login(QWidget* parent = 0);
};
When attempting to create the setupUI()
function the line txtUser = new QLineEdit;
would crash. I tried changing a lot of things and nothing would help. I took the project, copied it, and removed everything except this class and a main.cpp
and there was no error. I returned to my original project (reloading Qt Creator) and it continued to fail. I began commenting out random self-written header files from the main in case of some sort of conflict. After commenting out each one, I would re-compile and run. Each time it would continue to fail. After the last one, it worked. Then, I re-enabled everything and it ran great once again.
So, my question is, what could cause a segfault of this type with such simple code? In addition what might my changes have done to fix it if no code was changed? Basically, if I get this problem to ever occur again and I'm sure there are no errors, what steps should I take?
Please keep in mind I am using Qt and Qt Creator within Windows.
Finally, for the sake of completion, here is the code within 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);
}