Login screen (How to?)
-
Hi! I designed my Qt Gui application using QtDesigner and in order to make diverse layers inside the application (login, main menu, settings, etc.) I made diverse QWidgets for each layer and depending which button inside the GUI was pressed the layers where showing or hiding... Now I have to make all this cool buttons functional and I have to make the login screen usable but I don't know how, in my login screen I have 4 elements: 1: the logo, 2&3: Username and Password QLineEdits and 4: The "Enter" button. I have to make all those funcitional.
Thanks in advance:
Carlos.
-
Hi,
What are you going to validate your username/password ? Database ? Local file ?
-
Just a string, the username and pasword wont be changed
Just to put a note here - if you use a C++ string for the username and password suppose like this:
const char * password = "my very secret password";
No one can stop me (or anyone else) from loading your application/library in the disassembler and extracting that string. So do consider this, when thinking about passwords.
-
Yeah I know that it may not be really secure but I just want it to work the easiest way posible, anyways I still dont know how to make the username / password validation system
-
Well since it's only a very light login, put the logic in the widget and emit a signal when the login is successful. Then in your main window react on that signal and do the changes needed.
-
Well since it's only a very light login, put the logic in the widget and emit a signal when the login is successful. Then in your main window react on that signal and do the changes needed.
-
Do you mean the comparison logic ?
-
Something like
void MyLoginWidget::onOkButtonClicked() { if (_usernameLineEdit->text() == MyValidUserName && _passwordLineEdit->text() == MyTopSecretPassword) { emit loginSuccessful(); } else ( QMessageBox::warning(this, tr("Error"), tr("Invalid username/password combination"); _usernameLineEdit->clear(); _passwordLineEdit->clear(); } }
WARNING: this is just a quick and dirty sample code.
-
Something like
void MyLoginWidget::onOkButtonClicked() { if (_usernameLineEdit->text() == MyValidUserName && _passwordLineEdit->text() == MyTopSecretPassword) { emit loginSuccessful(); } else ( QMessageBox::warning(this, tr("Error"), tr("Invalid username/password combination"); _usernameLineEdit->clear(); _passwordLineEdit->clear(); } }
WARNING: this is just a quick and dirty sample code.
-
No, in the login widget. The MainWindow widget should only react on the loginSuccessful signal.
-
No, in the login widget. The MainWindow widget should only react on the loginSuccessful signal.
-
Ok… So you are trying to have everything in only once widget. Not a good idea. You should following the "Single Responsibility" paradigm. Create one widget per "complex" widget you will have in your main window. Otherwise you will end up with a monster class that does way to much things for its own good and yours to maintain.
-
@SGaist Yeah, I knew that my weird distribution would bring problems hahah. So I'll have to change it. Can you tell me which is the best distribution since I want that EVERYTHING happens in just one window (When I have the project finished I will embed it) ?
-
Creating several widgets doesn't mean to you will have one application window per widget. What I'm suggesting is to separate things code-wise. You will still have only one MainWindow in the end containing everything but cleanly separated from a coding point of view.
-
@SGaist Ok, I've created a new project > application > QtWidgets application then it created the "MainWindow" class, "MainWindow" header and "MainWindow" Ui.
So for each "screen" I have to create a new C++ class ? Or... How? I don't understand the way to create widgets... -
Yes, create a new C++ class. You will have the option to "pre-populate" it for design development. You should take the time to look at Qt Creator/Designer and Qt's tutorial about how to create UI with Designer.