0

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);
}

1 Answer 1

2

Sometimes you need to run qmake to make it work. You can do that right from QtCreator.

3
  • Would commenting out my headers have somehow forced QMake to run? Also does QMake not run with Ctrl+R?
    – Serodis
    Commented Jan 11, 2012 at 2:53
  • Not sure about the first question. Just try to open menu Build -> run qmake
    – Kath
    Commented Jan 11, 2012 at 4:23
  • Kate, I must say, I thought that this was done automatically. Your answer is great, it seems that answer fixes many problems I have dealt with. I have completely recreated projects (re-added all parts to a new .pro) due to these type of issues! Thank you!
    – Serodis
    Commented Jan 13, 2012 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.