Results 1 to 12 of 12

Thread: Update form when change in Pushbutton text

  1. #1
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Update form when change in Pushbutton text

    hi,

    I have one form named fin button i orm1 and this form contain a pushbutton which contain text Login. Once i clicked the login button, i can able to open a login window. Once i login successfully,i need to change the button text to Logout.i have changed that using settext(logout);. But This is getting changed only when i refresh that form1.

    Here is ma question, how can i update the form once the button text get changed.??


    Could you please help me to sort out this issue.?


    Thanks in advance..

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Update form when change in Pushbutton text

    QAbstractButton::setText() is the correct way to change the text of the button.

    What do you mean by, "But This is getting changed only when i refresh that form1" ? Perhaps you can post a small compilable program that demonstrates the problem.

  3. #3
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Update form when change in Pushbutton text

    Thanks for your reply

    This is the login click button handler in form 1

    Qt Code:
    1. void MainPage::LoginClickHandler()
    2. {
    3. LoginWindow *f2 = new LoginWindow;
    4. // this->hide();
    5. f2->show();
    6. f2->activateWindow(); // or form2->setActiveWindow() in Qt3
    7. f2->raise();
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    and in login form ,

    Qt Code:
    1. void LoginWindow::LoginClicked()
    2. {
    3. QString Password;
    4. QString StoredPassword;
    5.  
    6. StoredPassword.sprintf("%s",LoginPassword);
    7. Password =ui->QLinePassword->text();
    8.  
    9. //validate password
    10. int x = QString::compare(Password, StoredPassword); // x == 0 if correct
    11. if(x==0)
    12. {
    13. ui->QLabelIncorrectPass->setText("Login Successful");
    14. LoginFlag =1;
    15. this->close();
    16.  
    17. }
    18. else
    19. {
    20. ui->QLabelIncorrectPass->setText("Incorrect Password");
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    And i have added the text from login to logout in the constructor of mainpage.

    Once the Login window get opened,the mainpage also should be open and once login is successful,the result should be updated in the mainpage as well

    Is there any other way to do it.

    Thanks.
    Last edited by wysota; 11th June 2012 at 12:11. Reason: missing [code] tags

  4. #4
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Update form when change in Pushbutton text

    use simple signal slot system.. when use "click" the button, first change the form then set the button text

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Update form when change in Pushbutton text

    Or use a modal login dialog.

  6. #6
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Update form when change in Pushbutton text

    Thanks for your reply...

    will Update form 1 from form 2 will solve this issue?

    i am new to QT, so could you please clarify me with a piece of code.

    Thanks
    vinithr

  7. #7
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Update form when change in Pushbutton text

    Instead of using more than one forms you can use stacked widget where you easily moved to next widget once login is valid.
    So you might not have problems with forms. Simple by changing the widget index you move to next widget.

  8. #8
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Update form when change in Pushbutton text

    Hi,

    Thanks for your reply.

    In the case of signals and slots,i tried to add that button text after loading the page. In this case button text also changing once the login appears. And if i am clicking the cancel button in login form,the button text should be login only. This will not happen in the suggestion.

    My requirement is like, change the text of push button from other form or any other mechanism available in QT in the same window.


    Thanks in advance.
    vinithr

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Update form when change in Pushbutton text

    Use a modal dialog and change the push button on the main form from the main form. It's really quite straightforward and I am feeling generous:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class LoginDialog: public QDialog {
    5. Q_OBJECT
    6.  
    7. public:
    8. LoginDialog(QWidget *p = 0): QDialog(p) {
    9. pwEdit = new QLineEdit(this);
    10. QDialogButtonBox *buttonBox =
    11. connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    12. connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    13.  
    14. QVBoxLayout *layout = new QVBoxLayout(this);
    15. layout->addWidget(pwEdit);
    16. layout->addWidget(buttonBox);
    17. setLayout(layout);
    18. }
    19.  
    20. public slots:
    21. void accept() {
    22. if (pwEdit->text() == QString("xyzzy"))
    23. QDialog::accept();
    24. }
    25.  
    26. private:
    27. QLineEdit *pwEdit;
    28. };
    29.  
    30.  
    31. class MainWindow: public QMainWindow {
    32. Q_OBJECT
    33.  
    34. public:
    35. MainWindow(QWidget *p = 0): QMainWindow(p) {
    36. button = new QPushButton("Login", this);
    37. connect(button, SIGNAL(clicked()), SLOT(buttonClicked()));
    38.  
    39. setCentralWidget(button);
    40. }
    41.  
    42. public slots:
    43. void buttonClicked() {
    44. if (button->text() == "Login") {
    45. LoginDialog dialog;
    46. int result = dialog.exec();
    47. if (result == QDialog::Accepted) {
    48. // do login stuff
    49. button->setText("Logout");
    50. }
    51. }
    52. else {
    53. // do logout stuff
    54. button->setText("Login");
    55. }
    56. }
    57.  
    58. private:
    59. QPushButton *button;
    60. };
    61.  
    62. int main(int argc, char *argv[])
    63. {
    64. QApplication app(argc, argv);
    65.  
    66.  
    67. MainWindow m;
    68. m.show();
    69. return app.exec();
    70. }
    71. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to ChrisW67 for this useful post:

    vinithr (13th June 2012)

  11. #10
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Update form when change in Pushbutton text

    Thanks for your help with source. I will check this and let you know the result.


    Thanks Vinithr


    Added after 27 minutes:


    hi. Thanks for your reply.

    Now it is working as i expected.

    Thanks lot for your help

    regards
    vinithr
    Last edited by vinithr; 13th June 2012 at 13:14.

  12. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Update form when change in Pushbutton text

    It is the solution I suggested you adopt back in April. Please take the time to understand how it works.

  13. The following user says thank you to ChrisW67 for this useful post:

    vinithr (14th June 2012)

  14. #12
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Update form when change in Pushbutton text

    Thanks .. i understood the working of thsis application..


    Thanks
    Vinithr

Similar Threads

  1. Replies: 0
    Last Post: 15th August 2011, 19:09
  2. update form
    By hamidarr in forum Qt Programming
    Replies: 10
    Last Post: 25th June 2011, 21:22
  3. How to change QLabel value after Pushbutton pressed?
    By rdelgado in forum Qt Programming
    Replies: 4
    Last Post: 16th August 2010, 21:25
  4. Replies: 5
    Last Post: 12th March 2010, 21:43
  5. Help me to load one form over another form PushButton
    By wagmare in forum Qt Programming
    Replies: 7
    Last Post: 26th November 2008, 16:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.