Results 1 to 5 of 5

Thread: Handling multiple forms

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2008
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Handling multiple forms

    Hello everybody . I'm new to this forum ( because it's the first time for me using QT but i'm pretty sure i'll be using it regularly from now ) . I followed a couple of tutorials, wrote some simple GUI applications but I could'nt find an answer for my problem .

    I have to ui's ( dlg1.ui and dlg2.ui ). dlg1.ui is the main window of my application(project type: MainWindow) , with the name Form1 , and dlg2.ui is another window( project type Widget ) .

    Here is how my projects files look like .
    main.cpp
    Qt Code:
    1. #include <qapplication>
    2. #include "app.h"
    3.  
    4. int main(int argc,char *argv[])
    5. {
    6. QApplication app(argc,argv);
    7. App1 dialog;
    8. dialog.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    app.h
    Qt Code:
    1. #ifndef APP_H
    2. #define APP_H
    3.  
    4. #include "ui_dlg1.h"
    5. #include "ui_dlg2.h"
    6.  
    7. class App1:public QMainWindow,private Ui::Form1
    8. {
    9. Q_OBJECT
    10. public:
    11. App1();
    12. public slots:
    13. void button_clicked();
    14. };
    15.  
    16. class App2:public QWidget,private Ui::Form2
    17. {
    18. Q_OBJECT
    19. public:
    20. App2(QWidget *parent = 0);
    21. };
    22.  
    23. #endif
    To copy to clipboard, switch view to plain text mode 
    app.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "app.h"
    3.  
    4. App1::App1()
    5. {
    6. setupUi(this);
    7. connect(pushButton,SIGNAL(clicked()),this,SLOT(button_clicked()));
    8. }
    9.  
    10. App2::App2(QWidget *parent)
    11. {
    12. setupUi(this);
    13. }
    14.  
    15. void App1::button_clicked()
    16. {
    17. App2 form2(this);
    18. form2.show();
    19. }
    To copy to clipboard, switch view to plain text mode 

    This code works just fine but when I press the button , Form2 appears for a very very short time and then it's being destroyed . Why this happens ? And how can I correct it so that Form2 shows as a modal window ( no interraction with Form1 being possible till Form2 is being closed ) .

    Furthermore, how can I make two forms communicate with each other ? My idea was a global variabiles in app.cpp that each form can see .

    I hope I was clear enough . Have a great day !

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Handling multiple forms

    you create form2 on the stack. When the scope is left, it is destroyed (and thus disappears.)

    You have two possibilites.
    i) Make it a modal dialog (ie inherit from QDialog and call exec())
    ii) allocate form2 on the heap (using new, and ideally passing a QObject parent object to make sure the object gets deleted in time)

    HTH

  3. #3
    Join Date
    Dec 2008
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling multiple forms

    Thanks a lot caduel, i used dynamic allocation and it worked .
    Still, i have one more question .
    Let's say i Press de button, Form2 appears ( modally , that means i Form1 is disabled ).
    I input some text into Form2's LineEdit and then i press a button, also on Form2. When I press the button, Form2 closes and Form1 becomes active again .

    What I want to do now is to take the text from LineEdit on Form2 and show it somewhere in Form1 . I managed to get the actual text using a global variabile. But ... how can i detect that Form2 has closed ? For example, let's say in Form1 I also have a LineEdit. When I close Form2 , I want Form1 LineEdit to have the text from Form2 LineEdit .

    How can I accomplish this ?

    I tried the followind code:

    Qt Code:
    1. void App1::button_clicked()
    2. {
    3. App2 *form2 = new App2(this);
    4. form2->show();
    5. msg.setText(text);
    6. msg.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    hoping that the code after form2->show() would be executed after Form2 was closed.Unfortunately, the code executes as soon as Form2 appears on the screen.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Handling multiple forms

    override the okay() slot (if using a QDialog): store your lineedit's contents in your dialog.
    at the call site (when exec() returned), call a getter-function to access that text. do not use globals for that. it is ugly and will cause you trouble. (e.g. you can not use two such dialogs that way).

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

    msmihai (6th December 2008)

  6. #5
    Join Date
    Dec 2008
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling multiple forms

    could you give me an example ? i'm relative new to QT ... I don't fully understand what you explain to me ... and no .. I have a QmainWindow and a QWidget.

Similar Threads

  1. PyQt - Mapping multiple forms to a database
    By peterjb31 in forum Qt Programming
    Replies: 4
    Last Post: 2nd May 2008, 21:31
  2. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23
  3. Handling multiple UDP sockets
    By ecphora in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 20:01

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
  •  
Qt is a trademark of The Qt Company.