MainWindow & QWidget: login
Hi i have one problem. I try to create Mini app with Login system…Login system is on QWidget. Main.cpp run MainWindow -> MainWindow hide and run QWidget -> if login -> MainWindow show.
Current code MainWindow:
- {
- bool first_time = true;
- if(first_time)
- {
- Login l;
- l.show();
- MainWindow mw;
- mw.setVisible(false);
- first_time = false;
- }
- ui->setupUi(this);
- }
Current code QWidget if Log in password = password from DB
- if(password == db_password)
- {
- Login l;
- l.hide();
- MainWindow mw;
- mw.show();
- }
Main.cpp
- int main(int argc, char* argv[])
- {
- MainWindow mw;
- mw.show();
- return a.exec();
- }
Where can be problem? If i run program it spam new QWidgets. And dont run how i want.
9 replies
A usual way is:
add a slot login() to your main window
At the end of the MainWindow’s contructor add
In your main method do as usual:
- MainWindow mw;
- mw.show();
- return app.exec();
As soon as the QApplication event loop is started with exec, the timer fires and the slot with the login dialog is executed.
Main.cpp
- int main(int argc, char* argv[])
- {
- MainWindow mw;
- mw.show();
- return a.exec();
- }
MainWindow.cpp
- {
- ui->setupUi(this);
- createActions();
- }
- void MainWindow::login()
- {
- Login l;
- this->setVisible(false);
- l.show();
- }
Login.cpp
- if(password == db_password)
- {
- Login l;
- l.hide();
- MainWindow mw;
- mw.show();
- }
When i run app it just flashes
Of course.
in login(), you create the Login dialog on the stack. Show shows it, but returns immediately and the method is finished. Being stack based, the Login object is in turn destroyed.
You want to exchange show() with exec() to make the call blocking and also block the rest of your application. Using it without being logged in wouldn’t be much usfeful, would it?
Take a look at some examples and tutorials.
For example here [voidrealms.com], very simple and good tutorials in my opinion.
Tutorial 5 are widgets…
You must log in to post a reply. Not a member yet? Register here!



