Results 1 to 7 of 7

Thread: how to display window(Window A), when clicked on a button of another window(Window B)

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default how to display window(Window A), when clicked on a button of another window(Window B)

    I hav created two windows, window_A and window_B
    I hav button "SHOW" on window_B, when i click on that i want to display window_A.
    How can i do that?
    I hav files
    window_A.h
    window_B.h
    window_A.cpp
    window_B.cpp
    main.cpp

    where should i write that code?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,628
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to display window(Window A), when clicked on a button of another window(Windo

    With QWidget::show() or QDialog::exec(). You should write that code in the slot that handles clicks on the show button.

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to display window(Window A), when clicked on a button of another window(Windo

    Hi thank u.!
    But i'm getting compiler error saying ambiguity in "Show()" function..
    I think something wrong with inheritance i made....
    I inherited window_A in window_B

  4. #4
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to display window(Window A), when clicked on a button of another window(Windo

    in window_A.h

    include "window_b.h"

    private slot:

    void showWindow_B();

    and pointer to window_b in private so

    Window_B *myWindow_B;

    in window_A.cpp

    add
    in your main funtion

    connect(ui.startButton, SIGNAL(clicked()), this, SLOT(showWindow_B());


    void Window_A::showWindow_B
    {
    myWindow_B.show();
    {


    works in my current project with 5 uis

  5. #5
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to display window(Window A), when clicked on a button of another window(Windo

    Quote Originally Posted by prophet0 View Post
    in window_A.h

    include "window_b.h"

    private slot:

    void showWindow_B();

    and pointer to window_b in private so

    Window_B *myWindow_B;

    in window_A.cpp

    add
    in your main funtion

    connect(ui.startButton, SIGNAL(clicked()), this, SLOT(showWindow_B());


    void Window_A::showWindow_B
    {
    myWindow_B.show();
    {


    works in my current project with 5 uis

    hi this is what i did....


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



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


    window_a.cpp
    Qt Code:
    1. #include "window_a.h"
    2. #include "ui_window_a.h"
    3. #include "window_b.h"
    4. window_a::window_a(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::window_a)
    7. {
    8.  
    9. ui->setupUi(this);
    10. }
    11.  
    12. window_a::~window_a()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void window_a::on_pushButton_clicked()
    18. {
    19. window_b * mywindow_b;
    20. mywindow_b->show();
    21. }
    To copy to clipboard, switch view to plain text mode 



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



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




    here when i executed the program in qt, window_a displays and when i click on the button(i.e to display window_a), program hangs......nothing comes!!!


    what is the mistake i made????

  6. #6
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to display window(Window A), when clicked on a button of another window(Windo

    It is addition to previous post.....
    when i changed the window_a.cpp as shown below.....One window flashes and disappears..!!!
    what is the wrong....please help me to sort it..
    Thanks in advance..

    window_a.cpp
    Qt Code:
    1. #include "window_a.h"
    2. #include "ui_window_a.h"
    3. #include "window_b.h"
    4. window_a::window_a(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::window_a)
    7. {
    8.  
    9. ui->setupUi(this);
    10. }
    11.  
    12. window_a::~window_a()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void window_a::on_pushButton_clicked()
    18. {
    19. window_b mywindow_b;
    20. mywindow_b.show();
    21. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    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: how to display window(Window A), when clicked on a button of another window(Windo

    The first method should work if the object is created
    Also it is always safe to have a parent to any QObject based object.
    Qt Code:
    1. void window_a::on_pushButton_clicked()
    2. {
    3. window_b * mywindow_b = new window_b(this);
    4. mywindow_b->show();
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 9th November 2011 at 04:39. Reason: updated contents

  8. The following user says thank you to Santosh Reddy for this useful post:

    aurora (15th November 2011)

Similar Threads

  1. Replies: 2
    Last Post: 17th February 2011, 12:30
  2. Replies: 3
    Last Post: 23rd December 2010, 06:55
  3. Replies: 0
    Last Post: 10th September 2010, 13:23
  4. Replies: 11
    Last Post: 4th June 2008, 07:22
  5. Maximize Window button of Manin Window
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2007, 08:20

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.