c++ - Qt not getting text from lineedit -


qstring username = ui->lineedit->text(); qstring password = ui->lineedit_2->text(); qmessagebox failed; failed.setwindowflags(qt::framelesswindowhint);  if(username == "jon" && password == "12345") {     failed.settext("login failed. try again.");     failed.exec(); } else {     failed.settext(password);     failed.exec(); } 

using qt. sorry if has been asked before i'm quite new , couldn't find answer. don't understand i'm doing wrong. set username , password text inside line edits on ui. everytime click button function output of dialog text blank. how can the text read?

alright, ran tests , found got:

if set sort of signal slot connection calls function containg if/else statmentsthe program work. following program made:

dialog.h:

    #ifndef dialog_h     #define dialog_h      #include "ui_dialog.h"     #include <qdialog>      class dialogclass :  public qdialog, public ui::dialog     {         q_object      public:         dialogclass(qwidget *parent = 0);     public slots:         void check();     };      #endif // dialog_h 

dialog.cpp:

    #include "dialog.h"     #include <qmessagebox>      dialogclass::dialogclass(qwidget *parent) : qdialog(parent)     {         setupui(this);         connect(pushbutton, signal(clicked()), this, slot(check()));     }      void dialogclass::check()     {         qstring username = lineedit->text();         qstring password = lineedit_2->text();          qmessagebox failed;         failed.setwindowflags(qt::framelesswindowhint);          if(username == "jon" && password == "12345")         {             failed.settext("login failed. try again.");             failed.exec();         } else {             failed.settext(password);             failed.exec();         }     } 

and main.cpp

    #include <qapplication>     #include <qmessagebox>     #include "dialog.h"     int main(int argc, char *argv[])     {         qapplication a(argc, argv);         dialogclass w;         w.show();         return a.exec();     } 

bassically, make dialog class lot main function, mine has button dialog.ui has button. after that, use button establish signal slot connection ensures when button clicked, call check() function (which if/else algorithm make slot), checks see if username "jon", , password "12345".

why original implementation didn't work?

i sorry, not sure.

however, approach should work. add answer if ever figure out why if statements weren't picking up. however, approach should application working.

happy coding!


Comments