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:
@MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::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[])
{
QApplication a(argc, argv);
QApplication::setStyle(new QPlastiqueStyle);QTextCodec* czech = QTextCodec::codecForName("Windows-1250"); czech->setCodecForCStrings(QTextCodec::codecForName("Windows-1250")); 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.
-
You are creating new MainWindow(s) in constructor.
You created recursion without end.Try this:
@MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
Login l;this->setVisible(false); l.show(); this->setVisible(true);
}@
first_time variable isn't, required, because it's always first time.
-
and what with Login.cpp?
-
Sorry, overlooked that part.
In if ( login.cpp), you only save value for you program and emit it to MainWindow or whatever you want to do with it.
After you close you widget ( with push button, X in top right corner ( or left)...), it'll be destroyed and removed by itself. -
So again...what i want...
I run application and i see only Login part. After i success login i see MainWindow.
So in code: Main.cpp run MainWindow -> MainWindow run QWidget with login if (add bool? to Login.h?) login success you see part MainWindow and never run again Login.
-
A usual way is:
add a slot login() to your main window
At the end of the MainWindow's contructor add
@
QTimer::singleShot(0, this, SLOT(login()));
@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[])
{
QApplication a(argc, argv);
QApplication::setStyle(new QPlastiqueStyle);QTextCodec* czech = QTextCodec::codecForName("Windows-1250"); czech->setCodecForCStrings(QTextCodec::codecForName("Windows-1250")); MainWindow mw; mw.show(); return a.exec();
}@
MainWindow.cpp
@MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
createActions();QTimer::singleShot(0, this, SLOT(login()));
}
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?
-
and cant anyone show me how to do it without any problem? Im begginner in Qt.
-
Take a look at some examples and tutorials.
For example "here":http://www.voidrealms.com/tutorials.aspx?filter=qt, very simple and good tutorials in my opinion.
Tutorial 5 are widgets...