Results 1 to 12 of 12

Thread: How to regain parent's window functions,after child window has closed

  1. #1
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to regain parent's window functions,after child window has closed

    Hello

    i have 2 windows,one mainwindow and one dialog.a signal emmited from the mainwindow and the dialog appears.Do something in the dialog and before i close it i have to call a function from the mainwindow to refresh a mainwindow's label.
    How can i do that?

    example code:
    Qt Code:
    1. ======Class MainWindow========
    2. MainWindow::MainWindow( QWidget * parent, Qt::WFlags f ): QMainWindow( parent, f )
    3. {
    4. setupUi( this );
    5. connect( showBtn, SIGNAL( clicked() ), this, SLOT( showUsers() ) );
    6. }
    7.  
    8.  
    9.  
    10. void MainWindow::showUsers( )
    11. {
    12. //actons etc.etc.
    13. Users *usr;
    14. usr->show();
    15.  
    16. }
    17.  
    18.  
    19. ======Class Users========
    20. Users::Users( QWidget * parent ) : QDialog( parent )
    21. {
    22. setupUi(this);
    23. setModal(true);
    24. connect( testButton, SIGNAL( clicked( ) ), this, SLOT(changeSomething( ) ) );
    25. }
    26.  
    27.  
    28. void Users::changeSomething( )
    29. {
    30. //actons etc.etc.
    31. and somewhere here i have to use a function from mainwindow
    32. to update something e.g mainwindow->refresh();
    33. }
    To copy to clipboard, switch view to plain text mode 

    Can anyone help??

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to regain parent's window functions,after child window has closed

    What exactly is the problem? Accessing the window's pointer? There are at least two ways to do it, either pass a pointer to the dialog or use QWidget::parentWidget().

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to regain parent's window functions,after child window has closed

    If Users is going to be a modal dialog, you can do it this way:
    Qt Code:
    1. void MainWindow::showUsers( )
    2. {
    3. Users usr;
    4. if( usr.exec() == QDialog::Accepted ) {
    5. changeSomething( usr.getSomething() );
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    If it is a modeless dialog, you can use signals & slots mechanism. Just define some signal in the Users class and connect it to proper slot in MainWindow.

    Third option is to pass a pointer to MainWindow instance to User, but this isn't good as it introduces tight coupling.

  4. #4
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to regain parent's window functions,after child window has closed

    jacek that would be good but the problem is i have a lot of functions in the dialog,it's just one of them which when called then i have to refresh the mainwindow ..the above code was just a mini example.

    wysota i tried
    Qt Code:
    1. void Users::changeSomething( )
    2. {
    3. //actons etc.etc.
    4. QWidget *main=this->parentWidget();
    5. main->(should appear mainwindow's functions here?)
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to regain parent's window functions,after child window has closed

    Quote Originally Posted by mekos View Post
    the problem is i have a lot of functions in the dialog,it's just one of them which when called then i have to refresh the mainwindow ..
    I don't see a problem. Just make it emit a custom signal and connect that signal to a slot in MainWindow.

  6. #6
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default

    If i knew how to connect the User class with the mainwindow object, to emit a signal to User to Mainwindow it would be great

    ======Class Users========
    Users::Users( QWidget * parent ) : QDialog( parent )
    {
    setupUi(this);
    setModal(true);
    connect( testButton, SIGNAL( clicked( ) ), this, SLOT(changeSomething( ) ) );
    connect( testButton, SIGNAL( clicked( ) ), MainWindow, SLOT(refresh( ) ) );
    }

    this is not working..some example code would be great..

    correction:
    *to emit a signal FROM Users to Mainwindow
    Last edited by wysota; 3rd June 2008 at 23:06. Reason: Posts merged

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to regain parent's window functions,after child window has closed

    Quote Originally Posted by mekos View Post
    If i knew how to connect the User class with the mainwindow object, to emit a signal to User to Mainwindow it would be great
    First you need to define a custom signal:
    Qt Code:
    1. class Users : ...
    2. {
    3. Q_OBJECT
    4. ...
    5. signals:
    6. void yourSignal();
    7. ...
    8. };
    To copy to clipboard, switch view to plain text mode 
    then you can emit it:
    Qt Code:
    1. emit yourSignal();
    To copy to clipboard, switch view to plain text mode 

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

    mekos (3rd June 2008)

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to regain parent's window functions,after child window has closed

    Quote Originally Posted by mekos View Post
    wysota i tried
    Qt Code:
    1. void Users::changeSomething( )
    2. {
    3. //actons etc.etc.
    4. QWidget *main=this->parentWidget();
    5. main->(should appear mainwindow's functions here?)
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 
    Cast the pointer to appropriate class:

    Qt Code:
    1. QWidget *wgt = parentWidget();
    2. MainWindow *main = qobject_cast<MainWindow*>(wgt);
    3. Q_ASSERT(main!=0);
    4. main->...
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to wysota for this useful post:

    mekos (3rd June 2008)

  11. #9
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to regain parent's window functions,after child window has closed

    Thank you both
    i'll try your suggestions..hope i find it eventually

  12. #10
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to regain parent's window functions,after child window has closed

    I'm afraid Users is not child of Mainwindow..
    Q_ASSERT(main!=0) fails.
    debugger output:
    Cannot access memory at address 0x0
    and the variable parent is 0x0..
    do i have to set that the Users dialog is child of MainWindow?

    Qt Code:
    1. void MainWindow::showUsers( )
    2. {
    3. //actons etc.etc.
    4. Users *usr;
    5. usr->show();
    6. //declare that usr is child somewhere here? how?
    7. }
    To copy to clipboard, switch view to plain text mode 

    Or maybe i have created the two constructors the wrong way so and i'll never figure out something

  13. #11
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to regain parent's window functions,after child window has closed

    ok finally
    done it with the other way

    Qt Code:
    1. Users *usr;
    2. if( usr->exec() ) {
    3. usr->changeSomething() );
    4. }
    To copy to clipboard, switch view to plain text mode 

    thank you both

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to regain parent's window functions,after child window has closed

    If usr is a dialog, you can pass a parent to it when creating it. Currently you have a modal dialog without pointing which window should be blocked until you close the dialog.

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.