Results 1 to 2 of 2

Thread: Switching between two windows

  1. #1
    Join Date
    May 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Switching between two windows

    I am very much new to Qt and I have a simple doubt on switching between the windows.

    I have a main window and I am creating a secondary window using a different class. I am hiding the primary window using hide( ) function and only the second window belonging to different class is visible. When the user presses the close button I have to show back the first window. My code looks something like this.

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

    Important lines belonging to FirstWindow.h.
    Qt Code:
    1. #include "SecondWindow.h"
    2. class FirstWindow : public QDialog
    3. {
    4. Q_OBJECT
    5.  
    6. private:
    7.  
    8. SecondWindow * window2;
    9.  
    10. public:
    11. void some_function( ){
    12.  
    13. this -> hide( );
    14. window2 = new SecondWindow( );
    15. window2 -> displayTheWindow( );
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Important lines belonging to SecondWindow.h

    Qt Code:
    1. class SecondWindow : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. private:
    6.  
    7. QDialog * dialog;
    8.  
    9. public slot:
    10.  
    11. void startFirstWindow( );
    12.  
    13. public:
    14. SecondWindow( ){
    15. dialog = new QDialog( );
    16. connect( this, SIGNAL( rejected( ) ), this, SLOT( startFirstWindow( ) ) );
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 


    Can anybody please help me how should I write the slot of startFirstWindow( ). I just want to make the first window visible again. Or is there another approach to this without much changes.

    Thanks in advance for the help.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Switching between two windows

    For socond widget to show the fisrt window it should have the firstwindow's handle (pointer), only then it can connect to firtwindow's slot or call hide directly.

    Here is an small example to show and hide two widget from one another. Note this just an example how to do it.
    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3.  
    4. class Widget : public QWidget
    5. {
    6. public:
    7. explicit Widget(QWidget * parent = 0)
    8. : QWidget(parent)
    9. , mPushButton(new QPushButton("Hide self and show Partner", this))
    10. , mPartner(0)
    11. { }
    12.  
    13. void setPartner(QWidget * partner)
    14. {
    15. if(partner == 0)
    16. return;
    17.  
    18. if(mPartner != partner)
    19. {
    20. if(mPartner != 0)
    21. {
    22. disconnect(mPushButton, SIGNAL(clicked()), this, SLOT(hide()));
    23. disconnect(mPushButton, SIGNAL(clicked()), mPartner, SLOT(showMaximized()));
    24. }
    25.  
    26. mPartner = partner;
    27.  
    28. connect(mPushButton, SIGNAL(clicked()), this, SLOT(hide()));
    29. connect(mPushButton, SIGNAL(clicked()), mPartner, SLOT(showMaximized()));
    30. }
    31. }
    32. private:
    33. QPushButton * mPushButton;
    34. QWidget * mPartner;
    35. };
    36.  
    37. int main(int argc, char *argv[])
    38. {
    39. QApplication app(argc, argv);
    40.  
    41. Widget w1;
    42. Widget w2;
    43.  
    44. w1.setWindowTitle("Widget 1 - Look I have Changed");
    45. w2.setWindowTitle("Widget 2 - Look I have Changed");
    46.  
    47. w1.setPartner(&w2);
    48. w2.setPartner(&w1);
    49.  
    50. w1.showMaximized();
    51.  
    52. return app.exec();
    53. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Switching between models
    By janorcutt in forum Qt Programming
    Replies: 1
    Last Post: 24th January 2012, 20:52
  2. Qt Creator Switching between .ui and .h/.cpp (and help)
    By CoderMan in forum Qt Tools
    Replies: 3
    Last Post: 15th August 2011, 08:05
  3. Switching between 2 Forms
    By strateng in forum Qt Programming
    Replies: 6
    Last Post: 4th June 2010, 09:09
  4. Switching between two databases
    By codeman in forum Qt Programming
    Replies: 20
    Last Post: 13th May 2009, 15:33
  5. Switching 5.2 from 5.0.2
    By baray98 in forum Qwt
    Replies: 1
    Last Post: 11th May 2009, 18:59

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.