PDA

View Full Version : Update form when change in Pushbutton text



vinithr
23rd April 2012, 17:46
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..

ChrisW67
23rd April 2012, 19:10
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.

vinithr
23rd April 2012, 19:37
Thanks for your reply

This is the login click button handler in form 1


void MainPage::LoginClickHandler()
{
LoginWindow *f2 = new LoginWindow;
// this->hide();
f2->show();
f2->activateWindow(); // or form2->setActiveWindow() in Qt3
f2->raise();

}

and in login form ,


void LoginWindow::LoginClicked()
{
QString Password;
QString StoredPassword;

StoredPassword.sprintf("%s",LoginPassword);
Password =ui->QLinePassword->text();

//validate password
int x = QString::compare(Password, StoredPassword); // x == 0 if correct
if(x==0)
{
ui->QLabelIncorrectPass->setText("Login Successful");
LoginFlag =1;
this->close();

}
else
{
ui->QLabelIncorrectPass->setText("Incorrect Password");
}
}

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.

zgulser
23rd April 2012, 20:07
use simple signal slot system.. when use "click" the button, first change the form then set the button text

ChrisW67
24th April 2012, 01:21
Or use a modal login dialog.

vinithr
24th April 2012, 05:50
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

karthic
27th April 2012, 14:26
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.

vinithr
13th June 2012, 05:54
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

ChrisW67
13th June 2012, 09:33
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:


#include <QtGui>
#include <QDebug>

class LoginDialog: public QDialog {
Q_OBJECT

public:
LoginDialog(QWidget *p = 0): QDialog(p) {
pwEdit = new QLineEdit(this);
QDialogButtonBox *buttonBox =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(pwEdit);
layout->addWidget(buttonBox);
setLayout(layout);
}

public slots:
void accept() {
if (pwEdit->text() == QString("xyzzy"))
QDialog::accept();
}

private:
QLineEdit *pwEdit;
};


class MainWindow: public QMainWindow {
Q_OBJECT

public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
button = new QPushButton("Login", this);
connect(button, SIGNAL(clicked()), SLOT(buttonClicked()));

setCentralWidget(button);
}

public slots:
void buttonClicked() {
if (button->text() == "Login") {
LoginDialog dialog;
int result = dialog.exec();
if (result == QDialog::Accepted) {
// do login stuff
button->setText("Logout");
}
}
else {
// do logout stuff
button->setText("Login");
}
}

private:
QPushButton *button;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);


MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"

vinithr
13th June 2012, 13:14
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

ChrisW67
14th June 2012, 00:52
It is the solution I suggested you adopt back in April. Please take the time to understand how it works.

vinithr
14th June 2012, 06:56
Thanks .. i understood the working of thsis application..


Thanks
Vinithr