PDA

View Full Version : Quit action in MainWindow



Backslash
31st July 2007, 07:37
I'm attempting to close a form when quit is selected in the tool bar. The form has an action called actionQuit which is written as such in the MainWindow.cpp file:


void MainWindow::on_actionQuit_triggered()
{

}

How do I actually call the form to close inside of this? do I reference it as "ui.something"? Sorry if this is a seemingly simple question, I just cannot find the answer on my own.

Thank you,
Backslash

ufo-vl
31st July 2007, 08:21
void MainWindow::on_actionQuit_triggered()
{
close();
}

Michiel
31st July 2007, 10:43
Yes, or in the constructor:

connect(actionQuit, SIGNAL(triggered()), this, SLOT(close()));

You don't actually need another function. This is textbook signals/slots.

WinchellChung
31st July 2007, 14:58
Yes, as another newbie I can attest to the fact that Qt's signals and slots are one of its most powerful features, and will make your life so much easier once you start using them.

Backslash
31st July 2007, 17:13
Wow, I guess the problem was...I never thought it could be that easy. Thanks very much for the quick replies.

Thanks Again,
Backslash

Backslash
31st July 2007, 17:39
I have another question that is along the same lines...

I want to directly set the text for a widget on a second form when a button is pressed on the first form, can I do this? I'm trying to use auto-connect so I have a button's clicked signal connected here:


void MainWindow::on_push_enter_clicked()
{
KeyDialog dialog(this);
//here I would set the text
dialog.exec();
}

Thanks, this is probably also easy...
Backslash

Michiel
31st July 2007, 17:55
If you created the KeyDialog yourself, you can add a public function, or an option in the constructor, to change that text. It would look something like this when called:


void MainWindow::on_push_enter_clicked() {
KeyDialog dialog(this, "text");
dialog.exec();
}

Backslash
31st July 2007, 18:56
Thank you, that makes sense. I'll give it a try...

While we're on the subject though, Is there there a way to reference widgets on one form from another form?

Thanks,
Backslash

Michiel
31st July 2007, 19:05
Keep in mind that Qt forms (and all other Qt widgets) are simply C++ classes. As with every C++ class, members that are public are visible from the outside.

So yes, you can make the widgets public instead of private or protected. But this is considered bad practice, since it breaks encapsulation.

Backslash
31st July 2007, 22:21
Thanks again for your patient explanation of what should be simple concepts...I guess I wasn't thinking of it the way I should have been (as classes) and it now makes perfect sense.

Thanks,
Backslash