Getting segmentation fault when using Ui_LoginScreen().lineEdit->text();
-
Hello I am stuck and cannot continue further.
My application is about to read a database and then compare the input from a login interface where the user types his password and username. I have so far designed the login interface but I keep getting this segmentation fault whenever I try to run the code? I just want to compare the input with the values from the database. Why do I get this segmentation error and how can I solve it ?
Here is my code#include "LoginScreen.h" #include <QApplication> #include <QMessageBox> LoginScreen::LoginScreen(){ widget.setupUi(this); connect(widget.pushButton, SIGNAL(clicked()),this, SLOT(clickedSlot())); } LoginScreen::~LoginScreen(){} void LoginScreen::clickedSlot(){ QMessageBox msgBox; QString usernameFromInterface,passwordFromInterface; usernameFromInterface = Ui_LoginScreen().lineEdit->text();//ERROR OCCURS HERE }
-
Your ui_loginscreen object may be null. Check it.
-
How do I check if it is null ? This is the body that is generated. Should I declare it somewhere in the main maybe??
[code]/******************************************************************************** ** Form generated from reading UI file 'LoginScreen.ui' ** ** Created by: Qt User Interface Compiler version 5.5.1 ** ** WARNING! All changes made in this file will be lost when recompiling UI file! ********************************************************************************/ #ifndef UI_LOGINSCREEN_H #define UI_LOGINSCREEN_H #include <QtCore/QVariant> #include <QtWidgets/QAction> #include <QtWidgets/QApplication> #include <QtWidgets/QButtonGroup> #include <QtWidgets/QDialog> #include <QtWidgets/QHeaderView> #include <QtWidgets/QLabel> #include <QtWidgets/QLineEdit> #include <QtWidgets/QPushButton> QT_BEGIN_NAMESPACE class Ui_LoginScreen { public: QLineEdit *lineEdit; QLineEdit *lineEdit_2; QLabel *label; QLabel *label_2; QPushButton *pushButton; QLabel *label_3; void setupUi(QDialog *LoginScreen) { if (LoginScreen->objectName().isEmpty()) LoginScreen->setObjectName(QStringLiteral("LoginScreen")); LoginScreen->resize(693, 429); lineEdit = new QLineEdit(LoginScreen); lineEdit->setObjectName(QStringLiteral("lineEdit")); lineEdit->setGeometry(QRect(270, 120, 113, 20)); lineEdit_2 = new QLineEdit(LoginScreen); lineEdit_2->setObjectName(QStringLiteral("lineEdit_2")); lineEdit_2->setGeometry(QRect(270, 170, 113, 20)); lineEdit_2->setEchoMode(QLineEdit::Password); label = new QLabel(LoginScreen); label->setObjectName(QStringLiteral("label")); label->setGeometry(QRect(270, 100, 47, 13)); label_2 = new QLabel(LoginScreen); label_2->setObjectName(QStringLiteral("label_2")); label_2->setGeometry(QRect(270, 150, 47, 13)); pushButton = new QPushButton(LoginScreen); pushButton->setObjectName(QStringLiteral("pushButton")); pushButton->setGeometry(QRect(290, 210, 75, 23)); pushButton->setCheckable(true); label_3 = new QLabel(LoginScreen); label_3->setObjectName(QStringLiteral("label_3")); label_3->setGeometry(QRect(270, 30, 141, 61)); label_3->setTextFormat(Qt::RichText); label_3->setMargin(5); label_3->setIndent(5); retranslateUi(LoginScreen); QMetaObject::connectSlotsByName(LoginScreen); } // setupUi void retranslateUi(QDialog *LoginScreen) { LoginScreen->setWindowTitle(QApplication::translate("LoginScreen", "LoginScreen", 0)); label->setText(QApplication::translate("LoginScreen", "Username", 0)); label_2->setText(QApplication::translate("LoginScreen", "Password", 0)); pushButton->setText(QApplication::translate("LoginScreen", "Ok", 0)); label_3->setText(QApplication::translate("LoginScreen", "Welcome to SEP", 0)); } // retranslateUi }; namespace Ui { class LoginScreen: public Ui_LoginScreen {}; } // namespace Ui QT_END_NAMESPACE #endif // UI_LOGINSCREEN_H
-
Hi,
Because you're code is wrong. you are instantiating an object of type
Ui_LoginScreen
without initialising it. But even if you did you would not get the value you expect.I take it that your
widget
variable type isUi_LoginScreen
, correct ? If so, you should use it to retrieve the text from thelineEdit
widget.