Results 1 to 7 of 7

Thread: How to Open & Close a Widget ?!!

  1. #1
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Question How to Open & Close a Widget ?!!

    I've created an app that contains a Widget . I've put all the ctrls I need into it .
    I've created my own Slots by creating a (Header + cpp) files to implement the functionality I want .

    Everything ,so far , goes fine .


    Now I've created another Widget . when I try to display the 2nd widget it just appears for a second !!
    -I've created 2 HeaderFiles +2 .CPP files ; I mean each widget has its .cpp + .h .


    My Questions are :

    1- I need when the user press a button @ the 1st widget
    , the 2nd widget shows .

    2- How Could I close a specific Widget ?!!
    3 - Do I 've to create @ my app a "Main Window"
    4- Finally , Do I 've to create a Header + .cpp file to each Widget I've ?!!

    My Main function's Code :

    Qt Code:
    1. #include <QApplication>
    2. #include <QDialog>
    3. #include"CustomSlot.cpp"
    4. #include "ui_Test.h"
    5.  
    6.  
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11.  
    12. CustomSlot widget;
    13. widget.show();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    -------------------------------------------------------------------------------------------
    First CustomSlot.cpp 's code :

    Qt Code:
    1. #include"CustomSlot.h"
    2. #include "ui_Test.h"
    3.  
    4. #include "ui_Form2.h"
    5. #include"CustomSlot2.cpp"
    6.  
    7.  
    8.  
    9.  
    10.  
    11.  
    12. CustomSlot::CustomSlot(QWidget *parent): QWidget(parent)
    13. {
    14. ui.setupUi(this);
    15. }
    16.  
    17.  
    18. void CustomSlot:: OpenNxtWindow()
    19. {
    20.  
    21. CustomSlot2 widget;
    22. widget.show();
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 
    -----------------------------------------------------------------
    Second CustomSlot.cpp 's code :

    Qt Code:
    1. #include"CustomSlot2.h"
    2.  
    3. #include "ui_Form2.h"
    4.  
    5.  
    6.  
    7. CustomSlot2::CustomSlot2(QWidget *parent): QWidget(parent)
    8. {
    9. formTwo.setupUi(this);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to Open & Close a Widget ?!!

    The variable goes out of scope according to normal C++ rules and gets immediately destructed after the OpenNxtWindow() function ends. To make it remain alive you should allocate it on the heap with operator "new":
    Qt Code:
    1. void CustomSlot:: OpenNxtWindow()
    2. {
    3. CustomSlot2* widget = new CustomSlot2;
    4. widget->setAttribute(Qt::WA_DeleteOnClose);
    5. widget->show();
    6. }
    To copy to clipboard, switch view to plain text mode 
    or if it's a dialog you can use QDialog::exec() to produce a modal dialog:
    Qt Code:
    1. void CustomSlot:: OpenNxtWindow()
    2. {
    3. CustomSlot2 widget(this);
    4. widget.exec();
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    Fatla (12th June 2008)

  4. #3
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to Open & Close a Widget ?!!

    As usual , Jbn , Your answeris accurate .

    So, I'll need a header + cpp files to each form I've .

    Thanks .
    Last edited by Fatla; 12th June 2008 at 18:34.

  5. #4
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to Open & Close a Widget ?!!

    Well , I've another inquiry .
    - After I've opened a new widget , I 've put Button + QLineEdit @ the new open Widget just to test it .

    - What I 've found out is that ctrls @ the new open widget don't call the slots I implement ?!!
    - @ the new widget it's supposed when the user press the button , a text 'll be printed into QLineEdit .

    - When I Put the code that print some text into QLineEdit into Constructor , it Works .
    - Also when I use the built in slots (e.g. showMaximized() ) , it works .

    - My Question : Why don't the ctrls @ the new open Widget call the slots I implement ?!!

    N.B. : I've a Header + .cpp file to each form I've in order to implement my own slots .


    The code which I use to open my new widget that's located @ the first .cpp which I implement into it the slots of the 1st widget :

    Qt Code:
    1. CustomSlot2* widget = new CustomSlot2;
    2. widget->setAttribute(Qt::WA_DeleteOnClose);
    3. widget->show();
    To copy to clipboard, switch view to plain text mode 

    ----------------------------------------------------------------------
    My .cpp file 's code , which I implement the slots of ctrls of 2nd new open widget :
    Qt Code:
    1. #include"CustomSlot2.h"
    2.  
    3. #include "ui_Form2.h"
    4.  
    5. CustomSlot2::CustomSlot2(QWidget *parent): QWidget(parent)
    6. {
    7. formTwo.setupUi(this);
    8.  
    9. }
    10.  
    11. void CustomSlot2::PrintTxt
    12. {
    13. formTwo.txtData->setText("Alooooooo") ;
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Last edited by Fatla; 13th June 2008 at 21:05.

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to Open & Close a Widget ?!!

    Did you remember to add required Q_OBJECT macro to CustomSlot2 class? Did you remember to declare CustomSlot2::PrintTxt() as slot?
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    Fatla (13th June 2008)

  8. #6
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to Open & Close a Widget ?!!

    Mmmmm , actually it was a SILLY error ; I forgot to write the keyword "slot" beside the access modifier @ header file .

    By the way , what's the use of :

    widget->setAttribute(Qt::WA_DeleteOnClose);

    As when I 've commented it , nothing is changed !!

    Thanks JPN

  9. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to Open & Close a Widget ?!!

    Quote Originally Posted by Fatla View Post
    By the way , what's the use of :

    widget->setAttribute(Qt::WA_DeleteOnClose);

    As when I 've commented it , nothing is changed !!
    It ensures that the widget gets deleted once closed. You know, closing a window doesn't delete it by default. And there is no parent to clean it up either because it has no parent, it's a top level widget aka window.

    Thanks JPN
    You're welcome.
    J-P Nurmi

Similar Threads

  1. How to hide Close Button "X" on Widget ?
    By merry in forum Qt Programming
    Replies: 8
    Last Post: 25th January 2020, 09:03
  2. Close Button on Modal Widget?
    By vishal.chauhan in forum Qt Programming
    Replies: 5
    Last Post: 18th February 2008, 12:38
  3. Open Scene Graph widget?
    By Caius Aérobus in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2007, 14:23
  4. Replies: 3
    Last Post: 17th October 2007, 13:52
  5. Open FileDialog showing Back of the Widget in Mac
    By shyam prasad in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2007, 07:33

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.