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. Adding a form that appears before the main window
Forum Updated to NodeBB v4.3 + New Features

Adding a form that appears before the main window

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 12.1k 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.
  • O Offline
    O Offline
    ommz
    wrote on last edited by
    #1

    Ihave already created my UI and it runs fine. However i want to add a form that appears before the main window when starting the program...sort of like a form that asks for a password for opening the app. How do I do this?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      There are many ways to accomplish that. You can run the new form from main.cpp before executing the main window. Or you can include running of this form in main window's constructor. Take a look at QDialog and friends (or, if you don't need interaction, QSplashScreen).

      (Z(:^

      1 Reply Last reply
      0
      • V Offline
        V Offline
        veeraps
        wrote on last edited by
        #3

        You may achieve this by showing a customized QDialog (to input user credentials) in main window constructor to get the password and based on that you can proceed further.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          I would not do that in the constructor, I would do that with a custom modal dialog directly in main.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

            Dear all,

            I am newbie to Qt. I am using 4.8.3, Windows 7, QtCreator 2.6. Heavily using QtCreator to design a user interface.

            Is there any tutorial to build a modal dialog (for login) using QtCreator 2.6, after I create a mainwindow.ui?
            Do I create login.ui (login.cpp and login.h) first, then include them into myproject.pro?
            How do I pass variable/ value between those cpp program/files?

            Thank you very much in advance.

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              [quote author="yupifourbi" date="1353303780"]Do I create login.ui (login.cpp and login.h) first, then include them into myproject.pro?
              How do I pass variable/ value between those cpp program/files?[/quote]

              Again, there are many valid ways to do it. Yeah, you can create the login.ui + c++ class in your project. Then, one solution would be to instantiate and run the dialog in main.cpp, before the MainWindow is shown:
              @
              Login log(0);
              if (log.exec() != QDialog::Accepted) {
              return 1; // Error, for example
              } else {
              MainWindow mw(0);
              mw.show();
              }

              return app.exec();
              @

              Although now that I wrote it I do have some doubts. Well, you can always try it out and report back if it fails.

              (Z(:^

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

                Dear all,

                Here is my code (heavily created using QtCreator).

                main.cpp
                @
                #include "mainwindow.h"
                #include "login.h"

                #include <QApplication>

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

                Login log(0);
                if (log.exec&#40;&#41; != QDialog::Accepted) {
                  return 1; // Error, for example
                } else {
                  MainWindow m(0);
                  m.show();
                }
                
                return a.exec&#40;&#41;;                
                

                }
                @

                login.h
                @
                #ifndef LOGIN_H
                #define LOGIN_H

                #include <QDialog>

                namespace Ui {
                class Login;
                }

                class Login : public QDialog
                {
                Q_OBJECT

                public:
                explicit Login(QWidget *parent = 0);
                int checkLogin();
                ~Login();

                private:
                Ui::Login *ui;
                };

                #endif // LOGIN_H

                @

                login.cpp
                @
                #include <QtGui>
                #include "login.h"
                #include "ui_login.h"

                Login::Login(QWidget parent) :
                QDialog(parent),
                ui (new Ui::Login)
                {
                ui->setupUi(this);
                /
                Connecting push button pbLogin --> checklogin() */
                QObject::connect(ui->pbLogin, SIGNAL(activated()), this, SLOT(checkLogin()));

                }

                int Login::checkLogin()
                {
                // pengguna = username.
                // katakunci = password.

                int _cl =1;
                if ( (!ui->lePengguna->text().isEmpty()) &&
                    (!ui->leKatakunci->text().isEmpty())) {
                        if((ui->lePengguna->text()=="admin") && (ui->leKatakunci->text()=="1234")) {
                            QMessageBox::about(this, tr("Login success"), tr("<p>Welcome...</p>"));
                            _cl = 0;
                        } else {
                            QMessageBox::about(this, tr("Login unsuccess"), tr("<p>Wrong password</p>"));
                            _cl = 1;
                        }
                 }
                 return _cl;
                

                }

                Login::~Login()
                {
                delete ui;
                }
                @

                mainwindow.cpp
                @
                #include <QtGui>

                #include "mainwindow.h"
                #include "ui_mainwindow.h"

                MainWindow::MainWindow(QWidget parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {
                ui->setupUi(this); /
                MainWindow() /
                /
                connecting menu About --> tampilkan About() */
                QObject::connect(ui->actionSiapa_Dimana, SIGNAL(activated()), this, SLOT(about()));

                }

                void MainWindow::about()
                {
                QMessageBox::about(this, tr("About UP4B - Siapa Dimana"),
                tr("<p>Versi 0.01 skeleton.</p>"));
                }

                MainWindow::~MainWindow()
                {
                delete ui;
                }
                @

                mainwindow.h
                @
                #ifndef MAINWINDOW_H
                #define MAINWINDOW_H

                #include <QMainWindow>

                namespace Ui {
                class MainWindow;
                }

                class MainWindow : public QMainWindow
                {
                Q_OBJECT

                public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();

                public slots:
                void about();

                private:
                Ui::MainWindow *ui;

                };

                #endif // MAINWINDOW_H
                @

                The problems are:

                1. When I run the program, it shows Login window as expected.

                2. After I entry admin (at lePengguna) and "anything" (at leKatakunci)
                  whether the password is correct or not the program disappeared without stopping at any QMessageBox, but it still running in the background, I must force quit the program.

                3. I would like also to ask: how to change the "ui name" in the login.cpp (that created by designer in QtCreator). Can I just type different name directly in coding pane? What is the parameter at designer in QtCreator? Because I am still confuse differentiating between ui of mainwindow and ui of login.

                Please help. Thanks in advance.

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  Oh it's in my code, probably one of the reasons I was in doubt.
                  @
                  Login log(0);
                  if (log.exec() != QDialog::Accepted) {
                  a.quit();
                  // or this:
                  a.metaObject().invokeMethod(&a, "quit", Qt::QueuedConnection);
                  } else {
                  MainWindow m(0);
                  m.show();
                  }

                  return a.exec&#40;&#41;;
                  

                  @

                  (Z(:^

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

                    Dear Sierdzio,

                    I modified with line #3, still no luck.

                    I just found out that in the Signals & Slots editor, there is
                    pbLogin, clicked(), Login, accept().

                    However, when I deleted that signals & slots editor, now the pbLogin not working at all.
                    Is connect at login.cpp, line 9-11 failed??
                    @
                    ui->setupUi(this);
                    /* Connecting push button pbLogin --> checklogin() */
                    QObject::connect(ui->pbLogin, SIGNAL(clicked()), this, SLOT(checkLogin()));
                    @

                    From my debugger said:
                    @
                    Debugging starts
                    Object::connect: No such slot Login::checkLogin()
                    Object::connect: (sender name: 'pbLogin')
                    Object::connect: (receiver name: 'Login')
                    @

                    previously I create void checkLogin () as slot.
                    But I confuse with return code so I change it as function int checkLogin();

                    Should I use emit ? How to accomplished that?

                    Thanks in advance.

                    1 Reply Last reply
                    0
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #10

                      Well, the message is clear. QPushButton::activated() does not exist. Use ::clicked() or ::toggled() instead.

                      (Z(:^

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

                        I change int checkLogin() as public slots.

                        Now my login.h
                        @
                        #ifndef LOGIN_H
                        #define LOGIN_H

                        #include <QDialog>

                        namespace Ui {
                        class Login;
                        }

                        class Login : public QDialog
                        {
                        Q_OBJECT

                        public:
                        explicit Login(QWidget *parent = 0);
                        ~Login();

                        public slots:
                        int checkLogin();

                        private:
                        Ui::Login *ui;
                        };

                        #endif // LOGIN_H
                        @

                        The checkLogin is called and working now.

                        When success login with admin and 1234, QMessageBox "Welcome..." is displayed. But the mainwindow did not appear...

                        When not success login, QMessageBox "Wrong password." is displayed. The login window and mainwindow disappeared, but the program is still running.

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

                          Dear all,

                          Here my main.cpp
                          @
                          #include "mainwindow.h"
                          #include "login.h"

                          #include <QApplication>
                          #include <QMessageBox>

                          int main(int argc, char *argv[])
                          {
                          QApplication a(argc, argv);
                          QMessageBox msgBox;

                          Login log(0);
                          if (log.exec&#40;&#41; != QDialog::Accepted) {
                              msgBox.setText("Will stop the application");
                              msgBox.exec&#40;&#41;;
                              a.quit(); // BUT, the program is still running. Need to force quit.
                          } else {
                              msgBox.setText("will call the MainWindow");
                              msgBox.exec&#40;&#41;;
                              MainWindow m; 
                              m.show();  // BUT, there is no MainWindow ?! 
                          }
                          
                          msgBox.setText("Will execute a.exec&#40;&#41;");
                          msgBox.exec&#40;&#41;;
                          return a.exec&#40;&#41;;
                          

                          }
                          @

                          I try:
                          a. wrong username and password combination. The program shows "Will stop the application.", but the program is still running.

                          b. correct combination. The program shows "will call the MainWindow", but the mainwindow only flashed once the gone.

                          Both a and b, then program continue to display "will execute a.exec()".

                          Is it because there are many files (mainwindow.cpp, login.cpp), instead altogether in 1 file mainwindow?

                          Still don't know how to solve it. Please help.

                          1 Reply Last reply
                          0
                          • sierdzioS Offline
                            sierdzioS Offline
                            sierdzio
                            Moderators
                            wrote on last edited by
                            #13

                            App is probably not quitting because quit() is called too early (before the event loop kicks in in a.exec()).

                            The MainWindow flash could be due to the fact that you instantiate it inside the block (line 20) and then it goes out of scope in line 22 and is destroyed. Declare the MW outside the block or as a pointer.

                            (Z(:^

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

                              Dear Sierdzio my man!

                              my main.cpp now works as charm, million thanks.
                              @
                              #include "mainwindow.h"
                              #include "login.h"

                              #include <QApplication>
                              #include <QMessageBox>

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

                              QMessageBox msgBox;
                              
                              MainWindow m;
                              m.show();
                              
                              Login log(0);
                              if (log.exec&#40;&#41; != QDialog::Accepted) {
                                  msgBox.setText("Wrong credential. The program will stop.");
                                  msgBox.exec&#40;&#41;;
                                  a.quit();
                              } else {
                                  msgBox.setText("Ok. Let's go baby!");
                                  msgBox.exec&#40;&#41;;
                                  a.exec&#40;&#41;;
                              }
                              
                              return 0;
                              

                              }
                              @

                              Is it ok to say just 'return 0;'? As I don't pass anything else after the App/ program ends.
                              @
                              return 0;
                              @

                              1 Reply Last reply
                              0
                              • sierdzioS Offline
                                sierdzioS Offline
                                sierdzio
                                Moderators
                                wrote on last edited by
                                #15

                                [quote author="yupifourbi" date="1353399194"]
                                Is it ok to say just 'return 0;'? As I don't pass anything else after the App/ program ends.[/quote]

                                Interesting approach. It's probably alright, if the dialog works OK, then it should be fine.

                                (Z(:^

                                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