Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MainWindow & QWidget: login
Forum Updated to NodeBB v4.3 + New Features

MainWindow & QWidget: login

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    glararan
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jake007
      wrote on last edited by
      #2

      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.


      Code is poetry

      1 Reply Last reply
      0
      • G Offline
        G Offline
        glararan
        wrote on last edited by
        #3

        and what with Login.cpp?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jake007
          wrote on last edited by
          #4

          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.


          Code is poetry

          1 Reply Last reply
          0
          • G Offline
            G Offline
            glararan
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • G Offline
                G Offline
                glararan
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  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?

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    glararan
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jake007
                      wrote on last edited by
                      #10

                      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...


                      Code is poetry

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved