PDA

View Full Version : How to close a QDialog??



Bong.Da.City
27th August 2010, 23:58
I had created a Qdialog from another .ui file. That is the way i open it.

preferences *gamatos = new preferences(this);
gamatos->show();

So on the QDialog how with a press of a button close the QDialog...


Also you know on the mainwindow ( where i had a button which opens the QDialog i mentioned before) i have a push button. when you press it opens a QMessageBox. And if you try to click somewhere on the mainwindows it won't let you until you close the QMessageBox. Show how can i do the same when the dialog i said before opens?

waynew
28th August 2010, 00:17
To "show" a dialog as modal, use dialog->exec(); instead of show()
or in your case gamatos->exec();

Don't for get to delete gamatos; when the dialog returns to the line after the exec();

To close your dialog, put a close button on the form. Use a signal/slot to connect it to a function where you close();

Bong.Da.City
28th August 2010, 00:39
Sorry we are on the Newbie Section.. Couldn't you tell me with more Details?
ok i did what you said with the exec instead of show. but how to connect the dialog with the close(). Something like the thing below (which doesn't work)?
gamatos->addAction("pushButton_3",this,SLOT(close()));

waynew
28th August 2010, 00:57
Did you make your form with Designer? It makes a difference to the example to provide.

Bong.Da.City
28th August 2010, 01:04
No with the QtDesigner that is in the Qt creator.. You know :)

waynew
28th August 2010, 01:10
That's the Designer I meant :)
Ok, here is what to do: open the form for editing, right click on your close button, left click on "Go to Slot..."
This will create a slot for you in the .h and .cpp files and immediately show the .cpp slot code in your editor.
Inside that slot function, put the code: close(); and any other code you need to execute before closing the dialog - your choice.
Now your dialog is modal (by using the exec() to open it, so no other window is active until you close it, and clicking the 'close' button or whatever you called it, will close your dialog.
This is the cheap and easy way of making your close or cancel button work.
Does that do it?

Bong.Da.City
28th August 2010, 01:18
i know about the go slot and all that.. ok i added just close(); and the button doesn't do enything..


void preferences::on_pushButton_3_clicked()
{
close();
}

waynew
28th August 2010, 02:01
Ok, try this then:

<yourdialogname>::close();

Bong.Da.City
28th August 2010, 02:38
void preferences::on_pushButton_3_clicked()
{
<gamatos>::close();
}

4 errors :P

And if i do that


void preferences::on_pushButton_3_clicked()
{
gamatos::close();
}

It says gamatos was not declared on this scope...

waynew
28th August 2010, 02:41
Bong, the <> is just notation to indicate this is where to insert your value.

What is the name of your dialog class?

Bong.Da.City
28th August 2010, 03:04
ok on my friend's laptop had worked with close(); so leave it... can you now answer to the second question?





Also you know on the mainwindow ( where i had a button which opens the QDialog i mentioned before) i have a push button. when you press it opens a QMessageBox. And if you try to click somewhere on the mainwindows it won't let you until you close the QMessageBox. Show how can i do the same when the dialog i said before opens?

waynew
28th August 2010, 03:11
I think we already covered that. It's a modal vs non-modal dialog issue.
Modal dialogs - shown with exec(); won't let you activate any other window in the application until you close them, exactly as you described the message box behavior. Non-modal are the opposite. You can leave them open and go to other windows in the application.
There are application reasons to use both kinds.
When I said to show your dialog with exec(); that was to make it modal as you described the behavior you wanted.
See http://doc.trolltech.com/4.5/qdialog.html#details

Bong.Da.City
28th August 2010, 11:05
Ok i didn't understand it before.. Thanks it works :)