View Full Version : How to regain parent's window functions,after child window has closed
mekos
3rd June 2008, 21:05
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:
======Class MainWindow========
MainWindow::MainWindow( QWidget * parent, Qt::WFlags f ): QMainWindow( parent, f )
{
setupUi( this );
connect( showBtn, SIGNAL( clicked() ), this, SLOT( showUsers() ) );
}
void MainWindow::showUsers( )
{
//actons etc.etc.
Users *usr;
usr->show();
}
======Class Users========
Users::Users( QWidget * parent ) : QDialog( parent )
{
setupUi(this);
setModal(true);
connect( testButton, SIGNAL( clicked( ) ), this, SLOT(changeSomething( ) ) );
}
void Users::changeSomething( )
{
//actons etc.etc.
and somewhere here i have to use a function from mainwindow
to update something e.g mainwindow->refresh();
}
Can anyone help??
Thanks
wysota
3rd June 2008, 21:33
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().
jacek
3rd June 2008, 21:35
If Users is going to be a modal dialog, you can do it this way:
void MainWindow::showUsers( )
{
Users usr;
if( usr.exec() == QDialog::Accepted ) {
changeSomething( usr.getSomething() );
}
}
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.
mekos
3rd June 2008, 21:58
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
void Users::changeSomething( )
{
//actons etc.etc.
QWidget *main=this->parentWidget();
main->(should appear mainwindow's functions here?)
}
jacek
3rd June 2008, 22:28
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.
mekos
3rd June 2008, 22:44
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 :D
======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
jacek
3rd June 2008, 23:04
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 :D
First you need to define a custom signal:
class Users : ...
{
Q_OBJECT
...
signals:
void yourSignal();
...
};
then you can emit it:
emit yourSignal();
wysota
3rd June 2008, 23:04
wysota i tried
void Users::changeSomething( )
{
//actons etc.etc.
QWidget *main=this->parentWidget();
main->(should appear mainwindow's functions here?)
}
Cast the pointer to appropriate class:
QWidget *wgt = parentWidget();
MainWindow *main = qobject_cast<MainWindow*>(wgt);
Q_ASSERT(main!=0);
main->...
mekos
3rd June 2008, 23:16
Thank you both
i'll try your suggestions..hope i find it eventually :)
mekos
3rd June 2008, 23:36
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?
void MainWindow::showUsers( )
{
//actons etc.etc.
Users *usr;
usr->show();
//declare that usr is child somewhere here? how?
}
Or maybe i have created the two constructors the wrong way so and i'll never figure out something
mekos
3rd June 2008, 23:47
ok finally
done it with the other way
Users *usr;
if( usr->exec() ) {
usr->changeSomething() );
}
thank you both:)
wysota
4th June 2008, 07:22
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.