Important: Please read the Qt Code of Conduct -

[Solved] QLineEdit text() method causing app to crash

  • I've shown the dialog and the OK and Cancel buttons work, but the QLineEdit text() method causes my program to crash. Here's the code:

    @Ui::Dialog ui_d1;
    Dialog D1;

    if(D1.exec() == QDialog::Accepted)

            Temp1 = D1.lineEdit->text();
            // Some following code


    Temp1 is a QString variable and if I comment out the text() call and assign Temp1 = "2", e,g,, my program runs perfectly. Any ideas on this would be greatly appreciated. Thanks in advance.

  • It looks like you are trying to use auto-generated code and mixed it with your own code. But it looks quite wrong. Probably you have substituted the this pointer with the address of Dialog.

    You need to post the code of setupUi. Otherwise nobody can help.

  • If you try to alter GUI elements from an other class you should use the multiple inheritance way of using designer forms. Not the single inheritance (default) way. Check out the tutorials!

  • This is my dialog.h:

    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QtCore/QVariant>
    #include <QtWidgets/QAction>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QButtonGroup>
    #include <QtWidgets/QDialog>
    #include <QtWidgets/QDialogButtonBox>
    #include <QtWidgets/QHeaderView>
    #include <QtWidgets/QLabel>
    #include <QtWidgets/QLineEdit>


    class Ui_Dialog : public QDialog

    QDialogButtonBox *buttonBox;
    QLabel *label;
    QLabel *label_2;
    QLabel *label_3;
    QLabel *label_4;
    QLabel *label_5;
    QLabel *label_6;
    QLineEdit *lineEdit;

    // Ui_Dialog(QWidget *parent = 0);

    // QString lineEditText(void) const;

    // QString lineEditText(void) const {

    // return lineEdit->text();

    // }

    void setupUi(QDialog *Dialog)
        if (Dialog->objectName().isEmpty())
        Dialog->resize(838, 451);
        buttonBox = new QDialogButtonBox(Dialog);
        buttonBox->setGeometry(QRect(610, 320, 171, 32));
        label = new QLabel(Dialog);
        label->setGeometry(QRect(40, 30, 761, 16));
        QFont font;
        label_2 = new QLabel(Dialog);
        label_2->setGeometry(QRect(40, 60, 761, 16));
        label_3 = new QLabel(Dialog);
        label_3->setGeometry(QRect(40, 90, 761, 16));
        label_4 = new QLabel(Dialog);
        label_4->setGeometry(QRect(40, 120, 761, 16));
        label_5 = new QLabel(Dialog);
        label_5->setGeometry(QRect(40, 150, 761, 16));
        label_6 = new QLabel(Dialog);
        label_6->setGeometry(QRect(50, 290, 761, 16));
        lineEdit = new QLineEdit(Dialog);
        lineEdit->setGeometry(QRect(50, 330, 261, 20));
        QObject::connect(buttonBox, SIGNAL(accepted()), Dialog, SLOT(accept()));
        QObject::connect(buttonBox, SIGNAL(rejected()), Dialog, SLOT(reject()));
    } // setupUi
    void retranslateUi(QDialog *Dialog)
        Dialog->setWindowTitle(QApplication::translate("Dialog", "My Application", 0));
        label->setText(QApplication::translate("Dialog", "TextLabel", 0));
        label_2->setText(QApplication::translate("Dialog", "TextLabel", 0));
        label_3->setText(QApplication::translate("Dialog", "TextLabel", 0));
        label_4->setText(QApplication::translate("Dialog", "TextLabel", 0));
        label_5->setText(QApplication::translate("Dialog", "TextLabel", 0));
        label_6->setText(QApplication::translate("Dialog", "TextLabel", 0));
    } // retranslateUi


    namespace Ui {
    class Dialog: public Ui_Dialog {};
    } // namespace Ui


    #endif // DIALOG_H

  • and your constructor code??

  • Hi,
    Might found something:
    Ui::Dialog ui_d1;
    Dialog D1;
    is not valid!! What you tell the compiler is to allocate two classes, one a Ui::Dialog class and one a Dialog class. Then you request the Ui::Dialog class to setupUi function. That will work, it exists! But this DOES NOT setupUi of the Dialog (top level class). So what you see on your screen is the display of the ui_d1 variable. The D1 is NOT setup properly.
    Remove the Ui::Dialog ui_d1 variable, add the setupUi(this) in the constructor of the Dialog class and add the ui(new Ui::Dialog) to the constructor function argument list.
    That should do the trick. Just make sure you use public inheritance and the ui* should be available to other parts of your program.

  • I'm not clear on one of the instructions:

    add the ui(new Ui::Dialog) to the constructor function argument list.

    Could you explain, please?

  • Just adding setupUI (this) to the dialog constructor fixed the problem, along with just using the following declaration in my class procedure:

    Dialog *D1 = new Dialog;

    Thank you so much for your help. I really, really appreciate it. I was stumped.
    Thanks again!

  • Hi, just as an example:
    InPinClass::InPinClass(GU32 Channel_u32, QWidget *parent) :
    ui(new Ui::GroupBoxInPin)
    The constructor of a Form class in the cpp file. where the GroupBoxInPin is your Dialog class.
    Must add, that using only Dialog for your class name is a bit confusing. Better to add a post name to it, something like: DialogGetMyInfo.
    only Dialog is so similar to QDialog ;-)

  • Thanks. I have only 1 dialog so it's nothing significant. I use it twice so I just assign the label texts in the program. It is a Windows application that I'm updating that I offer freely on the web with an LGPL license. A Linux version also, as well as a Visual Basic and a C# version. I did the different versions to learn new programming languages. By far Qt is the most challenging. But the app I have runs data calculations that can take close to 1 minute on the Visual Basic and C# versions using Visual Studio, but almost instantly in Qt now that I know how to set the stack size in the .pro file for Qt. You wouldn't know anything about setting the stack size for Visual Basic or C# in Visual Studio, would you?

Log in to reply