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. Login window
QtWS25 Last Chance

Login window

Scheduled Pinned Locked Moved General and Desktop
17 Posts 10 Posters 8.5k Views
  • 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.
  • N Offline
    N Offline
    ninio
    wrote on last edited by
    #1

    Hi, I'm trying to do a login window wich must to be closed and open the main window. When user achieves the login I do:

    @MainWindow mw;
    mw.show();@

    But the new window does not appear. What am I doing wrong or what do I have to do?.

    Thank you.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pushpendrak
      wrote on last edited by
      #2

      if you are creating window inside some function then it is wrong as the window get created and shown, immediately it will get destroyed as the "mw" scope is inside that function only.
      @
      int main(...)
      {
      QApplication app(...);
      Mainwindow mw;
      mw.show();
      //wrong method
      return 0;
      // correct method
      return app.exec();
      /* here it will keep running as a thread so you will able to view the mainwindow, if return is some 0,1 etc. then it will break and you will not able to see the windows created, as it got destroyed out side the scope*/
      }@

      PushpendraK

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mario84
        wrote on last edited by
        #3

        There's two simple ways:

        1. Open your Login-Widget in main() before showing MainWindow (both from main())
        2. Show MainWindow and Open the Login-Widget from inside MainWindows constructor. (MainWindow itself will not be displayed until the constructor is finished)
        1 Reply Last reply
        0
        • N Offline
          N Offline
          ninio
          wrote on last edited by
          #4

          Finally I did:

          @MainWindow* mw = new MainWindow;
          this->close();
          mw->show();@

          Creating the window at the heap works. Thank you for the answers.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            ReshmaS
            wrote on last edited by
            #5

            @ninio:could you post your code here,. it might be of some help to me. Thanks.

            Reshma

            1 Reply Last reply
            0
            • JeroentjehomeJ Offline
              JeroentjehomeJ Offline
              Jeroentjehome
              wrote on last edited by
              #6

              Hi Ninio,
              Though it might work like that you have a potential memory leak at hand. When the new MainWindow is created it will give you the pointer to it so you can reverence it later on in your program. If the new operator is inside your logInWindow the pointer will be destroyed when the LogInWindow function is closed making the MainWindow unreachable by your code! The mw pointer should then be a public MainWindow pointer.
              When the pointer is not accessable anymore the memory can't be released until the program shutdown. When a new logIn Window is created a complete new mainwindow will appear.
              Might be better to use a parent pointer in the LogInWindow to alter the mainWindow show/hide options.
              Greetz

              Greetz, Jeroen

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                Actually, the window will be accessible via the QApplication object. You are right in principle, but things are not as bad as they seem :-)

                1 Reply Last reply
                0
                • JeroentjehomeJ Offline
                  JeroentjehomeJ Offline
                  Jeroentjehome
                  wrote on last edited by
                  #8

                  Ah, oke, learned something new there. Nice, but still it is bad pratice to create a new object and don't keep the pointer at hand ;-)

                  Greetz, Jeroen

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    issam
                    wrote on last edited by
                    #9

                    Hi all,
                    I tried to resolve the problem by writing a code example: (Connecting to a PostgreSQL data base)

                    create a login window

                    create a data base connection

                    show the login window

                    enter the username and the passwd (repeat until the login is accepted or the operation is canceled)

                    login succeeded ==> create and show the main window

                    @
                    int main(int argc, char *argv[])
                    {
                    QApplication app(argc, argv);

                    LoginWindow Lwindow;

                    /* accepted = 1; rejected = 0; */
                    int result;
                    bool pass = false;
                    int response;

                    QSqlDatabase Dbase;
                    Dbase = QSqlDatabase::addDatabase("QPSQL");
                    Dbase.setHostName("localhost");
                    Dbase.setDatabaseName("database_name");

                    while(!pass)
                    {
                    Lwindow.exec();
                    result = Lwindow.result();
                    if(result == 0)
                    {
                    pass = true;
                    Lwindow.done(0);
                    return 0;
                    }
                    else
                    {
                    QString user = Lwindow.getUser();
                    QString password = Lwindow.getPassword();
                    Dbase.setUserName(user);
                    Dbase.setPassword(password);
                    pass = Dbase.open();

                    /* if(m_dbase.lastError().isValid()) */
                    if(!pass)
                    {
                    response = QMessageBox::critical(NULL, "Data Base Access Problem",Dbase.lastError().text(),QMessageBox::Cancel|QMessageBox::Retry);
                    if(response == QMessageBox::Cancel)
                    {
                    pass = true;
                    return 0;
                    }
                    }
                    }
                    }

                    MainWindow Mwindow(Dbase);
                    Mwindow.setGeometry(100,100,500,600);
                    Mwindow.show();
                    return app.exec();
                    }

                    @

                    http://www.iissam.com/

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      [quote author="issam" date="1348049464"]Hi all,
                      I tried to resolve the problem by writing a code example like this :
                      [/quote]

                      Nice. And what was the result of that attempt? Did it work? If it did: congrats. If it did not: what failed exactly? What did you expect?

                      1 Reply Last reply
                      0
                      • JeroentjehomeJ Offline
                        JeroentjehomeJ Offline
                        Jeroentjehome
                        wrote on last edited by
                        #11

                        If you ask me it should work just fine. Maybe a bit of comments to add, but looks fine to me. Did it work??

                        Greetz, Jeroen

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          issam
                          wrote on last edited by
                          #12

                          It works well ! ;)

                          "download...":http://www.iissam.com/dbases/index.html

                          http://www.iissam.com/

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andre
                            wrote on last edited by
                            #13

                            In that case, please go and edit your first post in this topic to add a [Solved] tag to the title.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              Macro
                              wrote on last edited by
                              #14

                              [quote author="issam" date="1348128467"]
                              It works well ! ;)

                              "download...":http://www.iissam.com/dbases/index.html
                              [/quote]

                              Thank you....

                              1 Reply Last reply
                              0
                              • Y Offline
                                Y Offline
                                yupifourbi
                                wrote on last edited by
                                #15

                                Dear issam,

                                Where is the Lwindow definition? Do you separate into lwindow.cpp and lwindow.h?

                                Thanks in advance.

                                1 Reply Last reply
                                0
                                • I Offline
                                  I Offline
                                  issam
                                  wrote on last edited by
                                  #16

                                  Yes in loginwindow.h and LoginWindow.cpp :)

                                  http://www.iissam.com/

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    sinant
                                    wrote on last edited by
                                    #17

                                    Can you reupload the source code :) ?

                                    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