February 4, 2012

glararan glararan
Lab Rat
37 posts

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:

  1. MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow)
  2. {
  3.     bool first_time = true;
  4.     if(first_time)
  5.     {
  6.         Login l;
  7.         l.show();
  8.  
  9.         MainWindow mw;
  10.         mw.setVisible(false);
  11.  
  12.         first_time = false;
  13.     }
  14.  
  15.     ui->setupUi(this);
  16. }

Current code QWidget if Log in password = password from DB

  1.                 if(password == db_password)
  2.                 {
  3.                     Login l;
  4.                     l.hide();
  5.  
  6.                     MainWindow mw;
  7.                     mw.show();
  8.                 }

Main.cpp

  1. int main(int argc, char* argv[])
  2. {
  3.     QApplication a(argc, argv);
  4.     QApplication::setStyle(new QPlastiqueStyle);
  5.  
  6.     QTextCodec* czech = QTextCodec::codecForName("Windows-1250");
  7.     czech->setCodecForCStrings(QTextCodec::codecForName("Windows-1250"));
  8.  
  9.     MainWindow mw;
  10.     mw.show();
  11.    
  12.     return a.exec();
  13. }

Where can be problem? If i run program it spam new QWidgets. And dont run how i want.

9 replies

February 4, 2012

Jake007 Jake007
Robot Herder
248 posts

You are creating new MainWindow(s) in constructor.
You created recursion without end.

Try this:

  1. MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow)
  2. {
  3.         ui->setupUi(this);
  4.         Login l;
  5.  
  6.         this->setVisible(false);
  7.         l.show();
  8.         this->setVisible(true);
  9. }

first_time variable isn’t, required, because it’s always first time.

 Signature 

——————————————-
Code is poetry

February 4, 2012

glararan glararan
Lab Rat
37 posts

and what with Login.cpp?

February 4, 2012

Jake007 Jake007
Robot Herder
248 posts

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.

 Signature 

——————————————-
Code is poetry

February 4, 2012

glararan glararan
Lab Rat
37 posts

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.

February 4, 2012

Volker Volker
Robot Herder
5428 posts

A usual way is:

add a slot login() to your main window

At the end of the MainWindow’s contructor add

  1. QTimer::singleShot(0, this, SLOT(login()));

In your main method do as usual:

  1. MainWindow mw;
  2. mw.show();
  3. 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.

February 5, 2012

glararan glararan
Lab Rat
37 posts

Main.cpp

  1. int main(int argc, char* argv[])
  2. {
  3.     QApplication a(argc, argv);
  4.     QApplication::setStyle(new QPlastiqueStyle);
  5.  
  6.     QTextCodec* czech = QTextCodec::codecForName("Windows-1250");
  7.     czech->setCodecForCStrings(QTextCodec::codecForName("Windows-1250"));
  8.  
  9.     MainWindow mw;
  10.     mw.show();
  11.    
  12.     return a.exec();
  13. }

MainWindow.cpp

  1. MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow)
  2. {
  3.     ui->setupUi(this);
  4.     createActions();
  5.  
  6.     QTimer::singleShot(0, this, SLOT(login()));
  7. }
  8.  
  9. void MainWindow::login()
  10. {
  11.     Login l;
  12.     this->setVisible(false);
  13.     l.show();
  14. }

Login.cpp

  1.                 if(password == db_password)
  2.                 {
  3.                     Login l;
  4.                     l.hide();
  5.  
  6.                     MainWindow mw;
  7.                     mw.show();
  8.                 }

When i run app it just flashes

February 5, 2012

Volker Volker
Robot Herder
5428 posts

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?

February 5, 2012

glararan glararan
Lab Rat
37 posts

and cant anyone show me how to do it without any problem? Im begginner in Qt.

February 5, 2012

Jake007 Jake007
Robot Herder
248 posts

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…

 Signature 

——————————————-
Code is poetry

 
  ‹‹ How to license an application developed under QT      Multiple QDataStreams from different threads - Seg Fault ››

You must log in to post a reply. Not a member yet? Register here!