Results 1 to 8 of 8

Thread: Switching Form;

  1. #1
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Switching Form;

    Hi all I'm relatively new at this qt programming so please bear with me

    What I have been trying to do for hours now is close a login dialog and open a Mainwindow.

    On the login Dialog i have a button. What i want is for this button to close the login Dialog (logindialog) and open the main form (userwindow). here's my code so far:

    main.cpp:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "logindialog.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. LoginDialog w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 


    logindialog.h:

    Qt Code:
    1. #ifndef LOGINDIALOG_H
    2. #define LOGINDIALOG_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class LoginDialog;
    8. }
    9.  
    10. class LoginDialog : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit LoginDialog(QWidget *parent = 0);
    16. ~LoginDialog();
    17.  
    18. private:
    19. Ui::LoginDialog *ui;
    20.  
    21. private slots:
    22. void on_SubmitButton_clicked();
    23. };
    24.  
    25. #endif // LOGINDIALOG_H
    To copy to clipboard, switch view to plain text mode 


    userwindow.h:

    Qt Code:
    1. #ifndef USERWINDOW_H
    2. #define USERWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class UserWindow;
    8. }
    9.  
    10. class UserWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit UserWindow(QWidget *parent = 0);
    16. ~UserWindow();
    17.  
    18. private:
    19. Ui::UserWindow *ui;
    20. };
    21.  
    22. #endif // USERWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    logindialog.cpp:

    Qt Code:
    1. #include "logindialog.h"
    2. #include "ui_logindialog.h"
    3. #include <QtGui>
    4.  
    5. using namespace std;
    6.  
    7. LoginDialog::LoginDialog(QWidget *parent) :
    8. QDialog(parent),
    9. ui(new Ui::LoginDialog)
    10. {
    11. ui->setupUi(this);
    12. }
    13.  
    14. LoginDialog::~LoginDialog()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void LoginDialog::on_SubmitButton_clicked()
    20. {
    21. /* Code is here for checking username and password
    22.   Want the userwindow to open and the logindialog to close */
    23.  
    24.  
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

    userwindow.cpp:

    Qt Code:
    1. #include "userwindow.h"
    2. #include "ui_userwindow.h"
    3.  
    4. UserWindow::UserWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::UserWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. UserWindow::~UserWindow()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 


    I have tried loads of different things from loads of different posts on loads of different forums. Perhaps i'm doing something fundamentally wrong :S Any code snippets would be greatly appreciated!!


    Also on a side note, how come i have to use ui->label->settext() and most of the tutorials just use label->settext() ???

    Thanks for your time and trouble. Much appreciated!!

  2. #2
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Switching Form;

    hi,
    u need to create obj for UserWindow in LoginDialog;
    in LoginDialog.h
    Qt Code:
    1. #ifndef LOGINDIALOG_H
    2. #define LOGINDIALOG_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class LoginDialog;
    8. }
    9.  
    10. class LoginDialog : public QDialog
    11. {
    12. Q_OBJECT
    13. UserWindow *u;
    14. public:
    15. explicit LoginDialog(QWidget *parent = 0);
    16. ~LoginDialog();
    17.  
    18. private:
    19. Ui::LoginDialog *ui;
    20.  
    21. private slots:
    22. void on_SubmitButton_clicked();
    23. };
    24.  
    25. #endif // LOGINDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    and in LoginDialog.cpp


    Qt Code:
    1. #include "logindialog.h"
    2. #include "ui_logindialog.h"
    3. #include <QtGui>
    4.  
    5. using namespace std;
    6.  
    7. LoginDialog::LoginDialog(QWidget *parent) :
    8. QDialog(parent),
    9. ui(new Ui::LoginDialog)
    10. {
    11. ui->setupUi(this);
    12. }
    13.  
    14. LoginDialog::~LoginDialog()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void LoginDialog::on_SubmitButton_clicked()
    20. {
    21. this->close(); // close the logindialog
    22. u=new UserWindow; //create obj for UserWindow
    23. u->show(); // open userwindow
    24. }
    To copy to clipboard, switch view to plain text mode 

    thats it.

    and for
    Also on a side note, how come i have to use ui->label->settext() and most of the tutorials just use label->settext() ???
    when you create the form using ui; you need to use ui->label->setText(); but when you design the form using coding then you need to give label->setText();

    hope it helps
    Bala

  3. The following user says thank you to BalaQT for this useful post:

    Splatify (1st February 2011)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Switching Form;

    You could do either of these:
    • Launch the dialog modally and then create a separate main window object and show() it (all in main()). Look at the QDialog::exec() rather than show() method to get a modal dialog.
      Qt Code:
      1. #include <QtGui/QApplication>
      2. #include "logindialog.h"
      3.  
      4. int main(int argc, char *argv[])
      5. {
      6. QApplication a(argc, argv);
      7. LoginDialog w;
      8. w.exec();
      9.  
      10. MainWindow m;
      11. m.show();
      12.  
      13. return a.exec();
      14. }
      To copy to clipboard, switch view to plain text mode 
    • Launch the main window object which immediately shows a modal login dialog. You could use a number of ways to get this to happen.


    Really depends on your exact requirements.

  5. The following user says thank you to ChrisW67 for this useful post:

    Splatify (1st February 2011)

  6. #4
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Switching Form;

    Many Thanks to the both of you. I like the first solution more, as when I implement the second, the main form opens when the login form windows is closed, which is not ideal if you want some form of protection!!

    Also thanks for the bit about why 'ui' needs to be used... Like i said I'm new at this and getting my head around everything is taking a bit of time. Thanks again

  7. #5
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Switching Form;

    Just a quick question?

    The line
    Qt Code:
    1. UserWindow *u;
    To copy to clipboard, switch view to plain text mode 
    is this defining a variable? I'm confused! Thanks

  8. #6
    Join Date
    Jan 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Switching Form;

    I'm going to try my hand at answering this but just to test myself (I'm a newbie also ). Someone PLEASE correct me if I am wrong.

    Qt Code:
    1. 1. UserWindow *u;
    To copy to clipboard, switch view to plain text mode 

    is a forward declaration of a pointer variable "u" of type UserWindow. It is a variable which is seen by the compiler as a pointer so a full declaration is not necessary in the header.

  9. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Switching Form;

    It is the declaration of a private member variable called "u" that is a pointer to an object of type UserWindow. The type UserWindow must be declared earlier in the file either in full or as a forward declaration (which is all that is required for a pointer). As far as I can see, the code in BalaQt's post does not contain the necessary declaration.

    For this to compile you would need to add either:
    Qt Code:
    1. // include the declaration of the UserWindow class
    2. #include "userwindow.h"
    3. OR
    4. // a forward, incomplete declaration
    5. class UserWindow;
    To copy to clipboard, switch view to plain text mode 
    near the top of LoginDialog.h (adjust the names as needed).

  10. #8
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Switching Form;

    yes chris,
    As far as I can see, the code in BalaQt's post does not contain the necessary declaration.
    i forget to add that. Thanks for pointing out.

    @Splatify:
    UserWindow *u; here you are declaring the objects for the class UserWindow.

    Qt Code:
    1. void LoginDialog::on_SubmitButton_clicked()
    2. {
    3. this->close(); // close the logindialog
    4. UserWIndow *u=new UserWindow; //u need to add UserWindow.h in the top of the cpp file.
    5. u->show(); // open userwindow
    6. }
    To copy to clipboard, switch view to plain text mode 

    if u declare inside a slot or a block of code, like above, the scope is within the block. (i.e) u cannot access this class variable outside the block. In your case, you can simply use this, since you dont need this obj outside the block.

    but in some cases we may need a class obj or variable in our entire class functions or other class. In that case, we need to declare in the class declaration itself.

    Splatify/ tnowacoski : I recommend you to read the oop concepts. without this Qt is not cute

    pls go through the following link in your free time.
    http://doc.qt.nokia.com/4.7-snapshot...-concepts.html
    http://doc.qt.nokia.com/4.7-snapshot...practices.html

    and pls read a good c++ book (c++ primer)

    I found this link quiet useful, pls go through this
    http://silmor.de/41

    hope it helps,
    Bala
    Last edited by BalaQT; 2nd February 2011 at 05:59.

Similar Threads

  1. Pass variable from a form to another form
    By dark1988 in forum Qt Programming
    Replies: 5
    Last Post: 8th February 2011, 18:19
  2. Replies: 5
    Last Post: 12th March 2010, 21:43
  3. Help me to load one form over another form PushButton
    By wagmare in forum Qt Programming
    Replies: 7
    Last Post: 26th November 2008, 16:11
  4. Hiding a form and opening another form
    By anafor2004 in forum Newbie
    Replies: 1
    Last Post: 20th February 2008, 15:04
  5. Calling a new form from current form
    By webgenius in forum Qt Programming
    Replies: 7
    Last Post: 8th April 2007, 19:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.